By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

Best way to check if variable is array?

Mike Kypriotis
P: 37
I have read lots and lots of possible solutions all with its drawbacks, e.g. if it from a different frame or if it is a custom object including 'array' in its constructor, etc. but all more than 3 months old. The lattest mentioned Array.isArray([]) that comes with ECMAScript 5th Edition but do not work with IE7 and lower. Wanna know if there is a sure-proof solution for all?

P.S. For my particular application I take some arrays from JSON and want to check that they are arrays and then check teir length property to see if they have data inside
Nov 18 '11 #1

✓ answered by Dormilich

instanceof. toString() is easier to overwrite.

Share this Question
Share on Google+
13 Replies


Dormilich
Expert Mod 5K+
P: 8,639
since JSON supports only the Object and Array objects, one of the old constructor checks is sufficient
Nov 18 '11 #2

Mike Kypriotis
P: 37
found this page
http://perfectionkills.com/instanceo...obust-isarray/
which concludes using
Expand|Select|Wrap|Line Numbers
  1. function isArray(o) {
  2.   return Object.prototype.toString.call(o) === '[object Array]';
  3. }
  4.  
which is my best find so far, question is though that does not say which edition of ECMA-262 he took the definition of Object.prototype.toString (hoping its also working for 3rd edition cause I have tested the function for only the lattest browsers -5th ed)
Nov 18 '11 #3

Dormilich
Expert Mod 5K+
P: 8,639
toString() is a very, very basic method. that’s definitely not a recent addition
Nov 19 '11 #4

Mike Kypriotis
P: 37
for a non framed enviroment (just taking some data from JSON) would u recommend this or instanceof? (have read that though the above is more complete -support multi-framed is slower and more complicated )
Nov 20 '11 #5

Dormilich
Expert Mod 5K+
P: 8,639
framed environment?

I’d use instanceof, the question is which version of IE supports that.
Nov 20 '11 #6

Mike Kypriotis
P: 37
with "framed enviroment" meant working with multiple frames/windows and have to transfer an array from on to an other. From Javascript Bible found:
InstanceOf Compatibility: WinIE5+, MacIE-, NN6+, Moz+, Safari+, Opera+, Chrome+
Nov 21 '11 #7

Dormilich
Expert Mod 5K+
P: 8,639
I don’t think it has an influence on testing for an array.
Nov 21 '11 #8

Mike Kypriotis
P: 37
not sure what u mean, tested this code
Expand|Select|Wrap|Line Numbers
  1. a = new Array(1,2,3);
  2. a instanceof Array; //true
  3. a instanceof Object; //also true but 
  4. //I am interesting in testing for arrays
  5.  
which is what i want but read that arrays are also of type object so tested
Expand|Select|Wrap|Line Numbers
  1. function userobject(str1){this.name=str1;}
  2. a=new userobject("nikos");
  3. alert(a instanceof Array);//false
  4.  
which means that it distinguish object from array, so I am 100% sure of this working right?

P.S. On the other hand if want to test for both arrays and object determine which is which I will have to go with the prev solution right?
Nov 21 '11 #9

Dormilich
Expert Mod 5K+
P: 8,639
any non-primitive (undefined, null & the literals) inherits from Object because Object is the base of every object (including Array) in JavaScript.
Nov 21 '11 #10

Mike Kypriotis
P: 37
So you mean that checking for undefined, null & literals with the InstanceOf Object will return true. But in case of checking with InstanceOf Array all is OK right?
Nov 21 '11 #11

Dormilich
Expert Mod 5K+
P: 8,639
those will return false, as they are not objects.
Nov 21 '11 #12

Mike Kypriotis
P: 37
concluding for the job of checking some arrays in source code (not frames) and/or from JSON u would use instanceOf or Object.prototype.toString.call(o) === '[object Array]' ?
Nov 21 '11 #13

Dormilich
Expert Mod 5K+
P: 8,639
instanceof. toString() is easier to overwrite.
Nov 22 '11 #14

Post your reply

Sign in to post your reply or Sign up for a free account.