To just compare types, one would think typeof
would be the right tool
typeof [] === typeof ['1','2','3']; // true, both are arrays
Note that null
, arrays etc. are of type 'object'
, which means
typeof [] === typeof null; // is true (both are objects)
typeof [] === typeof {}; // is true (both are objects)
which is expected behaviour.
If you have to specifically check for null, arrays or other things, you could just write a better typeof
function
var toType = function(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
Then you could do
toType([]) === toType(['1','2','3']); // true
toType([]) === toType({}); // false
toType(1) === toType(9999); // true
toType(null) === toType(null); // true
toType(null) === toType([]); // false