0

I have try three way in the bellow, the result is comment on the right,the diffcult is I can't different it from Object datatype.How can I get it datatype like Array or String but not Object?

   var arr = [];
   console.log('type of arr', typeof arr); //objct
   console.log('instanceof array', arr instanceof Array); //true
   console.log('instanceof object', arr instanceof Object);  // true
hh54188
  • 14,887
  • 32
  • 113
  • 184
  • By the way, [this has already been asked](http://stackoverflow.com/questions/4775722/javascript-check-if-object-is-array). – voithos Jun 13 '12 at 05:57

5 Answers5

3

Here is one technique:

> var arr = [];
> Object.prototype.toString.call(arr);
"[object Array]"

What this does is call the toString method of the prototype object, using whatever is passed in as the this pointer. For more information on this technique, see the reference on call.

It turns out that you can use this technique to figure out other object types as well:

> var func = function(){}
> Object.prototype.toString.call(func);
"[object Function]"

> var obj = {};
> Object.prototype.toString.call(obj);
"[object Object]"
voithos
  • 68,482
  • 12
  • 101
  • 116
3

you can use jQuery's "isArray" function for this

var arr1 =[];
alert(jQuery.isArray(arr1));  // true

var arr2 = new Array();
alert(jQuery.isArray(arr2));  // true

var obj = new Object();
alert(jQuery.isArray(obj));   // false
some_coder
  • 340
  • 7
  • 15
1

I got this info at MDN - In Javascript Version 1.8.5 Array.isArray is added and it is standard in ECMAScript 5

// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray( new Array() );
Array.isArray( Array.prototype ); // Little known fact: Array.prototype itself is an array.

Also if isArray is not available

if(!Array.isArray) {
  Array.isArray = function (vArg) {
    return Object.prototype.toString.call(vArg) === "[object Array]";
  };
}

For more details at MDN

aked
  • 5,625
  • 2
  • 28
  • 33
0

This is very simple, your question is your answer indeed,

var arr = [];
if('instanceof object', arr instanceof Object){
    alert('arr is a object');
    if('instanceof array', arr instanceof Array){
        alert('arr is a Array');
    }
}​else{
    alert('this is not a object');
}

Now let's use a simple variable testObj, This is not even a object then how it could be a array,

    var testObj;
    if('instanceof object', testObj instanceof Object){
      alert('testObj is a object');
    }else{
      alert('testObj is not a object');
    }
​ 

Try this for more

Umesh Aawte
  • 4,590
  • 7
  • 41
  • 51
0

Array always will be instance of object - Object is the base object in javascript and any another object created by new is inherited from.

 new String('a') instanceof Object // true - also instance of String
 new Number(3) instanceof Object // true -also instance of Number etc.
 new Boolean(true) instanceof Object // true
 new Date instanceof Object // true
 new function(){} instanceof Object // true

 [] instanceof Object // true - [] is equal to new Array

 check this out: 
 Array = 1;
 [] //TypeError: Cannot read property 'slice' of undefined
 :)

however

'a' instanceof Object // false
 3 instanceof Object // false   

Try this:

 var str = 'aaa',
     arr  = [],
     myClass = function(){},
     myClassInstance = new myClass;

 str.constructor == String // true
 arr.constructor == Array // true
 myClassInstance.constructor == myClass // true
abuduba
  • 4,986
  • 7
  • 26
  • 43
  • (new Date(3) instanceof Object) returns true. While you may be able to create new Date(3) and get a valid date, 3 is not a date, it's a number. Within the confines of array detection, this method may work, but the expanded answer described is misleading for other usages. – chris.nesbit1 May 15 '15 at 13:34