+ 1
Is it possible to assign a value to a function parameter inside a function before calling it?
Javascript, functions parameters
3 Answers
+ 5
If you're talking about default function parameter you could do something like this
function multiply(a, b = 1) {
return a * b;
}
when you call the function like this,
console.log(multiply(6, 5));
It'll output 30
But if you call the function without specifying the b parameter with value, JS will use the default value that you have set for the function, in our case 1.so
console.log(multiply(5));
Would print out 5
It'll use back the default parameter value that you've set for the multiply function
Hope this help
+ 3
yes, it is used to have a default parameter.
in 99% of cases your parameter(s) which is(/are) optional (/has an default) should be at the end of the paramterlist
+ 3
Thanks leon i understand now