JavaScript Arrays: Create, Access, Add & Remove Elements
We have learned that a variable can hold only one value. We cannot assign multiple values to a single variable. JavaScript array is a special type of variable, which can store multiple values using a special syntax.
The following declares an array with five numeric values.
let numArr = [10, 20, 30, 40, 50];In the above array, numArr is the name of an array variable. Multiple values are assigned to it by separating them using a comma inside square brackets as [10, 20, 30, 40, 50]. Thus, the numArr variable stores five numeric values. The numArr array is created using the literal syntax and it is the preferred way of creating arrays.
Another way of creating arrays is using the Array() constructor, as shown below.
let numArr = new Array(10, 20, 30, 40, 50);Every value is associated with a numeric index starting with 0. The following figure illustrates how an array stores values.