Plusnet
Sunday 22nd November 2009 Login | Register

Another JavaScript Riddle

June 18th, 2007 at 13:26 by Tamlyn Rhodes

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.

Tamlyn

This entry was posted by Tamlyn Rhodes on Monday, June 18th, 2007 at 1:26 pm and is tagged with and is posted in the category Web Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


one comment on "Another JavaScript Riddle"

Colin

Now you see, this is why I don't like JavaScript :-)

Add a Comment




Photos

photo photo photo photo photo photo

View More

Forums

Users online: 60

  • Total Topics: 80032
  • Total Posts: 658587
  • Total Members: 11768

Visit the Forums

Plusnet

Force9

Metronet

Free-Online

Madasafish

PAYH

Just The Name

Related Sites

Community Apps

Here at Plusnet we're always trying to use clever open source things to make our lives easier. Sometimes we write our own and make other people's lives easier too!

View the Plusnet Open Source applications page

About Plusnet

We sell broadband, phone, VoIP and more to homes and businesses in the UK. Winner of 9 out of 11 Categories in the 2008 USwitch survey. Winner of "Best Consumer ISP" at 2008 ISPA awards. Voted number 1 in the Broadband Choices 2008 survey.

© Plusnet plc All Rights Reserved. E&OE

Community Site News is powered by WordPress

Add to Technorati Favourites