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

Community
  • 1
  • 1
Maizere Pathak.Nepal
  • 2,383
  • 3
  • 27
  • 41
  • Take a look into this other question http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript – acj Apr 26 '13 at 11:18

6 Answers6

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
}
VisioN
  • 143,310
  • 32
  • 282
  • 281
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
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76
  • 1
    Maybe the downvoter would be kind enough to explain why the -1, for the sake of learning :) – LeonardChallis Apr 26 '13 at 11:25
  • 1
    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](http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/) for more. – Asad Saeeduddin Apr 26 '13 at 11:30
  • It is a simple and clean solution which works in my scenario. Tested on latest version of Chrome and Firefox. Thanks – Aryan Firouzian Jan 14 '19 at 10:19
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
jAndy
  • 231,737
  • 57
  • 305
  • 359
1

Simple:

if( Object.prototype.toString.call( someVar ) === '[object Array]' ) {
    alert( 'Array!' );
}
basarat
  • 261,912
  • 58
  • 460
  • 511
1

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

if($.isArray(x)){
  alert("isArray");
}
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
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

s_qw23
  • 354
  • 2
  • 11