Robust way to check if a variable is an array in Javascript
If you use plain javascript, this’ll be helpful
function isArray( obj ) {
return Object.prototype.toString.call( obj ) === '[object Array]';
}
If you use jQuery 1.3+, you can try this – $.isArray( obj ). More on this here: http://api.jquery.com/jQuery.isArray/
Still, you’re not convinced and want an explanation?
You can visit this page to read an explanation (probably the detailed one I’ve seen so far!) by Kangax
http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
About this entry
You’re currently reading “Robust way to check if a variable is an array in Javascript,” an entry on Singaram's Tech Musings
- Published:
- April 23, 2011 / 8:22 PM
- Category:
- Javascript
- Tags:
- JavaScript, JQuery

No comments yet
Jump to comment form | comment rss [?] | trackback uri [?]