17

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)?

Community
  • 1
  • 1
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • I'm amazed FF shows that too! – Popnoodles Feb 02 '13 at 00:07
  • Firefox's JavaScript is a superset of ECMAScript 3. It has features that are not standard syntax. Included among those are array comprehensions, `let` expressions, and destructuring assignment. – the system Feb 02 '13 at 00:12

3 Answers3

18

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

lostsource
  • 21,070
  • 8
  • 66
  • 88
  • Ahhh! I knew it. Thanks for the link :) – Jason Sperske Feb 02 '13 at 00:09
  • 1
    Darn, my O'Reilly Javascript: The Definitive Guide is out of date and I didn't know it. – matt Feb 02 '13 at 00:10
  • 1
    You can turn it on in chrome, too, by enabling experimental features, just open a tab and type `chrome://flags/` in the address bar... The implementation may differ from mozilla's, just like the ECMA6 [new `var` aka `let`](http://stackoverflow.com/questions/13568325/let-var-or-var-to-let) – Elias Van Ootegem Feb 02 '13 at 00:18
8

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;
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
4

Sort of. You can do something like:

function the_func(some_val) {
    // if some_val is not passed to the_func 
    if (typeof some_val == 'undefined') {
        some_val = 'default_some_val';
    }

    // now you can use some_val in the remaining parts of the method
}
john_science
  • 6,325
  • 6
  • 43
  • 60