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? |
|||
add comment |
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:
|
|||||||||||||||||||||
|
Via Crockford:
The main failing Crockford mentions is an inability to correctly determine arrays that were created in a different context, e.g., |
|||
|
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 |
|||||||||||||||||||||
|
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 |
|||||
|
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
|
|||
|
I was using this line of code:
|
|||||
|
If you're only dealing with EcmaScript 5 and above then you can use the built in e.g.,
|
|||
|
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
|
|||||
|
code referred from https://github.com/miksago/Evan.js/blob/master/src/evan.js
|
|||||||||
|
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).
|
|||||
|
In Crockford's JavaScript The Good Parts, there is a function to check if the given argument is an array:
He explains:
|
|||
|
For those who code-golf, an unreliable test with fewest characters:
This is commonly used when traversing/flattening a hierarchy:
|
|||
|
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. |
|||||||||
|
I liked the Brian answer:
but you could just do like this:
|
|||
|
|
|||||||||||||
|
If you are using Angular, you can use the angular.isArray() function
|
|||
|
A better solution, would be to test for methods you will need to be using. Using the in keyword. That way, if the you later define an array-like object you can still use the same function on it. For example, the only method this function needs to be implemented to treat the argument as an array is map:
This principle is called duck typing, as in "When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck." It is a widely used principle in many high level languages, notably Python. From their documentation: Python has a strong tradition of duck-typing, where explicit type-checking is never done and code simply calls methods on an object, trusting that those methods will be there and raising an exception if they aren’t. I haven't been able to do any specific tests, but simply checking whether an object has a specific property is surely faster than checking the type at runtime. |
|||
|
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