Javascript WTF
by BenV on Nov.29, 2009, under Fun
Last week at work we ran into this gem (click on the image to get it full screen):
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.