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.

4
  • 1
    possible duplicate of Handling optional parameters in javascript Commented Dec 28, 2011 at 9:54
  • 1
    This question doesn't have anything to do with jQuery, it is only about JavaScript. Commented Dec 28, 2011 at 9: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. Commented Dec 28, 2011 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. Commented Dec 28, 2011 at 10:07

6 Answers 6

8

Just changing the definition of the function will work

function 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

function test(name, age)
{
    age = (age === undefined) ? 0 : age; // change 0 to the default value you want..
}

Update

There is now support for default parameters

function test(name, age = 0)
{
  // age will be 0 if no second parameter is provided or it is undefined
}
Sign up to request clarification or add additional context in comments.

Comments

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';
   ...
 }

Comments

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.

Comments

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.

1 Comment

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.)
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
}

3 Comments

What is NULL? It will be undefined.
better to check for undefined as if arguments[1] is set to false the check will fail
All good comments - I have edited my answer - does it look better now?
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;
  ...
}

3 Comments

What is NULL? It will be undefined.
NULL is referenced in the original question. I am presuming they have defined its meaning.
I think in the question the OP has tried what works in PHP, since NULL is a special value in PHP. But fair enough, I see your point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.