Skip to content

Instantly share code, notes, and snippets.

@silvers
Last active December 14, 2015 15:09
Show Gist options
  • Save silvers/5105631 to your computer and use it in GitHub Desktop.
Save silvers/5105631 to your computer and use it in GitHub Desktop.
obj.length === +obj.length
var test = function(obj) {
console.log(obj);
console.log(obj.length);
console.log(+obj.length);
if (obj.length === +obj.length) {
console.log("array!!!");
} else {
console.log("object!!!");
}
};
test([1,2,3,4,5]);
test({a:1,b:2,c:3});
/* node length.js
[ 1, 2, 3, 4, 5 ]
5
5
array!!!
{ a: 1, b: 2, c: 3 }
undefined
NaN
object!!!
*/
@yuchuanxi
Copy link

obj maybe not a Object. maybe arguments. maybe var obj = {length: 5}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment