0

In Javascript I defined a function with one require parameter and some optional parameters. But when I want to call the function with only some optional parameter, then I am facing the problem.

function foo(par1, par2='default_value1', par3='default_value2', par4='default_value3'){
   //do the stuff
}

It works-

foo('some_value');
foo('some_value', par1='some_value');
foo('some_value', par1='some_value', par2='some_value');

But it's not works-

foo('some_value', par1='some_value', par4='some_value');

In this case, value of "par4" automatically assigned to "par3" during the execution of the function. I.e. value of par3 will be "some_value"

Samiddha
  • 31
  • 4
  • JavaScript doesn't have named arguments (you can use destructuring though), in your examples you're creating global variables `par1` and `par2` and assigning them to values, it doesn't control which parameter the argument is associated with when the function executes. – Nick Parsons Oct 03 '21 at 12:29

0 Answers0