Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

6. Arrays > Array Type

Array Type

We’ve seen throughout this chapter that arrays are objects with some special behavior. Given an unknown object, it is often useful to be able to determine whether it is an array or not. In ECMAScript 5, you can do this with the Array.isArray() function:

Array.isArray([])     // => true
Array.isArray({})     // => false

We can write an isArray() function that works in any version of JavaScript like this:

var isArray = Array.isArray || function(o) {
    var ts = Object.prototype.toString;
    return typeof o === "object" &&
 	   ts.call(o) === "[object Array]";
};

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint