Created
May 15, 2012 07:21
-
-
Save stoive/2699727 to your computer and use it in GitHub Desktop.
Two types of undefined
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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