Last modified: Jul 20, 2026
JavaScript Array Variables Guide
Arrays are essential in JavaScript. They let you store multiple values in a single variable. This guide will help you understand, create, and work with arrays. We will keep things simple and clear.
An array is an ordered list. Each item in this list is called an element. Arrays can hold numbers, strings, objects, or even other arrays. They are flexible and powerful.
What is an Array Variable?
An array variable is a container. It holds a collection of data. Instead of creating many separate variables, you use one array. This makes your code cleaner and easier to manage.
For example, instead of storing five names in five variables, you store them in one array. This is more efficient and readable.
Creating an Array
You can create an array in two ways. The most common is using square brackets. This is called the array literal syntax.
// Creating an array with square brackets
let fruits = ['apple', 'banana', 'cherry'];
You can also use the Array constructor. This method is less common but still valid.
// Creating an array with the Array constructor
let numbers = new Array(1, 2, 3, 4, 5);
Both methods work. The literal syntax is preferred because it is shorter and faster.
Accessing Array Elements
Each element in an array has an index. The index starts at 0. To access an element, use the index inside square brackets.
let colors = ['red', 'green', 'blue'];
// Access the first element (index 0)
console.log(colors[0]); // Output: red
// Access the third element (index 2)
console.log(colors[2]); // Output: blue
red
blue
If you try to access an index that does not exist, you get undefined. Always check the array length before accessing elements.
Modifying Arrays
Arrays are mutable. You can change their content. You can add, remove, or update elements.
To update an element, assign a new value to its index.
let cars = ['Toyota', 'Honda', 'Ford'];
// Update the second element
cars[1] = 'BMW';
console.log(cars); // Output: ['Toyota', 'BMW', 'Ford']
[ 'Toyota', 'BMW', 'Ford' ]
Adding and Removing Elements
JavaScript provides several methods to add or remove elements. push adds an element to the end. pop removes the last element.
let animals = ['cat', 'dog'];
// Add 'rabbit' to the end
animals.push('rabbit');
console.log(animals); // Output: ['cat', 'dog', 'rabbit']
// Remove the last element
animals.pop();
console.log(animals); // Output: ['cat', 'dog']
[ 'cat', 'dog', 'rabbit' ]
[ 'cat', 'dog' ]
To add or remove from the beginning, use unshift and shift.
let numbers = [2, 3, 4];
// Add 1 to the beginning
numbers.unshift(1);
console.log(numbers); // Output: [1, 2, 3, 4]
// Remove the first element
numbers.shift();
console.log(numbers); // Output: [2, 3, 4]
[ 1, 2, 3, 4 ]
[ 2, 3, 4 ]
Array Length
The length property tells you how many elements are in an array. It is updated automatically.
let cities = ['Paris', 'London', 'Tokyo'];
console.log(cities.length); // Output: 3
3
You can also set the length. This can truncate the array.
cities.length = 2;
console.log(cities); // Output: ['Paris', 'London']
[ 'Paris', 'London' ]
Looping Through Arrays
You often need to process every element. The for loop is a classic way.
let scores = [85, 92, 78, 90];
for (let i = 0; i < scores.length; i++) {
console.log('Score: ' + scores[i]);
}
Score: 85
Score: 92
Score: 78
Score: 90
A simpler method is the forEach loop. It runs a function for each element.
scores.forEach(function(score) {
console.log('Score: ' + score);
});
Score: 85
Score: 92
Score: 78
Score: 90
Common Array Methods
JavaScript offers many built-in methods. indexOf finds the index of an element. It returns -1 if not found.
let fruits = ['apple', 'banana', 'cherry'];
let index = fruits.indexOf('banana');
console.log(index); // Output: 1
1
The includes method checks if an element exists. It returns true or false.
let hasApple = fruits.includes('apple');
console.log(hasApple); // Output: true
true
To combine arrays, use concat. It does not change the original arrays.
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // Output: [1, 2, 3, 4]
[ 1, 2, 3, 4 ]
Multidimensional Arrays
An array can contain other arrays. This creates a multidimensional array. It is useful for matrices or grids.
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Access the element in row 1, column 2
console.log(matrix[1][2]); // Output: 6
6
Array Destructuring
Destructuring lets you unpack array values into variables. This makes code more concise.
let [first, second] = ['red', 'green'];
console.log(first); // Output: red
console.log(second); // Output: green
red
green
Spread Operator
The spread operator (...) expands an array. It is useful for copying or combining arrays.
let original = [1, 2, 3];
let copy = [...original];
console.log(copy); // Output: [1, 2, 3]
let more = [...original, 4, 5];
console.log(more); // Output: [1, 2, 3, 4, 5]
[ 1, 2, 3 ]
[ 1, 2, 3, 4, 5 ]
Best Practices
Always use const for arrays that should not be reassigned. Even with const, you can modify array content. Only reassignment is forbidden.
Check array length before accessing elements. This prevents errors. Use methods like includes or indexOf to check for elements.
For more on variable rules, see our JavaScript Variable Naming Rules guide. It will help you name your arrays clearly.
Understanding scope is also important. Read our JavaScript Variable Scope Explained article to learn how arrays behave in different scopes.
If you are new to variables, check the Types of JavaScript Variables page. It covers all variable types including arrays.
Conclusion
Arrays are a core part of JavaScript. They help you store and manage data efficiently. You learned how to create, access, modify, and loop through arrays. You also explored useful methods and best practices.
Practice with examples. Try creating arrays for your own projects. The more you use arrays, the more comfortable you will become. Arrays will make your code cleaner and more powerful.