Calling function without passing arguments, runs without error. | Sololearn: Learn to code for FREE!

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

4/7/2019 7:02:08 AM

Raj Chhatrala

2 Answers

New Answer

+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

+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" ;)