2. Declaring and Initializing Array
There are a few ways
to create arrays in JavaScript, but
the most common is to use an array
literal, which is a single declaration that declares a variable and
specifies the values of the array as its content.
Use the basic syntax
for creating variables, to create an array using a literal array with one addition: the values in the array are separated by
commas, and the entire array value set is enclosed in square brackets.
Here is the syntax to create an array using the literal array:

where name is the name of the
variable allocated to the array, and the values in the array are value1,
value2, and value3.
The var statement is often very long when using an array literal to declare a large array. While you want to terminate every line of JavaScript code with a semicolon in most cases, breaking a literal array over several lines is acceptable. For example, you might write a literal array for a long array as follows:
An element is each value found in the array. Each element has a number, referred to as an index, that represents the position of the element within the array. While numbering usually begins with 1 in everyday life, numbering also starts with 0 in programming. This is the case for arrays.
By enclosing your index number in
brackets after the array name, means you refer to a particular array element.
As in the example below, chemElements [0] is the first element in the
chemElements array, chemElements [1] is the second element, chemElements [2] is
the third element, and so on. This also means that you would refer to the 10th
element in the array using index 9 if you have an array consisting of 10
elements.
Example:
<script>
//creating array with literal method
var chemElements = ["Argon", "Krypton", "Neon", "Xenon"];
//accessing the values of array
document.write(chemElements[0]+"</br>");
document.write(chemElements[1]+"</br>");
document.write(chemElements[2]+"</br>");
document.write(chemElements[3]+"</br>");
</script>
Output:

The var name = [value1, value2, value3,...]; syntax allows the array to be generated and the elements that it contains to be defined. Elements may be added to an existing array as well. To do this, you use a syntax similar to that used to assign a value to a standard variable, except that, after the name of the array, you also have an index for the new element in square brackets.
For example, to add the value
“Mercury” as a fifth element of the chemElements array, you would use the
following statement:

Because the existing four elements
have the index number 0, 1, 2, and 3, this statement uses the next number, 4,
and assigns it the string value "Mercury". The example below shows the updated
code with the above statement to add the fifth element.
<script>
//creating
array with literal method
var chemElements = ["Argon",
"Krypton", "Neon", "Xenon"];
chemElements [4] = "Mercury";
//accessing the values of
array
document.write(chemElements[0]+"</br>");
document.write(chemElements[1]+"</br>");
document.write(chemElements[2]+"</br>");
document.write(chemElements[3]+"</br>");
document.write(chemElements[4]+"</br>");
</script>
You may create an array that does
not contain any elements, and later add elements to it. By using the following
syntax, you create a new empty array: In this expression, the square brackets imply that the new variable is an array. You can then add elements to the array just as you would to an array that contains elements already. Dynamically, the size of an array can change. If you allocate a value to an element that has not yet been created, the element and any elements that may precede it are automatically created. The first statement in the following code, for example, creates an array of chemElements without any elements. The second statement that assigns the third element to "Neon," which also creates the first two elements in the process (chemElements[0] and chemElements[1]).


Most programming languages require all elements in an array to be exactly the same data type. In JavaScript, however, the values assigned to the elements of the array can be of different data types.