I want to know the type of the variable put in the function. So, I used typeof
and like this:
randomFunctionName: function(obj){
switch(typeof obj){
case "object":
//Something
case "text":
//Something else
}
}
But the problem is, I can't tell if obj
is an array or an object, since
typeof [] === "object" //true
typeof {} === "object" //true
So, how I can I separate them? Is there any difference between them?
An array is an object. You can test if an object is an array as follows:
Object.prototype.toString.apply(value) === '[object Array]';
You can wrap this up into a function as follows:
function isArray(a)
{
return Object.prototype.toString.apply(a) === '[object Array]';
}