I wrote the following code and got no output. Here is the code
var a1 = undefined;
var a2 = 5;
if(a1 > a2)
alert(1);
if(a1 < a2)
alert(2);
if(a1 >= a2)
alert(3);
if(a1 <= a2)
alert(4);
There was no alert box that came up which means that the if statements resulted in false. Can I know the reason?
JavaScript tries to casts the value of
xtonumberusing ToPrimitive (@RobG). Sincexis undefined, this will returnNaNwhich compares false to any value. So it will always returnfalse.