Declare an Array in JavaScript
Like other JavaScript variables, you do not have to declare arrays before you can use them. But rigorously declaring all your variables will make your scripts much easier to understand in six months.
This tutorial covers the declaration of one-dimensional JavaScript arrays; the next shows you how to declare multidimensional arrays. Yet another surveys referencing array elements. We'll then move on to associative arrays.
Three Ways to Declare an Array in JavaScript
You can declare JavaScript arrays: by using the array constructor, the literal notation, or by calling methods with array return types.
JavaScript Array Constructor
We can explicitly declare an array with the JavaScript "new" keyword to instantiate the array in memory (i.e. create it, and make it available).
// Declare an array (using the array constructor)
var arlene1 = new Array();
var arlene2 = new Array("First element", "Second", "Last");
We first declared an empty array. We then assigned three elements to the second array, while declaring it. "new Array()" is the array constructor.
Array Literal Notation
Below, we use an alternate method to declaring arrays:
// Declare an array (using literal notation)
var arlene1 = [];
var arlene2 = ["First element", "Second", "Last"];
We declared above the exact same arrays as previously; instead of new Array(…), you can use square brackets, optionally enclosing elements. Using square brackets (vs. array constructor) is called the "array literal notation."
Implicit Array Declaration
JavaScript also lets you create arrays indirectly, by calling specific methods:
// Create an array from a method's return value
var carter = "I-learn-JavaScript";
var arlene3 = carter.split("-");
We indirectly created an array by using the string split() method; several other JavaScript methods return an array as result. As illustration, the array returned by the split() method is identical to the one declared below.
var arlene4 = new Array("I", "learn", "JavaScript");
Assigning Size When Declaring Arrays
The same way we initialized array elements while declaring the array, JavaScript also lets you directly assign a size while declaring the array:
// Declare array of size 7
var arlene = new Array(7);
The script above sets the array size (or "length") during the declaration. This property will be explained in our array length tutorial.
Initialize new arrays as you declare them
To avoid script errors, you should get into the habit of initializing an array when you declare it: by default, JavaScript will assign the special value of NULL
to any variable you declare without initializing it with a value. In the case of array variables, you can either set the values of the array when you declare it (that assumes that you already know which values the array will contain - not always the case), or declare the array and initialize it as an empty array:
// Declare an empty array using literal notation:
var arlene = [];
// The variable now contains an array of length zero
Another way to declare an empty JavaScript array:
// Use the "new" constructor to declare an array:
var arlene = new Array();
// Array variable has been created (contains no element)
An "empty array" is just a regular array: the only difference is that it contains zero elements. To check if a JavaScript array is empty or not, simply check if its length property (the array size) is equal to zero or not.
Lifespan of JavaScript Arrays
Arrays you declare will remain available to the JavaScript interpreter for the life of the script, or for the scope of the array variable — whichever ends first.