
JavaScript Arrays
Arrays are special variables that can hold multiple values. JavaScript array is an object that represents a collection of similar types of elements.
Creating an Array :
Using an array literal is the easiest way to create an array.
Syntax
const array_name = [item1, item2, item3, ...];
As you can see the above syntax values are contained inside the [ ], and separated by a comma (,).
An example is given below
const fruits = ["mango", "orange", "banana", "grapes" ];
Spaces and line breaks are not important, a declaration can span multiple lines.
const fruits = [ "mango", "orange", "grapes" ];
You can also create an empty array and then you can provide the elements of an array.
const fruits = []; fruits [0] = "mango"; fruits [1] = "orange"; fruits [2] = "grapes";
and also using the JavaScript keyword new we can create an array.
const fruits = new Array("mango", "orange", "grapes");
The above two examples do exactly the same so no need to use the new Array().
For simplicity, and readability use the array literal method.
Access an Array Elements
You can access the array elements by using the index number
const fruits = ["mango", "orange", "grapes"]; let fruits = [1]; //OrangeArray indexes start with 0, [0] is the first element, and [1] is the second element.
const fruits = ["mango", "orange", "grapes"]; let fruits[1] = "banana";