Skip to content

Instantly share code, notes, and snippets.

@bjdixon
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjdixon/bc8f78068c608fa2ea42 to your computer and use it in GitHub Desktop.
Save bjdixon/bc8f78068c608fa2ea42 to your computer and use it in GitHub Desktop.
Check if value is an array or not in javascript
// 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