created by Brian LeRoux & Andrew Lunny. sparodically uncurated by David Trejo.

2013 03 27 automatic semicolon insertion

This is another case of an accidental global:

    var a = 1
        b = 1;

        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!


Fork me on GitHub