0

I have a function that takes multiple parameters where I give each parameter a default value. When I call this function I only want to change a specific parameter. Is this possible to do in JavaScript? I was unable to find a clear answer in a Google search.

I understand that I can use several 'if' statements within my function however I am able to solve this problem in Python through the example:

setValues(val2="new")

This does not work in JavaScript being that the assignment occurs to the first positional variable in the function rather than the one explicitly chosen.

I have the code:

function setValues(val1="a", val2="b", val3="c")
{
  /*
    Print the value of each variable. 
  */
}
  
setValues(val2="new");

Output:

val1 = "new"
val2 = "b"
val2 = "c"

Expected:

val1 = "a"
val2 = "new"
val3 - "c"

0 Answers0