+ 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 !

16th Nov 2018, 7:33 AM
Rex Hung
6 ответов
+ 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
16th Nov 2018, 8:13 AM
Taste
Taste - avatar
+ 1
Thanks
16th Nov 2018, 8:20 AM
Rex Hung
+ 1
It works like this if(item1 === undefined) item1="milk";
16th Nov 2018, 8:26 AM
Taste
Taste - avatar
0
But I still don't understand....😖
16th Nov 2018, 8:23 AM
Rex Hung
0
It's so hard....
16th Nov 2018, 8:24 AM
Rex Hung
0
It means if you dont supply enough parameter It'll supply the parameter by itself, using the given value
16th Nov 2018, 1:02 PM
Taste
Taste - avatar