What is the output ? Can anyone explain the process? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the output ? Can anyone explain the process?

<script> function addition(a=1,b=2,c){ return a+b+c; } console.log(addition(3)); </script>

29th Apr 2019, 2:14 AM
KHUSHbu
KHUSHbu - avatar
4 Answers
+ 5
If no parameter is passed in place of a,b,c then the default params are a=1 b=2 c=undefined But in above function, the params passed are: a=3 b=2 c= undefined If no param is passed then the default value is either decided by user using '=' symbol. If not decided then the default value is 'undefined' And any number of is added to undefined returns 'NaN'. Hence output is NaN.
29th Apr 2019, 2:25 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 5
The value passed '3' is stored in variable/parameter 'a'. So, a=3 The value of 'b' which needs to be second argument, is not passed. So the default value is '2' So, b=2 The value of 'c' which needs to be third argument, is not passed. The default value of c is not set. So by system default, the value by default becomes 'undefined' So, c=undefined And finally, a+b+c =3+2+undefined =5+undefined =NaN So that function returns NaN. console.log(addition (3)) =console.log(NaN) So output is NaN. Please do not hesitate to ask me if you have any confusion. "I am always to help"
29th Apr 2019, 3:11 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 2
Ohk thank for clear my confusion😊
29th Apr 2019, 3:36 AM
KHUSHbu
KHUSHbu - avatar
+ 1
And what is the meaning of console.log(addition (3));why it pass the value of 3 ?
29th Apr 2019, 2:33 AM
KHUSHbu
KHUSHbu - avatar