Skip to content

Instantly share code, notes, and snippets.

@stoive
Created May 15, 2012 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoive/2699727 to your computer and use it in GitHub Desktop.
Save stoive/2699727 to your computer and use it in GitHub Desktop.
Two types of undefined
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`?
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment