cancel
Showing results for 
Search instead for 
Did you mean: 

Another JavaScript Riddle

Another JavaScript Riddle

Another JavaScript Riddle

Inspired by meebo's JavaScript Riddle blog post, here is another bit of weirdness I just came across. I've initialised the variable foo to some value and executing these statements gives these results: >>> foo == false false >>> !foo true The first line implies that since foo isn't false, then it must be true (every value in JavaScript is either true or false). But if not foo is true, then foo must be false! However the really weird bit is: >>> foo == foo false What?! foo isn't equal to itself? At this point you'll either know the answer or you'll be throwing your hands up in the air and cursing JavaScript for not making any sense. For those of you in the latter category, the answer is that foo is the special value NaN which stands for "Not a Number". Attempting to equate NaN to anything, even itself, always returns false! If you still think that JavaScript is nuts, go and read these articles by Douglas Crockford. It's actually quite a cool language.

0 Thanks
1 Comment
122 Views
1 Comment
Community Veteran
Now you see, this is why I don't like JavaScript :-)