Creating an Array

Using an array literal is the easiest way to create a JavaScript Array. The syntax looks like this:

  var array-name = [item1, item2, ...];

An example of an actual array looks like this:

  var cars = ["Saab", "Volvo", "BMW"];


Using the JavaScript Keyword new

The following example also creates an array, and assigns values to it:

  var cars = new Array("Saab", "Volvo", "BMW");


The two examples above do exactly the same. There is no need to use new Array(). For simplicity, readability and execution speed, use the first one (the array literal method).

Back Table of Contents Next