Is it possible to assign a value to a function parameter inside a function before calling it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to assign a value to a function parameter inside a function before calling it?

Javascript, functions parameters

22nd Aug 2019, 7:47 AM
MOSES SAMUEL KOSE
MOSES SAMUEL KOSE - avatar
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
22nd Aug 2019, 8:02 AM
Leon lit
Leon lit - avatar
+ 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
22nd Aug 2019, 7:56 AM
Anton Böhler
Anton Böhler - avatar
+ 3
Thanks leon i understand now
22nd Aug 2019, 8:05 AM
MOSES SAMUEL KOSE
MOSES SAMUEL KOSE - avatar