An array is a special variable that can hold more than one value at a time. It is used to store a collection of data, which makes it much easier to manage large lists of related information.
Imagine you have a list of cars. Storing them in single variables would look like this:
let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";
However, what if you had 300 cars? Finding a specific one would be a nightmare. An array allows you to store them all under a single name and access them using a number called an index.
The most common way to create a JavaScript array is by using array literals (square
brackets []).
const cars = ["Saab", "Volvo", "BMW"];
const
keyword, as the reference to the array should not change, even if you modify the
elements inside.
You can access an array element by referring to its index number. Remember that JavaScript arrays are zero-based: the first element is [0], the second is [1], and so on.
const fruits = ["Banana", "Orange", "Apple"];
let fruit = fruits[0]; // Result: "Banana"
You can change the value of a specific element by using its index:
const fruits = ["Banana", "Orange", "Apple"];
fruits[0] = "Mango";
// Now the first fruit is "Mango" instead of "Banana"
The length property of an array returns the number of elements it contains.
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.length); // 4
length property to access the last element
of any array: fruits[fruits.length - 1].
In JavaScript, arrays use numbered indexes, while objects use named indexes. Arrays are a special type of object used for lists of data where the order matters.
[index] to access or modify specific elementslength property tells you how many items are in the array[] over new Array() for better performance
for...in to loop over an array. It's designed for objects and doesn't guarantee index order, and it can pick up inherited properties too. Use a regular for loop or for...of for arrays instead.
let b = a; makes b point to the same array as a — mutating b also changes a. Use [...a] to make a real copy.
delete arr[i] to remove an item. This leaves a hole (undefined) at that index instead of shrinking the array. Use splice() to actually remove an element.
A second example — adding and removing items, which is what arrays are used for constantly in real apps:
let todos = ["Buy milk", "Walk the dog"];
todos.push("Finish homework"); // add to the end
todos.splice(1, 1); // remove "Walk the dog" (1 item at index 1)
console.log(todos); // ["Buy milk", "Finish homework"]
Q: How do I remove an item from an array?
A: Use splice(index, count) to remove by position, or filter() to remove by condition without mutating the original array.
Q: Are arrays passed by reference or by value?
A: By reference. Passing an array into a function and mutating it inside will change the original array too — copy it first with [...arr] if you need to avoid that.
Q: What's the difference between push() and unshift()?
A: push() adds to the end; unshift() adds to the beginning. See the MDN Array reference for the full list of array methods.