12

javascript beginner here.

Let's say I'm having a javascript function that takes 3 arguments:

function f(arg1, arg2, arg3) { // do stuff }

I know that I can call f(value1, value2); and in that case inside the function scope arg1 will be value1, arg2 will be value2 and arg3 will be null.

Everything ok with this. However if I want to call the function giving values only to arg1 and arg3 I need to do something like this: f(value1, null, value2);

Is there a way I can specify which arguments to have which values in a more C#-esque manner (without specifying not given arguments as null)? Something like this: for calling f with values only for arg1 and arg3 I would write f(value1, arg3 = value2);

Any ideas? Cheers!

1
  • No, but you can pass 2 arguments and check the type of the latter
    – Johan
    Sep 3, 2014 at 11:07

9 Answers 9

15

there is a way i have seen for this:

for example

function person(name,surname,age)
{
...
}

person('Xavier',null,30);

you can do this:

function person(paramObj)
{
   var name = paramObj.name;
   var surname = paramObj.surname;
   var age = paramObj.age;
}

calling like this:

person({name:'Xavier',age:30});

I think this is the closest you'll be able to do it like in c# have in mind that JS is not compilled so you can't predict the arguments of a function.

EDIT:

For better syntax you can use ES6 object destructuring, like this:

function person({name, surname, age})
{
   ...
}

https://javascript.info/destructuring-assignment

4

Yes you can. It can be written as:

f(arg1, undefined, arg3);

In this call, arguments 1 and 3 will pass, and argument 2 will not be sent.

1
  • 6
    argument 2 will be sent and it will have the value "undefined"
    – Vee6
    Feb 24, 2016 at 15:45
3

If you were going to do (let's say it was valid)

f(value1, arg3 = value2)

Then argument 2 would be undefined, so just pass that:

f(value1, undefined, value2)
3

The only way you would be able to do this with JS is to pass one array containing all of the parameters.

Your default values would have to be set within the function - you can't define default values for arguments in JavaScript.

function foo( args ){
  var arg1 = args[ 0 ] || "default_value";
  var arg2 = args[ 1 ] || 0;
  ///etc...
}

Even better, instead of an array you could pass a simple object which would allow you to access the arguments by their key in the object:

function foo( params ){
  var arg1 = params[ "arg1" ] || "default_value";
  var arg2 = params[ "arg2" ] || 0;
  ///etc...
}
0
2

Hey I had a similar problem but i don't know if it would apply in any context, and if it's the same for ES5, but in ES6 and within my context i was able to just pass undefined as the argument i want to skip.

What i mean by context, is that in my case I am assigning a default value to that argument in the function with ES6 format:

const exampleFunc = (arg1 = "defaultValue",arg2) => {
    console.log(arg1,arg2)
}

exampleFunc(undefined,"hello!"); 
//this would log to the console "defaultValue","hello!"

I'm not sure if this would work if you don't have a default value assigned in the function with ES6 format, but it worked for me! Hope this helped

2

With ECMAScript 6 (ECMAScript 2015) and the introduction of Default Parameters, it can be as easy as setting default values to the parameters and passing undefined to skip a parameter:

function paramsTest(p1 = "p1 def", p2 = "p2 def", p3 = "p3 def") {
  console.log([p1, p2, p3]);
}

paramsTest(); // [ "p1 def", "p2 def", "p3 def"]
paramsTest("p#1", "p#2"); // [ "p#1", "p#2", "p3 def"]
paramsTest("p#1", undefined, null); // [ "p#1", "p2 def", null]

0

Add this code to your function (for default values)

function f(a, b, c)
{
  a = typeof a !== 'undefined' ? a : 42;
  b = typeof b !== 'undefined' ? b : 'default_b';
  a = typeof c !== 'undefined' ? c : 43;
}

call the function like this

f(arg1 , undefined, arg3)
0

Another interesting approach to this would be to use the Function.prototype.call() method. All we have to do is assign the arguments with default call object values, and then you're free to call the method with only the arguments you want to use:

function f(arg1 = this.arg1, arg2 = this.arg2 || false, arg3 = this.arg3) {
    console.log([arg1, arg2, arg3]);
}

f.call({ arg1: 'value for arg1', arg3: 'value for arg3'});
f('value for arg1', undefined, 'value for arg3');

Notice that I set arg2 to this.arg2 || false - this syntax allows a fallback to false instead of undefined when no f.call instance object arg2 property exists.

0

in ES6, if your parameters are optional because they have default values and you are okay passing an object, you can use spread syntax:

function f(opts) {
  const args = {
    arg1: 'a',
    arg2: 'b',
    arg3: 'c',
    ...opts
  }
  const {arg1, arg2, arg3} = args
  return arg1.concat(arg2, arg3)
}
f() // "abc"
f({arg1: '1', arg3: '3'}) // "1b3"
f({arg2: ' whatever '}) //  "a whatever c"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.