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

2
  • I'm amazed FF shows that too! Commented Feb 2, 2013 at 0: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. Commented Feb 2, 2013 at 0:12

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Ahhh! I knew it. Thanks for the link :)
Darn, my O'Reilly Javascript: The Definitive Guide is out of date and I didn't know it.
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
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;

Comments

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
}

4 Comments

maybe with two fewer lines though.
And this is how 100% of my code and every other argument checking code I have ever seen in JavaScript looks.
Can do something like: some_val = (typeof some_val !== 'undefined') ? some_val : 'defaultVal';
@javaBean007 Correct, there are shorter ways to do this. But it is a beginner question, so I opted for the most descriptive solution that is the easiest to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.