JavaScript Arrays
An array is a special type of JavaScript object used to store multiple values in a single variable.
For example:
In JavaScript, array indexes start from 0.
Empty Array
You can create an empty array using square brackets:
Array With Elements
You can create an array with multiple elements:
An array can contain different types of values:
However, in most applications, it is better to keep an array's elements logically related.
Access Whole Array
You can access the complete array using its variable name:
Output:
Access Single Element From an Array
You can access an array element using its index.
Output:
Remember that array indexing starts from 0.
Length of Array
The length property returns the number of elements in an array.
Output:
Example:
Output:
Loops in Arrays
JavaScript provides several ways to loop through an array.
for Loop in Array
A traditional for loop can be used to access every element using its index.
Syntax
Example
Output:
Here, i represents the array index.
for-in Loop in Array
The for-in loop iterates over the indexes of an array.
Syntax
Example
Output:
In this loop:
contains the index.
For example:
for-of Loop in Array
The for-of loop directly gives you the value of each array element.
Syntax
Example
Output:
Here:
contains the actual value.
Difference Between for-in and for-of
Output:
Whereas:
Output:
Therefore:
JavaScript Array Methods
JavaScript provides many built-in methods for working with arrays.
The following table contains commonly used array methods.
Method | Syntax | Description |
|---|---|---|
|
| Adds one or more items to the end of an array. |
|
| Removes the last item from an array. |
|
| Removes the first item from an array. |
|
| Adds one or more items to the beginning of an array. |
|
| Returns a new array containing elements from the start index up to, but not including, the end index. |
|
| Adds, removes, or replaces elements in the original array. |
|
| Executes a function once for each array element. |
|
| Creates a new array by combining arrays and/or values. |
|
| Returns the first index where the item is found, otherwise returns |
|
| Returns the last index where the item is found, otherwise returns |
|
| Returns |
|
| Returns the first element that satisfies the condition, otherwise |
|
| Returns the index of the first element that satisfies the condition, otherwise |
|
| Returns a new array containing all elements that satisfy the condition. |
|
| Creates a new array by transforming each element. |
|
| Sorts the elements of an array. |
|
| Reverses the order of elements in an array. |
|
| Converts array elements into a string using the specified separator. |
|
| Reduces all array elements into a single value. |
1. push()
The push() method adds one or more elements to the end of an array.
Syntax
Example
Output:
You can add multiple elements:
Output:
The push() method returns the new length of the array.
2. pop()
The pop() method removes the last element from an array.
Syntax
Example
Output:
pop() returns the element that was removed.
3. shift()
The shift() method removes the first element from an array.
Syntax
Example
Output:
4. unshift()
The unshift() method adds one or more elements to the beginning of an array.
Syntax
Example
Output:
5. slice()
The slice() method creates a new array by copying elements from a specified range.
Syntax
The endIndex is not included.
Example
Output:
Here:
Elements at indexes 1, 2, and 3 are copied.
The original array is not modified.
Output:
You can also omit the end index:
This returns all elements from index 2 to the end.
6. splice()
The splice() method can remove, add, or replace elements in an array.
Unlike slice(), splice() modifies the original array.
Syntax
Remove elements
Output:
Here:
So 20 and 30 are removed.
Add elements
To add elements without deleting anything, use 0 as deleteCount.
Output:
Replace elements
Output:
7. forEach()
The forEach() method executes a function for each element of an array.
Syntax
Example
Output:
You can also use the index:
Output:
8. concat()
The concat() method creates a new array by combining arrays and/or values.
Syntax
Example
Output:
You can also combine individual values:
Output:
The original array is not modified.
9. indexOf()
The indexOf() method returns the first index where a specified value is found.
Syntax
Example
Output:
If the item is not found, it returns:
Example:
Output:
indexOf()returns-1, notfalse, when the item is not found.
10. lastIndexOf()
The lastIndexOf() method returns the last index where a specified value is found.
Syntax
Example
Output:
If the item is not found:
11. includes()
The includes() method checks whether an element exists in an array.
Syntax
Example
Output:
If the element does not exist:
Output:
12. find()
The find() method returns the first element that satisfies a condition.
Syntax
Example
Output:
The method checks:
It stops at the first matching element.
If no element satisfies the condition, it returns:
13. findIndex()
The findIndex() method returns the index of the first element that satisfies a condition.
Syntax
Example
Output:
Because 30 is at index 2.
If no element satisfies the condition, it returns:
14. filter()
The filter() method creates a new array containing all elements that satisfy a condition.
Syntax
Example
Output:
Unlike find(), filter() can return multiple elements.
15. map()
The map() method executes a function for every element and creates a new array containing the returned values.
Syntax
Example
Output:
The original array remains unchanged.
Arrow Function
The same example can be written more shortly:
16. sort()
The sort() method sorts the elements of an array.
Syntax
For strings:
Output:
Sorting Numbers
For numbers, use a compare function.
Output:
For descending order:
sort() modifies the original array.17. reverse()
The reverse() method reverses the order of elements in an array.
Syntax
Example
Output:
reverse() modifies the original array.18. join()
The join() method converts the elements of an array into a string.
Syntax
Example
Output:
You can use any separator:
Output:
Without specifying a separator:
The default separator is a comma.
19. reduce()
The reduce() method processes all elements of an array and reduces them to a single value.
Syntax
Example: Calculate Sum
Output:
The execution happens like this:
Therefore:
Arrow Function
The same code can be written as:
Or in a shorter form: