public
Created

Two types of undefined

  • Download Gist
undefined.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
new Array(10).length
// evaluates to `10`
 
new Array(10).map(function(current, i) { return i });
// evaluates to `[]`
 
var arr = [];
arr[2] = undefined;
arr[9] = undefined;
 
console.log(arr);
// logs `[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
 
console.log(arr.map(function(current, i) { return i }));
// logs `[undefined, undefined, 2, undefined, undefined, undefined, undefined, undefined, undefined, 9]`
 
arr.map(function(current, i) { return i }).forEach(function(current, i) { console.log(i) });
// logs `2`
// logs `9`
 
for (var i in arr) console.log(i);
// logs `2`
// logs `9`
 
for (var i = 0; i < arr.length; ++i) console.log(arr[i]);
// logs 10*`undefined`
 
/*
* Any guesses on how to tell the difference between `undefined` and, err, `undefined`?
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.