0

I have the following javascript code:

    function toggleHiddenOps(dir, tour = false){
        // do stuff
    }

I noticed it was breaking in Chrome and IE (seems to work in FF 27). If I change the above to:

    function toggleHiddenOps(dir, tour){
        // do stuff
    }

It resolves the problem. Is there some issue with declaring variables like this in JS? Or perhaps my problem actually lies elsewhere?

Thanks.

5
  • 1
    Why are you wanting to assign a variable in the arguments, rather than in the function body itself? Commented Mar 2, 2014 at 15:32
  • To define a default value. This is possible in PHP. Commented Mar 2, 2014 at 15:33
  • It's possible to define defaults in JavaScript as well, but you have to do it in the function body (for most browsers, but I'm unsure as to why it's preferable to it the way you outline in your question). Commented Mar 2, 2014 at 15:34
  • That syntax is coming in ECMAScript6. At the moment, very few engines support it. Commented Mar 2, 2014 at 15:39
  • It's a case of writing more succinct code. I often use this syntax in PHP. Commented Mar 2, 2014 at 15:40

4 Answers 4

1

This functionality exists only in FF for now. It will be in ECMAScript6, so expect it to start showing up in other engines over time.

Chrome, IE, Opera and Safari don't support that syntax in their latest versions.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/default_parameters?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2Fdefault_parameters

A good workaround for this is to use arg= typeof arg !== 'undefined' ? arg : defaultValue;

For example:

function multiply(a, b) {
   b = typeof b !== 'undefined' ?  b : 1;
   return a*b;
}

multiply(5); // 5

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

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

1 Comment

Great, thanks for the info. Best answer, will accept in a minute when I am able. Seems like a shortcoming to me... as usual FF wins.
0

This is illegal in javascript.

If you want a default exactly false:

function toggleHiddenOps(dir, tour){
   tour = tour === void(0) ? false : tour;
   // do stuff
}

If you only care if its falsey:

function toggleHiddenOps(dir, tour){
   // do stuff, tour equals undefined now if not passed 2nd param
}

Comments

0

Javascript function don't support default paramter, a hack for it is:

function toggleHiddenOps(dir, tour){
    tour = typeof tour === 'undefined' ? 'default value' : tour
    console.log(dir)
    console.log(tour)
}
toggleHiddenOps('dir')

Comments

0

You cannot specify default values in JS using PHP syntax.

To be fair, draft ECMAScript 6 specification allows to specify default argument value like in PHP, and this is even supported in Firefox 15+, but note that support of ES6 itself is mostly experimental and can theoretically be changed or removed at any point.

A future-proof/cross-browser way to check in JS if argument is not specified is following:

function toggleHiddenOps(dir, tour) {
    if ('undefined' === typeof tour) {
        tour = 'Some default value';
    }

    // do stuff
}

8 Comments

Ah, that's what I suspected. Thanks.
You can't yet. It's coming in ES6.
"In JS, you can check if argument is not specified this way" That doesn't check whether the argument is unspecified. It checks whether the argument's value is undefined. It may be that it's undefined because it was unspecified; alternately, it may be that it was specified, but undefined.
@MaratTanalin: I don't consider it an issue. I'm just saying the text is incorrect. I'm not saying there's a problem with the code. Other than the completely unnecessary Yoda-speak, that's how I'd do it, too.
@MaratTanalin: No, of course I'm not. If you really want to know whether that argument was passed, arguments.length will tell you. (arguments being a pseudo-array created for every function call containing the actual arguments it was called with.) The check you're doing just checks for undefined, not whether the argument was passed, as I said originally. For supplying a default, I would use what you've put in the code, because arguments can be expensive on some engines. It's just the text saying what the code given does is wrong.
|

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.