Question: What is the proper way to define a function in JavaScript that takes optional parameters?
For example:
function myFunc(optionVar1) {
if(optionVar1 == undefined) {
...
} else {
...
}
}
myFunc('10'); // valid function call
myFunc(); // also a valid function call
Is it proper to use a ? mark like Ruby in the function declaration like so to denote optional parameters:
function myFunc(optionVar1?) {...} // <--- notice the ? mark
The first google response I discovered:
http://www.tipstrs.com/tip/354/Using-optional-parameters-in-Javascript-functions
I haven’t seen any instances where a question mark is used to alert a caller to an optional parameter. While it’s done in other languages, I don’t think it’s necessary in javascript.
In fact, it seems that you can’t use a question mark in the variable name. Only letters, numbers, $ and _.