Default parameters in javascript

If there are two default parameters in a function how to override the second one only. For example:

function x(y = 2, z = 3) {
  console.log(y * z)
}

I want to call it with y = 2 and z = 4
how to do that

As far as I am aware it’s not possible to use default parameters in that way. A JS function will always take your first argument as the first argument, and so the only way to call that would be using x(2,4). You essentially have 4 options in this case.

// use both defaults
x()
// returns 6

// use the first default
x(4)
// returns 12

// using just the second default
x(2, 4)
// returns 8

// using no defaults
x(3,5)
//returns 15
1 Like

thanks, that really helped

1 Like