I would like to check whether a variable is either an array or a single value in JavaScript.
I have found a possible solution...
if (variable.constructor == Array)...
Is this the best way this can be done?
I would like to check whether a variable is either an array or a single value in JavaScript. I have found a possible solution...
Is this the best way this can be done? |
|||
There are several ways of checking if an variable is an array or not. The best solution is the one you have chosen.
This is the fastest method on Chrome, and most likely all other browsers. All arrays are objects, so checking the constructor property is a fast process for javascript engines. If you are having issues with finding out if an objects property is an array, you must first check if the property is there.
Some other ways are:
This method runs about a 1/3rd the speed as the first example. Still pretty solid, looks cleaner, if you're all about pretty code and not so much on performance.
This last one is, in my opinion the ugliest, and it is the slowest. Running about 1/5th the speed as the first example. Array.prototype, is actually an array. you can read more about it here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray Also, I ran some test upon jsperf.com http://jsperf.com/instanceof-array-vs-array-isarray so have some fun and check it out. |
|||||||||||||||||
|
You could also use:
This seems to me a pretty elegant solution, but to each his own. |
|||||||||||||||||||||
|
I noticed someone mentioned jQuery, but I didn't know there was an jQuery implements it as Peter suggests:
Having put a lot of faith in jQuery already (especially their techniques for cross-browser compatibility) I will either upgrade to version 1.3 and use their function (providing that upgrading doesn’t cause too many problems) or use this suggested method directly in my code. Many thanks for the suggestions. |
|||||||||
|
There are multiple solutions with all their on quirks. This page gives a good overview. One possible solution is:
|
|||||||||||||||||||||
|
This is an old question but having the same problem i found a very elegant solution that i want to share. Adding a prototype to Array makes it very simple
Now once if you have an object you want to test to see if its an array all you need is to check for the new property
isArray is only available if its an array |
|||||||||||||||||||||
|
Via Crockford:
The main failing Crockford mentions is an inability to correctly determine arrays that were created in a different context, e.g., |
|||||
|
I personally like Peter's suggestion: http://stackoverflow.com/a/767499/414784 (for ECMAScript 3. For ECMAScript 5, use Comments on the post indicate, however, that if
Note that in JavaScript The Definitive Guide 6th edition, 7.10, it says |
|||||||||
|
In modern browsers you can do
(Supported by Chrome 5, Firefox 4.0, IE 9, Opera 10.5 and Safari 5) For backward compatibility you can add the following
If you use jQuery you can use If you don't need to detect arrays created in different frames you can also just use
Note: the
|
|||||||||||||
|
When I posted this question the version of JQuery that I was using didn't include an Since JQuery now does offer this function, I would always use it...
(as of version 1.6.2) It is still implemented using comparisons on strings in the form
|
|||
|
If you're only dealing with EcmaScript 5 and above then you can use the built in e.g.,
|
|||
|
In Crockford's JavaScript The Good Parts, there is a function to check if the given argument is an array:
He explains:
|
|||
|
I was using this line of code:
|
|||||
|
If you are using Angular, you can use the angular.isArray() function
|
|||||
|
Thought I would add another option for those who might already be using the Underscore.js library in their script. Underscore.js has an isArray() function (see http://documentcloud.github.com/underscore/#isArray).
|
|||||
|
code referred from https://github.com/miksago/Evan.js/blob/master/src/evan.js
|
|||||||||
|
For those who code-golf, an unreliable test with fewest characters:
This is commonly used when traversing/flattening a hierarchy:
|
|||
|
I liked the Brian answer:
but you could just do like this:
|
|||
|
|
|||||||||||||
|
I have created this little bit of code, which can return true types. I am not sure about performance yet, but it's an attempt to properly identify the typeof. https://github.com/valtido/better-typeOf also blogged a little about it here http://www.jqui.net/jquery/better-typeof-than-the-javascript-native-typeof/ it works, similar to the current typeof.
It think it may need a bit of fine tuning, and take into account things, I have not come across or test it properly. so further improvements are welcomed, whether it's performance wise, or incorrectly re-porting of typeOf. |
|||
|
I think using myObj.constructor==Object and myArray.constructor==Array is the best way. Its almost 20x faster than using toString(). If you extend objects with your own constructors and want those creations to be considered "objects" as well than this doesn't work, but otherwise its way faster. typeof is just as fast as the constructor method but typeof []=='object' returns true which will often be undesirable. http://jsperf.com/constructor-vs-tostring one thing to note is that null.constructor will throw an error so if you might be checking for null values you will have to first do if(testThing!==null){} |
||||
|
The universal solution is below:
Starting from ECMAScript 5, a formal solution is :
Also, for old JavaScript libs, you can find below solution although it's not accurate enough:
The solutions are from http://www.pixelstech.net/topic/85-How-to-check-whether-an-object-is-an-array-or-not-in-JavaScript |
|||
|
Since the .length property is special for arrays in javascript you can simply say
Underscorejs and several other libraries use this short and simple trick. |
|||||||||
|
Something I just came up with:
|
|||||||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?
.toString()
should be avoided as much as possible, so if you're not dealing with multiple frames you should useinstanceof
. – rolandjitsu Aug 17 '13 at 15:19