BenV's notes

Javascript WTF

by on Nov.29, 2009, under Fun

Last week at work we ran into this gem (click on the image to get it full screen):

Javascript wtf in firebug

Javascript wtf in firebug

For reasons unknown to us, this piece of code acts very strange. First it evaluates selected == index which it considered to be false. (outlined in green in the image) So far so good. However, what it does next is a real WTF.
Can someone explain to us why this code runs INTO the if statement?
The if (false) { whatever } part?
Personally I’d expect the code to jump over the if block, since that’s what if statement are for. Skipping code unless the evaluation is true.
Or explained a lot better on wikipedia. However, what happens is that whatever is actually executed.

So after carefully inspecting this piece of code and saying obscene stuff, minds boggled, trying to find that hidden semicolon or whatever, we couldn’t find it. Maybe a browser bug? But both Opera (version 10.10) and Firefox (version 3.5.5) failed in the same way.
Maybe some magic with jQuery? Whatever it is, it remains a mystery to us.




:, , , ,

Leave a Reply

You must be logged in to post a comment.

Archives

  • 2018 (1)
  • 2016 (1)
  • 2015 (7)
  • 2014 (4)
  • 2013 (11)
  • 2012 (27)
  • 2011 (26)
  • 2010 (25)
  • 2009 (68)