Arrays are one of the most important data structures in JavaScript. An array allows developers to store multiple values inside a single variable.
Instead of creating separate variables for every value, arrays provide an organized way to collect and manage related data together.
Arrays are widely used in web development for storing lists of users, products, marks, images, messages, and many other types of information.
An array is a special type of object that stores multiple values in a single variable. Each value inside an array is called an element.
Every element in an array has a unique position called an index. JavaScript array indexing starts from 0.
let fruits = ["Apple", "Banana", "Mango"];
In this example, three values are stored inside one array named fruits.
Arrays make data management easier by allowing programmers to store and process multiple values efficiently.
JavaScript provides different ways to create arrays. The most common method is using square brackets [].
let numbers = [10,20,30,40];
This is the simplest and most commonly used way to create an array.
let numbers = new Array(10,20,30,40);
The Array constructor can also be used to create arrays, but developers usually prefer array literals because they are shorter and easier to understand.
Array elements are accessed using their index number. The first element always has index 0.
let colors = ["Red","Green","Blue"]; document.write(colors[0]);
Red
| Index | Value |
|---|---|
| 0 | Red |
| 1 | Green |
| 2 | Blue |
Array values can be modified by accessing their index and assigning a new value.
let fruits = ["Apple","Banana","Mango"]; fruits[1] = "Orange"; document.write(fruits);
Apple,Orange,Mango
The length property is used to find the total number of elements present in an array.
let subjects = [ "JavaScript", "Python", "Java", "SQL" ]; document.write(subjects.length);
4
JavaScript provides different methods to add new elements to arrays.
The push() method adds a new element at the end of an array.
let numbers = [1,2,3]; numbers.push(4); document.write(numbers);
1,2,3,4
The unshift() method adds an element at the beginning of an array.
let fruits = ["Banana","Mango"];
fruits.unshift("Apple");
document.write(fruits);
Apple,Banana,Mango
JavaScript provides methods to remove elements from arrays.
The pop() method removes the last element from an array.
let numbers = [10,20,30]; numbers.pop(); document.write(numbers);
10,20
The shift() method removes the first element from an array.
let colors = ["Red","Green","Blue"]; colors.shift(); document.write(colors);
Green,Blue
JavaScript provides many built-in array methods that help developers perform different operations such as adding, removing, searching, sorting, and modifying array elements.
Using array methods makes code shorter, cleaner, and easier to maintain.
The concat() method is used to combine two or more arrays. It does not modify the original arrays; instead, it returns a new array containing all elements.
array1.concat(array2);
let first = [1,2,3]; let second = [4,5,6]; let result = first.concat(second); document.write(result);
1,2,3,4,5,6
The slice() method is used to extract a portion of an array. It returns a new array without changing the original array.
array.slice(start,end);
let fruits = ["Apple","Banana","Mango","Orange"]; let result = fruits.slice(1,3); document.write(result);
Banana,Mango
The starting index is included, but the ending index is not included.
The splice() method is used to add, remove, or replace elements in an array. Unlike slice(), splice() changes the original array.
array.splice(index, number, item1, item2);
let numbers = [10,20,30,40]; numbers.splice(1,2); document.write(numbers);
10,40
let colors = ["Red","Blue"]; colors.splice(1,0,"Green"); document.write(colors);
Red,Green,Blue
JavaScript provides several methods to find elements inside an array.
The indexOf() method returns the position of the first occurrence of an element in an array.
let fruits = ["Apple","Banana","Mango"];
let position = fruits.indexOf("Mango");
document.write(position);
2
If the element does not exist, indexOf() returns -1.
The includes() method checks whether a specific value exists in an array. It returns true or false.
let numbers = [10,20,30]; document.write(numbers.includes(20));
true
The find() method returns the first element that satisfies a given condition.
let numbers = [5,10,15,20];
let result = numbers.find(function(value){
return value > 10;
});
document.write(result);
15
The sort() method is used to arrange array elements in ascending or alphabetical order.
let names = ["Rahul","Amit","Neha"]; names.sort(); document.write(names);
Amit,Neha,Rahul
By default, JavaScript converts numbers into strings while sorting. A comparison function should be used for proper numerical sorting.
let numbers = [50,10,30,20];
numbers.sort(function(a,b)
{
return a-b;
});
document.write(numbers);
10,20,30,50
The reverse() method reverses the order of array elements.
let numbers = [1,2,3,4]; numbers.reverse(); document.write(numbers);
4,3,2,1
The join() method converts array elements into a single string separated by a specified separator.
let fruits = ["Apple","Banana","Mango"];
let result = fruits.join("-");
document.write(result);
Apple-Banana-Mango
Iteration means accessing each element of an array one by one. JavaScript provides multiple ways to iterate arrays.
let fruits = ["Apple","Banana","Mango"];
for(let i=0; i <= fruits.length; i++)
{
document.write(fruits[i]);
}
Apple Banana Mango
The forEach() method executes a function once for every element of an array.
array.forEach(function(element)
{
});
let numbers = [10,20,30];
numbers.forEach(function(value)
{
document.write(value);
});
10 20 30
The map() method creates a new array by applying a function to every element of an existing array.
let numbers = [1,2,3,4];
let result = numbers.map(function(value)
{
return value * 2;
});
document.write(result);
2,4,6,8
The filter() method creates a new array containing only elements that satisfy a specific condition.
let numbers = [5,10,15,20];
let result = numbers.filter(function(value)
{
return value >= 10;
});
document.write(result);
10,15,20
The reduce() method reduces all array elements into a single value. It is commonly used for calculations such as total, average, and counting.
let numbers = [10,20,30];
let total = numbers.reduce(function(sum,value)
{
return sum + value;
},0);
document.write(total);
60
A multidimensional array is an array that contains one or more arrays as its elements. It is useful when data needs to be stored in a table-like structure with rows and columns.
Multidimensional arrays are commonly used for storing matrices, game boards, student records, and other structured data.
let marks = [ [80,90,70], [75,85,95], [60,70,88]]; document.write(marks[0][1]);
90
In this example, the first index selects the inner array and the second index selects the element inside that array.
Nested loops are generally used to access every element of a multidimensional array.
let numbers = [ [1,2,3], [4,5,6], [7,8,9]];
for(let i = 0; i < numbers.length; i++)
{
for(let j = 0; j < numbers[i].length; j++)
{
document.write(numbers[i][j]);
}
}
1 2 3 4 5 6 7 8 9
JavaScript arrays can store objects as elements. This structure is commonly used when handling real-world data such as users, products, employees, and students.
let students = [
{
name:"Rahul",
age:20
},
{
name:"Amit",
age:21
}
];
document.write(students[0].name);
Rahul
let products = [
{
id:101,
name:"Laptop",
price:50000
},
{
id:102,
name:"Mobile",
price:20000
}
];
document.write(products[1].price);
20000
Array destructuring allows developers to extract values from arrays and store them into separate variables using a simple syntax.
let [variable1, variable2] = array;
let colors = ["Red","Green","Blue"]; let [first, second, third] = colors; document.write(first);
Red
Destructuring makes code cleaner and easier to read when working with array values.
The spread operator (...) expands array elements into individual values. It is useful for copying arrays and combining multiple arrays.
let numbers = [10,20,30]; let copy = [...numbers]; document.write(copy);
10,20,30
let first = [1,2,3]; let second = [4,5,6]; let result = [...first,...second]; document.write(result);
1,2,3,4,5,6
The rest operator also uses three dots (...). It collects remaining elements into a new array.
let numbers = [10,20,30,40,50]; let [first,...remaining] = numbers; document.write(remaining);
20,30,40,50
There are different ways to create copies of arrays in JavaScript.
let original = [1,2,3]; let copy = [...original];
let original = [1,2,3]; let copy = original.slice();
let original = [1,2,3]; let copy = [].concat(original);
Arrays are objects in JavaScript. Two arrays with the same values are not considered equal because they are stored at different memory locations.
let a = [1,2,3]; let b = [1,2,3]; console.log(a == b);
false
The Set object can be used with arrays to remove duplicate values.
let numbers = [10,20,20,30,30]; let unique = [...new Set(numbers)]; document.write(unique);
10,20,30
The typeof operator returns object for arrays. Therefore, JavaScript provides the Array.isArray() method to check whether a value is an array.
let data = [10,20,30]; document.write(Array.isArray(data));
true
Arrays are used in almost every type of web application because they help store and manage collections of data.
JavaScript arrays are powerful data structures that allow developers to store multiple values in a single variable. Arrays provide many useful methods for adding, removing, searching, sorting, and processing data efficiently.
Important array concepts include indexing, array methods, iteration, multidimensional arrays, arrays of objects, destructuring, spread operator, and advanced array operations.
A strong understanding of arrays is necessary for learning advanced JavaScript topics such as objects, DOM manipulation, APIs, asynchronous programming, and modern JavaScript frameworks.