Last active
August 29, 2015 14:06
-
-
Save bjdixon/bc8f78068c608fa2ea42 to your computer and use it in GitHub Desktop.
Check if value is an array or not in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// typeof operator reports arrays as 'object' | |
// this function can be used to determine if the value is an array or not. | |
var is_array = function (value) { | |
return Object.prototype.toString.apply(value) === '[object Array]'; | |
}; | |
var ar1 = ['abc', 'def', 'hij']; // array | |
var ob1 = {'0': 'abc', '1': 'def', '2': 'hij'}; // object | |
var va1 = 'abcdefhij'; // string | |
console.log(is_array(ar1)); // true | |
console.log(is_array(ob1)); // false | |
console.log(is_array(va1)); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment