user5568265 user5568265 - 5 months ago 13
Javascript Question

(Why) does setting `array[NaN]` do nothing?

I was reading an article about javascript PRNGs, and I came across something that surprised me:


var a = new Array();
var b;
a[b++] = 1;


a
is now
[]
and no exception is thrown — the write to the array simply vanishes. Try it out in your browser console if you don’t believe me.


I didn't believe him, so I tried it out in my browser console (Firefox 47):

» var a = new Array();
» var b;
» a[b++] = 1

» a
← Array [ ]
» b
← NaN


There are several curious things going on here, but in particular, I'm trying to understand why the statement
a[b++] = 1
doesn't [appear to] do anything.

Answer

Taking it from the top:

var a = new Array();
// 'a' is now an empty array, plain ol' boring empty array, could have been 
// written as a = [];
var b;
// 'b' have no value and is 'undefined'. 'console.log(b); // undefined'
a[b++] = 1;
// Lets break the above statement down into pieces:
  b++       // Increment b by one, undefined + 1 === NaN
a[   ]      // Use NaN as a property for our array 'a'
       = 1; // Assign 1 to that property
// Ok? So what happened why does 'a' still look empty?
console.log(a); // []
// The way your console with show you an array is by printing the numeric keys
// in it, and looking at the length, eg:
// var q = [];
// q.length = 1;
// console.log(q); // [undefined x 1]

// With 'a' in our case there is no numeric keys in it so [] is printed.
// So did our value dissapear?
// No. It is there:
console.log('NaN' in a); // true
// And:
for (var prop in a) console.log(prop); // NaN

// Why is this even a feature? 
// Arrays are extending Objects so they have the same properties as em.
console.log(a instanceof Object); // true
console.log(a instanceof Array); // true