Javascript what R U doin? Javascript, stahp!

Published on 11 février 2014

I like javascript…

I know that can sound a bit weird, but I really do like JS. I mean it’s got all these tools, frameworks, which allows you to achieve complex things in little time. Moreover it’s very accessible and easy to learn.

I was playing around with a page’s DOM when I noticed something weird, just see for yourself:

Lets say the htlm is (doesn’t really matter):


And say I’m using a jQuery selector that doesn’t return anything such as $(" div td ") for instance and then print out the length of the result. Here is what happen:

var test;
var test1;
var test2;
test.length //Cannot read property &lsquo;length&rsquo; of undefined, quite normal
test1 =  $("div td"); //length : 0
test2 += $("div td");//length : 24!!! Because  test2 : undefined[object Object]


It seems like using a shorthand assignement operator on a an not yet assigned variable is not a good idea…still the outcome is quite unexpected.

For more JS " fun " :

comments powered by Disqus