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
1

5 Answers 5

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]"
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

1 Comment

(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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.