Hi I have to admit that I am still grasping many ES6 syntax even though I have used a fair amount of them.
For example, I understand that you can do console.log(multiply(5))
to get the result of a given function of
function multiply(a, b = 1) {
return a * b;
}
But let say you have
function multiply(a, b = 1, c) {
return a * b * c;
}
Obviously you can't do (console.log(multiply(5,,5))
. In this case, is rearranging the arguments position in the function to become function multiply(a, c, b = 1)
the only possible way? Or is there any other smarter way?
You can pass undefined
to use default values:
function multiply(a, b = 1, c) {
return a * b * c;
}
multiply(2, undefined, 3); // 6
You can read about default parameter values and see more examples at MDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With