In JavaScript, an array is an ordered list of values. Each value, known as an element, is assigned a numeric position in the array called its index. The indexing starts at 0, so the first element is at position 0, the second at position 1, and so on.
Arrays can hold any type of data-such as numbers, strings, objects, or even other arrays—making them a flexible and essential part of JavaScript programming.

Example
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
let car1 = "Saab"
let car2 = "Volvo"
let car3 ="BMW"
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.
1. Create Array using Literal
Creating an array using array literal involves using square brackets [] to define and initialize the array.
// Creating an Empty Array
let a = [];
console.log(a);
// Creating an Array and Initializing with Values
let b = [10, 20, 30];
console.log(b);
Output
[]
[ 10, 20, 30 ]
2. Create using new Keyword (Constructor)
The " Array Constructor " refers to a method of creating arrays by invoking the Array constructor function.
// Creating and Initializing an array with values
let a = new Array(10, 20, 30);
console.log(a);
Output
[ 10, 20, 30 ]
Note: Both the above methods do exactly the same. Use the array literal method for efficiency, readability, and speed.
Basic Operations on JavaScript Arrays
1. Accessing Elements of an Array
Any element in the array can be accessed using the index number. The index in the arrays starts with 0.
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
// Accessing Array Elements
console.log(a[0]);
console.log(a[1]);
Output
HTML
CSS
2. Accessing the First Element of an Array
The array indexing starts from 0, so we can access first element of array using the index number.
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
// Accessing First Array Elements
let fst = a[0];
console.log("First Item: ", fst);
Output
First Item: HTML
3. Accessing the Last Element of an Array
We can access the last array element using [array.length - 1] index number.
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
// Accessing Last Array Elements
let lst = a[a.length - 1];
console.log("First Item: ", lst);
Output
First Item: JS
4. Modifying the Array Elements
Elements in an array can be modified by assigning a new value to their corresponding index.
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
console.log(a);
a[1]= "Bootstrap";
console.log(a);
Output
[ 'HTML', 'CSS', 'JS' ]
[ 'HTML', 'Bootstrap', 'JS' ]
5. Adding Elements to the Array
Elements can be added to the array using methods like push() and unshift() .
The push() method add the element to the end of the array.
The unshift() method add the element to the starting of the array.
// Creating an Array and Initializing with Values
let a = ["HTML", "CSS", "JS"];
// Add Element to the end of Array
a.push("Node.js");
// Add Element to the beginning
a.unshift("Web Development");
console.log(a);
Output
[ 'Web Development', 'HTML', 'CSS', 'JS', 'Node.js' ]
6. Removing Elements from an Array
To remove the elements from an array we have different methods like pop() , shift() , or splice() .
The pop() method removes an element from the last index of the array.
The shift() method removes the element from the first index of the array.
The splice() method removes or replaces the element from the array.

Join the conversation! Your thoughts help the community grow.