Search code examples
javascriptparameters

How can I mark a JavaScript function parameter as required?


When I'm writing JavaScript in IntelliJ IDEA, I can check the parameters of a function I'm calling and it will say something like:

Optional arguments

I'm not sure if this is an IntelliJ feature where it knows about the standard library or if it's a JavaScript feature that lets you mark parameters as optional/required. Either way, I'd like to learn how I can create my own functions with optional/required parameters. All I know is that optional seems to be the default because IntelliJ says all my function's parameters are optional.


Solution

  • If you are just concerned with the code completion bit then you can get that in the IntelliJ environment with the use of JSDoc comments. This does not affect your code, as others have pointed out you cannot specify to the JavaScript interpreter if a parameter is optional or not, but the editor will pick up the information from the comment and display it as you type. That way your own functions display similarly to the library functions.