+ 1
These questions have confused me so long ....
I have a question in JS about default parameters Ex. function makeShoppingList(item1="milk",item2="bread'',item3='eggs") {, console.log(`Remember to buy $(item1)`); console.log(`Remember to buy $(item2)`); console.log(`Remember to buy $(item3)`); } makeShoppingList('milk'); makeShoppingList('breas'); makeShoppingList('eggs'); Output: Remember to buy milk Remember to buy bread Remember to buy eggs Remember to buy bread Remember to buy bread Remember to buy eggs Remember to buy eggs Remember to buy bread Remember to buy eggs I don't understand the output . Can you please explain because I have no one to teach me. Thank you !
6 Antworten
+ 1
The default parameter will applied when the parameter isnt provided at function call. In this case your item2 and item3 will use the default value you're given bread and eggs.
Because at the function call you only pass 1 parameter which only applid to item1
+ 1
Thanks
+ 1
It works like this
if(item1 === undefined)
item1="milk";
0
But I still don't understand....😖
0
It's so hard....
0
It means if you dont supply enough parameter
It'll supply the parameter by itself, using the given value