negative indexes

june 20 , 2013

Negative numbers mean different things to different functions on the Array prototype.


    var nums = [1, 2, 3];
    nums.splice(nums.indexOf('wtf'), 1);
    nums; // [1, 2]

@markdalgleish


isfinite null is true

apr 28 , 2013

isFinite function of JavaScript tests whether a number is finite.


    isFinite(42); // true
    isFinite(1/0); // false
    isFinite(0/0); // NaN is not finite -> false
    isFinite('42'); // true
    isFinite('hi'); // false

These are normal results.

    isFinite(); // false
    isFinite(undefined); // false

Undefined values are not finite. These are normal results too.

    isFinite(null); // true

Wait, what? Is null a number? It is converted into 0? Why?

Since null != 0 and null == undefined, (even thought null !== undefined) I expected null will behave something like undefined!

@123jimin


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


Fork me on GitHub