Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 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>
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.
+ 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"
+ 2
Ohk thank for clear my confusion😊
+ 1
And what is the meaning of console.log(addition (3));why it pass the value of 3 ?