Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
JS Doc , is there any way to specify "Choice" param?

I'm pretty new to JS and JS Doc and I had a question for optional parameters.

Imagine this function

/**
 * @param {string} [param1]
 * @param {string} [param2]
 */
const foo= (){...}

As JSDoc says, an optional parameter is like this

/**
 * @param {string} [somebody] - Somebody's name.
 */

Is there any way, with my example, to define 2 optional parameters but at least one is required ?(If param1 is set, param2 is not and if param2 is set, param1 is not).

Thanks for help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The square brackets around the parameter name means it's optional. To make it required, remove the squared brackets.

/**
 * @param {string} param1 - required
 * @param {string} [param2] - optional
 */
const foo= (){...}

You could also just use Typescript, for a more robust type system.

const foo = (param1: string, param2?: string);

Is there any way, with my example, to define 2 optional parameters but at least one is required ?(If param1 is set, param2 is not and if param2 is set, param1 is not).

To solve this problem, you can define multiple function overloads.

function foo(email:string);
function foo(username:string){
  if(username) return "Used username overlaod";
  if(email) return "Used email overload";
}

For JSDoc it's harder. Looks like this:

/**
 *
 * @constructor
 * @param {string} username
 *
 * @constructor
 * @param {string} email
 */
function foo(value) {
  // ...
}

I would recommend using TS over JSDoc.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!