An array is a data structure, which is used to store multiple values inside the same container. Though the main idea behind arrays in JavaScript is quite similar to the arrays in other languages, yet they exhibit different behavior and follow different syntax in JavaScript.
Unlike other programming languages, arrays containing the value of any datatype are declared using the var keyword. By now, you should know that be it any container (variable or array) of any datatype, JavaScript declares them all using the var keyword.
Let's move on to the syntax of arrays in JavaScript.
Syntax for declaration & initialization:
var arrayName; arrayName = [value1, value2, value3, ..., valueN];
The above syntax will create an array with the name arrayName and will fill in some values inside it. Each element in an array is stored at a unique index, which is used to identify the location of an element inside the array.
The index in an array in JavaScript starts from 0. Thus, if there are n elements in an array, the last element in the array will be stored at index n-1.
Syntax for accessing an element in an array:
arrayName[i];
The above syntax access the element's value present inside the array named arrayName at the index i.
Let’s see the implementation of arrays in JavaScript, and understand them in more detail.