automatic semicolon insertion

mar 27 , 2013

This is another case of an accidental global:


    var a = 1
        b = 1;

    (function(){
        var a = 2
            b = 2;
    }())

    console.log(a); // prints 1
    console.log(b); // prints 2

While debugging, one might be looking only at the “b” statements, but the problem is the missing comma after “a”. Since JavaScript has “automatic semicolon insertion” for some statements like “var”, a semicolon gets inserted after “a = 2”, and variable “b” gets declared as a global!

@jfparadis!


null to bool

mar 25 , 2013
    null == false // false

Okay, we know this. Null doesn't convert to boolean.

Now try this...

    !null // true

Well, this is awkward.

@noway421


false isnt false

mar 6 , 2013
    true == 'true'     // true
    false == 'false';  // false

This is expected behaviour as == doesn't do value equality, but rather it does numeric value equality1, thus 'false' is truethy, thus equals 1

@grnadav


null, undefined and test

feb 28 , 2013

Checking a variable for one word with one to ten letters lowercase only?

Try this regular expression: /^[a-z]{1,10}$/.test('wakaluba').

    /^[a-z]{1,10}$/.test(null);
    /^[a-z]{1,10}$/.test(undefined);

Both should obviously fail, but return true. srsly, WTF JS?

@damienklinnert


Fork me on GitHub