Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do function arguments support a default value in JavaScript?

While looking at a question related to porting a PHP function to JavaScript. I saw what I assumed was incorrect JavaScript:

function my_isnum(str, negative=false, decimal=false)

Then I tried this in JSFiddle:

function my_isnum(str, negative=false, decimal=-2)
{
    console.log(arguments);
    console.log(str);
    console.log(negative);
    console.log(decimal);
}
my_isnum("5", "Hi");

And to my utter amazement this is what I see in the Firebug console:

["5", "Hi"]
5
Hi
-2

Now in Chrome this is what I see:

Uncaught SyntaxError: Unexpected token = 

What I don't understand is this an example of some early standard being supported by Firefox (the MDN on function doesn't seem to mention this)?

like image 218
Jason Sperske Avatar asked Feb 02 '13 00:02

Jason Sperske


People also ask

Can a function argument have default value?

Once a default value is used for an argument in the function definition, all subsequent arguments to it must have a default value as well. It can also be stated that the default arguments are assigned from right to left.

Does JavaScript support default parameter values?

In JavaScript, a parameter has a default value of undefined. It means that if you don't pass the arguments into the function, its parameters will have the default values of undefined .

How do function arguments work in JavaScript?

JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value. Changes to arguments are not visible (reflected) outside the function.

Is argument the default?

In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this is the case in the C programming language).


2 Answers

This seems to be in the ECMAScript 6 specification and is at the moment only supported by Firefox

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/default_parameters

like image 119
lostsource Avatar answered Oct 04 '22 21:10

lostsource


Lostsource's answer is correct, come ECMA6, default values are very likely to be supported, I think it will, but as it is still a working draft, you can't really be sure... For now, you can use the logical or:

function foo(bar, foobar)
{
    bar = bar || 'foobar';//once
    foobar = foobar || !!bar || true;//or multiple times

This works, sort of, like a ternary. The expressions are resolved, from left to right: as soon as JS encounters a truthy value, that's what will be assigned:

var someVar = false || undefined || null || 0 || 1;

Will assign 1 to someVar. If no values are passed to a function, all arguments are assigned undefined by default, so in that case:

myArgument = myArgument || 'default';
//is the same as:
myArgument = undefined || 'default';//evaluates to "default"

But when you pass false as an argument, or null, or an empty string, the default value will be assigned, so be careful.
In those cases an if/ternary is a better fit (as seen in theJollySin's answer). The ternary equivalent of which is:

some_val = typeof some_val === 'undefined' ? 'default' : some_val;
like image 26
Elias Van Ootegem Avatar answered Oct 04 '22 21:10

Elias Van Ootegem