Working with Arrays

The Array object provides a means of storing and handling a set of other objects. Arrays can store numbers, strings, or other JavaScript objects. There are a couple of ways to create JavaScript arrays. For example, the following statements create three identical versions of the same array:

var arr = ["one", "two", "three"];var arr2 = new Array();arr2[0] = "one";arr2[1] = "two";arr2[2] = "three";var arr3 = new Array();arr3.push("one");arr3.push("two");arr3.push("three");

The first method defines arr and sets the contents in a single statement, using []. The second method creates the arr2 object and then adds items to it, using direct index assignment. The third method creates the arr3 object and ...

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.