2

I have a function

function test(name)
{

}

I call it with test("kanishka");

In one case, I want to pass two parameters (name and age) to this function:

test("kanishka",27);

In PHP, I can do this by setting a default value to age :

test(name , age = NULL)

Is there any way in JavaScript to do this?

I tried

test(name , age = NULL) 

but it gives errors.

I can declare 2 functions

test(name) and  test(name , age = NULL)

but it duplicates the code.

Or I can change my previous function call which took one parameter to two parameters, giving a default value for age:

test("kanishka" , 0)

but in this case I have to find all function call and change them.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • 1
    possible duplicate of [Handling optional parameters in javascript](http://stackoverflow.com/questions/1529077/handling-optional-parameters-in-javascript) – Felix Kling Dec 28 '11 at 09:54
  • 1
    This question doesn't have anything to do with jQuery, it is only about JavaScript. – Douglas Dec 28 '11 at 09:56
  • Regarding *I can declare 2 functions*: No, you can't JavaScript does not support function overloading. If you declare two functions with the same name, the latter will override the former. – Felix Kling Dec 28 '11 at 10:03
  • 1
    _"I can declare 2 functions"_ - Not with the same name and scope you can't. You can give them different names. _"but it duplicates the code"_ - Not necessarily, you can put all the code in the function with both params and then the other function will simply call it with the default value for the second param. However, you don't need to do that at all, see the answers below. – nnnnnn Dec 28 '11 at 10:07

6 Answers6

8

Just changing the definition of the function will work

test(name, age)
{

}

All existing uses of the function will assign to age an undefined value..

Whenever you use a second parameter it will be assigned to the age parameter.

If you want a default value change the function to

test(name, age)
{
    age = (age === undefined) ? 0 : age; // change 0 to the default value you want..
}
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
4

basically you will do something like this

function foo(a, b)
 {
   a = typeof(a) != 'undefined' ? a : 42;
   b = typeof(b) != 'undefined' ? b : 'default_b';
   ...
 }
Yazan Malkawi
  • 501
  • 4
  • 10
1

You can still call the method, even if you pass the wrong number of arguments. The "optional" argument will be set to undefined, which you can check for using an if statement.

Douglas
  • 36,802
  • 9
  • 76
  • 89
1

You can use the arguments object which holds all arguments passed to a function - regardless of if they are defined or not...

test(name){
    var age = arguments[1] == undefined ? 0 : arguments[1]
    // do stuff here
}
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
1

You can either declare the function with the 2 arguments:

function test(name, age) {
   //...
}
test("James"); //age will be undefined
test("James", 22); //age will be 22

Or you could forget about the parameters altogether and use the arguments object:

function test() {
    console.log(arguments[0]);
}

This way you can pass in as many arguments as you like, and access them through the arguments object.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • Or do both. (Use case: if, say, the first few parameters made sense as named parameters but the rest formed a list of similar items.) – nnnnnn Dec 28 '11 at 10:06
0

The "standard" way to provide default values for methods is to check whether the arguments have been supplied at the start of the method body:

function test(name, age)
{
  age = typeof(age) != 'undefined' ? age : NULL;
  ...
}
Paul Turner
  • 38,949
  • 15
  • 102
  • 166