function isDataType(dataType, obj) {
return Object.prototype.toString.call(obj) == '[object' + dataType + ']';
}
var arr = [];
alert(isDataType("Array", arr)); // alerts 'false' which is false
When I make obj equal to an array and make the data type to evaluate as an array, it still says false. Is there a way to fix this? Thanks.
You are missing a space after
'[object. Your code should then evaluate to true.You should use instanceof to find out if an object is of a specific type, though.