views:

27

answers:

3

In PHP we can specify default value of variable in function like:

function myFunction(myDefaultVariable, myOtherVariable, myCheckVariable = "basic"){
    // so yeah, myDefaultVariable is required always,
    // same applies for myOtherVariable,
    // and myCheckVariable can be skipped in function call, because it has a default value already specified.
}

Is there something similar to this in JavaScript?

+3  A: 

You don't need to pass all the variables in Javascript.

Although a less hacky way would be to use objects:

function foo(args) {
    var text = args.text || 'Bar';

    alert(text);
}

To call it:

foo({ text: 'Hello' }); // will alert "Hello"
foo(); // will alert "Bar" as it was assigned if args.text was null
Michael Shimmins
You are basically passing a single argument: an Object with the properties that correspond to "arguments; do I understand it right?
Piskvor
Ah, thanks! Exactly what I was looking for!
Tom
@Piskvor - yes correct. Its a single argument that contains attributes you can access. Any of these can be null, so its important to check and not assume they have been specified. Often, for 'mandatory' arguments, I check if they're supplied at the top of the method and throw an exception if not. `if(!('argument' in args)) throw('argument is a required parameter');`
Michael Shimmins
Given a small amount of arguments this is no better than passing each argument individually and just doing the same `foo = foo || 'default';`. Also, you should probably explain that any *falsey* values will be ignored -- i.e. if `args.text` is `0` then `var text` will be `Bar`...
J-P
@J-P: Indeed. On the other hand, try passing each argument individually e.g. to this constructor, and you'll go mad - not least from trying to provide them all in the correct order: http://www.highcharts.com/ref/
Piskvor
just wanted to note that the foo=foo||'default' techniques fails when foo is supplied, but evaluates as false. If this is an issue, see my answer for an example which checks if foo is truly undefined.
Paul Dixon
+2  A: 

not exactly, but you can simulate it by checking if a value was passed and setting a default, e.g.

optionalArg = (typeof optionalArg == "undefined")?'defaultValue':optionalArg

Note that technique like this works even when optionalArg is supplied but evaluates as false - something that a simpler idiom like optionalArg=optionalArg || 'default' fails on.

Also inside every function you can access to an array called arguments which will contain all the arguments passed to the function, you can use that to have functions with variable length argument lists.

Paul Dixon
+1  A: 

None that I'm aware of:

But there are 2 ways to tackle this.

//1. function.arguments - this is advisable if you don't know the 
//   maximum number of passed arguments.

function foo() {
  var argv = foo.arguments;
  var argc = argv.length;
  for (var i = 0; i < argc; i++) {
    alert("Argument " + i + " = " + argv[i]);
   }
}

foo('hello', 'world');

//2. "Or" operator - this is good for functions where you know the
//   details of the optional variable(s).

function foo (word1, word2) {
   word2 = word2 || 'world';
   alert (word1 + ' ' + word2);
}

foo ('hello'); // Alerts "hello world"
sheeks06