468,770 Members | 2,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,770 developers. It's quick & easy.

Best way to check if variable is array?

Mike Kypriotis
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.

13 3654
8,651 Expert Mod 8TB
since JSON supports only the Object and Array objects, one of the old constructor checks is sufficient
Nov 18 '11 #2
found this page
which concludes using
Expand|Select|Wrap|Line Numbers
  1. function isArray(o) {
  2.   return Object.prototype.toString.call(o) === '[object Array]';
  3. }
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
8,651 Expert Mod 8TB
toString() is a very, very basic method. that’s definitely not a recent addition
Nov 19 '11 #4
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
8,651 Expert Mod 8TB
framed environment?

I’d use instanceof, the question is which version of IE supports that.
Nov 20 '11 #6
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
8,651 Expert Mod 8TB
I don’t think it has an influence on testing for an array.
Nov 21 '11 #8
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
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
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
8,651 Expert Mod 8TB
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
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
8,651 Expert Mod 8TB
those will return false, as they are not objects.
Nov 21 '11 #12
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
8,651 Expert Mod 8TB
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.

Similar topics

3 posts views Thread by Neil Jarman | last post: by
9 posts views Thread by portroe | last post: by
3 posts views Thread by laredotornado | last post: by
5 posts views Thread by farrell.niall | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.