+ 9

Calling function without passing arguments, runs without error.

Hello People. I'm just curious about a simple function in Javascript which looks something like this : function x(a, b, c){ a = true; b = true; c = a || b ? "x" : "y"; console.log(c); } It accepts three parameters and when I call it I don't pass any parameters to it like : x(); It's still running usually and giving me output "x". Want to know how things work in here.

7th Apr 2019, 7:02 AM
Raj Chhatrala
Raj Chhatrala - avatar
2 Answers
+ 4
If a function is called with missing arguments (less than declared), the missing values are set to: undefined reference- https://www.w3schools.com/js/js_function_parameters.asp
7th Apr 2019, 11:19 AM
ODLNT
ODLNT - avatar
+ 2
If you don't pass any parameters, the default value will be undefined, however, in your function the param a (which is undefined) is set to be equal the Boolean"true", so your param a is now equal to true... The same happens with the param b. That's why the output is "x" ;)
7th Apr 2019, 3:29 PM
Daniel Yuki
Daniel Yuki - avatar