28

How to know whether an object is array or not?

 var x=[];

console.log(typeof x);//output:"object"
alert(x);//output:[object Object]
console.log(x.valueOf())//output:<blank>? what is the reason here?
console.log([].toString()); also outputs <blank>     
Object.prototype.toString.call(x) output:[object Array] how?

since console.log([].toString()); outputs :blank

1st:

why i get blank at 2nd last statement?

2nd:

Is there a way to know exactly what an object is: Array or plain Object({}) without the help of their respective methods like x.join() indicates x is an Array,not in this way.

Actually,in jquery selection like $("p") returns jquery object so if i use

console.log(typeof $("p"));//output:"object

I just wanted to know the actual Name of the Object.Thats it.Thank u for u help

1

6 Answers 6

13

In pure JavaScript you can use the following cross browser approach:

if (Object.prototype.toString.call(x) === "[object Array]") {
    // is plain array
}

jQuery has special method for that:

if ($.isArray(x)) {
    // is plain array
}
Sign up to request clarification or add additional context in comments.

7 Comments

console.log([].valueOf())//output:<blank>? what is the reason here?
@Maizere Basically [].valueOf() returns [], and doesn't make any sense here.
@Maizere Glad it helped. FYI, jQuery has also $.isPlainObject() for objects check.
Object.prototype.toString.call(x) output:[object Array] how? since console.log([].toString()); outputs :blank
@Maizere Because Object.prototype.toString is not the same as Array.prototype.toString. The latter simply stringifies array, e.g. [1,2,3].toString() === "1,2,3".
|
4

You can use instanceof. Here's some FireBug testing:

test1 = new Object();
test2 = new Array();
test3 = 123;

console.log(test1 instanceof Array); //false
console.log(test2 instanceof Array); //true
console.log(test3 instanceof Array); //false

3 Comments

Maybe the downvoter would be kind enough to explain why the -1, for the sake of learning :)
Wasn't me (frankly I don't think this is wrong enough to deserve a downvote), but this fails in multi-DOM environments such as iframes and the like. See this article for more.
It is a simple and clean solution which works in my scenario. Tested on latest version of Chrome and Firefox. Thanks
2

Best practice is the invocation of Object.prototype.toString() on the target object, which displays the internal [[Class]] property name.

Object.prototype.toString.call( x ); // [object Array]

This has the adventage, that it works on any and every object, regardless of if you're working in a multi frame / window environment, which causes problems on using x instanceof Array.


Newer ES5 implementations, also give you the method Arrays.isArray(), which returns true or false.

Array.isArray( x ); // true

And last but not least, jQuery has its very own .isArray() method, which also returns a boolean

jQuery.isArray( x ); // true

Comments

1

Simple:

if( Object.prototype.toString.call( someVar ) === '[object Array]' ) {
    alert( 'Array!' );
}

Comments

1

http://api.jquery.com/jQuery.isArray/

if($.isArray(x)){
  alert("isArray");
}

Comments

0

I think you are searching for something like this:

if( Object.prototype.toString.call( someVar ) === '[object Array]' ) {
    alert( 'Array!' );
}

Hope this helps. A little to slow :P

1 Comment

Yes, this has already been posted two or three times.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.