0
Output is 40, but why?
function x(a,b=5,c=6,d=10) { return a+b-d; } document.write(x(30,20)); This is a question in one of the SoloLearn Challenges. I don't understand why output is 40. Can anyone explain it kindly?
3 Answers
+ 3
GĂŒlefĆan
Explaination:
function x(a, b = 5, c = 6, d = 10) {
return a + b - d;
}
In the above function b, c and d has default value means if you will not pass the parameter from the outside then default value will be considered as original value.
So according to this function x(20, 30) we have the value of a 20 and b 30 but we didn't pass the parameter c and d so default value will be considered as original value of c and d
So here a = 20, b = 30, c = 6, d = 10
Note :-
//function x has default value of b 6 but we are passing parameter b from outside so b will be 30
Finally a + b - d = 20 + 30 - 10 = 40
+ 1
document.write(x(30,20)):
a = 30
b = 20
return a + b - d
a + b = 50 - 10
result = 40
0
Thank you guysâïž