var isOnlyArray = function(o) {
if (! (Object.prototype.toString.call(o) === "[object Array]")) {
return false;
}
for (property in o) {
if (o.hasOwnProperty(property)) {
var asInt = parseInt(property, 10);
if (!(0 <= asInt && asInt < o.length)
|| String(asInt) !== property) {
return false;
}
}
}
return true;
}
This function confirms that it's an Array and that every defined property on an object is an integer index that's in the range specified by .length.
var a = [1, 2];
console.log(isOnlyArray(a)); // true
a[2] = 4;
console.log(isOnlyArray(a)); // true
a["foo"] = 5;
console.log(isOnlyArray(a)); // false
JSON.stringify(var1)gives you array notation, despite and without the extra property, really.