0

In PHP we can define the argument value for the functions if it's not set(called), so for example:

<?php
    function blah($arg = false){
        var_dump($arg);
    }
?>

In the above example if we call the function like:

<?php
    blah();
    // ==> out put will be: false;
    blah(true);
    // ==> out put will be: true;
?>

So we can define a value for the arguments if they are not settled while we call the function, how this could be achieved in javascript functions?

I have it exactly like PHP:

<script>
    function blah(arg = false){
        //...
    }
</script>

The above code works just fine in Mozilla Firefox, but in Chrome, the function is not working and gets fixed when I remove = false in the parenthesis, Chrome developer tools says:

Uncaught Syntax Error: Unexpected token =

1
  • 3
    Default parameters are a part of ECMAScript 6. At the moment, only FF supports some of newcoming features. Commented Dec 15, 2013 at 12:32

5 Answers 5

3

This is not possible in Javascript. Try this Conditional Operator statement instead:

<script>
    function blah(arg){
       arg = typeof arg !== 'undefined' ? arg : 'someValue';
    }
</script>

Where 'someValue' is the default value that the arg variable will get when there are no arguments passed to the blah() function.

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

4 Comments

I think that arg !== undefined will do as well
@xpy: Be careful doing that. undefined in one window is not === undefined in another window, so if you're doing a library or any kind of app with multiple windows (frames)...
@xpy And in old browsers undefined can be reassigned I believe.
@thefourtheye: And can be shadowed even in new browsers, in loose mode. :-)
2

This is the cleanest pattern for implementing default arguments in javascript IMO.

function (arg) {
  arg = arg || 'defaultVale';
}

However this can fail if you expect the value of arg to be some falsy value, e.g 0, false, NaN, null using it is not really recommended.

This version protects against this case by explicitly comparing with undefined.

function (arg) {
  arg = arg === undefined ? 'defaultVale' : arg;
  // As T.J Crowder pointer out if not in strict mode or if the code will run 
  // in an IFrame use typeof arg === "undefined" instead of directly
  // comparing with undefined
}

Another nice pattern is using objects for arguments instead. This has two benefits

  1. Order of arguments is not important
  2. It's easy to implement default arguments

Code

var defaults = {
  arg1: 10,
  arg2: 20
};

var f = function (args) {
  args = jQuery.extend(true, args, defaults); //jQuery
  args = _.defaults(args, defaults); // Underscore
};

f({
  a: 25 //Use a non default value
});

3 Comments

Note that undefined can be shadowed, and the undefined in one window is not === the undefined in another window, so if you're writing any code that may be used in a multi-frame environment, that check is a bad idea. typeof arg === "undefined" is reliable.
@T.J.Crowder Very nice point T.J, but this is not possible in strict mode. And he should be using "strict mode" in any case.
@ Hugo: Strict mode has no effect on the multi-window (frames) situation.
0

In JavaScript there is no default parameter. Just write the code like:

function test(arg) {
    if(arg) {
        // do something as arg is defined.
    } else {
        // do something as arg is undefined.
    }
}

test(true); // arg is defined
test();  // arg is undefined

Comments

0

Simple variation

function defaulter(p1) {
    p1 = p1 || "default";
}

4 Comments

This should NOT be recommended. If the user explicitly passes falsy value for p1, it will still use default
Or "", NaN, null, false... There's a place for this, but only if the non-default value won't be falsey. Without a clear description, it's not useful to say "just do this."
@T.J.Crowder I was just changing it to falsy values :)
Yes, I could have added a comment regarding falsy values.
0

In Js you can't have default values for parameters. You can check if the data is of a known type with typeof operator:

function blah(arg)
{
    if (typeof arg === 'undefined')
    {
        arg = false;
    }
}

or setting his value in a short-circuit way

function blah(arg)
{
    arg = arg || false;
}

For example in coffeescript you can set it by using

blah = (arg = "mydefaultvalue") ->

that is translated into

blah = function(arg)
{
    if (arg == null) 
    {
        arg = "mydefaultvalue";
    }
}

Comments

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.