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

array ruse

july 18 , 2013
[,,,].join() // ==> ",,"

wtf?

@eliranmal

Turns out that the trailing comma is removed (trailing commas are allowed in javascript, but not JSON). Once removed, there are only three "elements" in the array, and both are undefined. When join is called, by default it uses a comma, yielding ",,". I think this is what happens

@DDTrejo

JSON has nothing to do with this issue. Although trailing comma is really allowed by Javascript and that's the case. So [1,2,3,] equals to [1,2,3]. So literally [,,,] is something like [undefined, undefined, undefined,]. You can add undefined to the end explicitly to get 4 elements array:

[,,,undefined].join() // ==> ',,,'

@ixti

Each beginning or ending comma is Elision (except comma after AssignmentExpression). Each Elision are evaluated as one array field. http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.4

— [@tomi77]


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


Fork me on GitHub