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 these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: