Skip navigation

Before reading this blog entry, please have a quick look at my earlier blog entry null typeof shock.

In the earlier blog (null typeof shock), using typeof operator returns misleading value object.

To make it clear,

var myArr = Array();  // creating variable myArr and assigning a array into it
//var myArr = [];  // another way of doing it
alert(typeof(myArr));  // object, NOT array


So how do you make sure if a variable is given, it is an array?

Here comes instanceof operator to rescue.

alert(myArr instanceof Array) // true.

Well, don’t you feel strange to hear typeof and instanceof are operators!! Search and read about it then. In JavaScript you have lot of strange operators like this.

Advertisements

One Comment

  1. This will not work if you are dealing with an Array coming from a different window (frames or other windows). Look at jQuery’s isArray method, which is now considered state of the art:
    function( obj ) {
    return toString.call(obj) === “[object Array]”;
    }


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

%d bloggers like this: