JavaScript Array Methods
Last Updated : 24 Jan 2026
JavaScript has its own set of methods built into the language that allow developers to efficiently interact with arrays. JavaScript allows us to do such operations as adding, removing, updating, or getting elements, transforming, or iterating through arrays.

What do you understand by an Array?
In JS, the array is a special kind of variable that can store multiple values at a time. We use arrays to be able to efficiently and effectively work with lists of data. An array consists of elements, and each element has a number, an index starting at 0. The array is a special type of variable that can hold any kind of data, such as a number, string, object, or even another array present inside it. They are mostly used for collection management, iteration, sorting/filtering, etc.
Array Basic Methods
1. Array toString() in JavaScript
This method fabricates an array in a string format separated by a comma. This approach does not alter the initial array and is significant for representing the content of the array in the form of a string.
Output:
Array to String is Yashraj, Thomas, Kate
Explanation:
In the above example, we defined a variable named names using the const keyword. We assigned it an array consisting of names. We utilized the toString() method to transform the array into a single string and logged the result in the console.
2. Array at() in JavaScript
It returns the element at a specified index in an array. It can be constructed for both positive and negative indices. Indices can be positive, which begin at the start (0-based), and can be negative, which count from the end of the array.
Note: The at() method is a new method introduced in ECMAScript 2022 (ES13). It won't run in the older environment. It will work perfectly in the ECMAScript 2022 (ES13) supported environment.
Example
Output:
Second Element is Thomas Last Element is Kate
Explanation:
In the above example, we defined a variable named names using the const keyword and assigned it an array consisting of names. By utilizing the at() method of array, we accessed the second and the last element from the array and logged them in the console.
If your environment does not support at() method then you can use the following code:
Output:
Second Element is Thomas Last Element is Kate
3. Array forEach() in JavaScript
The forEach() method is a built-in method that is utilized to call a function for each element present in the array. It neither returns a new array nor modifies the original array, but it is utilized for iteration.
Example
Output:
spring summer winter autumn
Explanation:
In the above code, we defined a variable named season and assigned it an array consisting of season names. By utilizing the forEach() method, we return every element of the array and log the output in the console.
4. Array join() in JavaScript
It joins all elements of an array to form a single string. The second parameter is an optional custom separator otherwise, it is a comma by default. It's helpful for making formatted strings out of array items.
Output:
Joined Elements are Yashraj - Thomas - Kate
Explanations:
In the above example, we defined a variable named names utilizing the const keyword and an array is assigned to it, consisting of names. By utilizing the join() method, we joined the elements on the basis of (-) and logged the result in the console.
5. Array pop() in JavaScript
It eliminates the last element from the array. It will modify the length of the array. You will see that it changes the given array. Utilizing pop() method on an empty array returns undefined.
Output:
Removed Element is Kate Updated Array is [ 'Yashraj', 'Thomas' ]
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and assigned it an array consisting of names. By utilizing the pop() method, we removed the last element from the array and logged the output in the console.
6. Array push() in JavaScript
The push() method is utilized to add one or more elements to the end of an array and then return the new array with a different length. It appends these new elements to the end of the old array.
Output:
New length of the array is 3 Updated array is [ 'Yashraj', 'Thomas', 'Kate' ]
Explanation:
In the above code, we created an array named names utilizing the const keyword and we assigned it an array consisting of names. By utilizing the push() method, we inserted an element at the end of the array. And logged the output in the console.
7. Array shift() in JavaScript
This array shift() method simply pops the first element of the array. It is done by mutating the original array and moving each element to the left after the shift. If the array has no elements, then it returns nothing.
Output:
Removed element: Yashraj Updated array: [ 'Thomas', 'Kate' ]
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and an array is assigned to it, consisting of names. By utilizing the shift() method, we removed the first element of the array and logged it to the console.
8. Array unshift() in JavaScript
The unshift() method is utilized to add new items to the start of the array and then return the new array with a different length. It just moves everything to the right of where you want to insert the new element.
Output:
New length of the array: 3 Updated array: [ 'Yashraj', 'Thomas', 'Kate' ]
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and an array is assigned to it, which contains names. By utilizing the unshift() method, we insert an element at the beginning of the array. Here, we inserted Yashraj into the array and logged it in the console.
9. Array concat() in JavaScript
The concat() method is used to concatenate or join two or multiple arrays into a new array. It does not modify the original array.
Output:
Combined Names are [ 'Yashraj', 'Thomas', 'Kate', 'Kartik' ]
Explanation:
In the above example, we defined two variables named names1 and names2 using the const keyword and an array is assigned to both of the variables, which consists of names. By making use of the concat() method, we combined both arrays and stored them in a variable named combinedNames. We logged the result in the console.
10. Array copyWithin() in JavaScript
This method shallow copies the specified part of an array to another location within the same array, followed by the return of the original array without modifications in the size of the original array. The arguments are the target index and the start index.
Output:
[ 'Yashraj', 'Kate', 'Kartik', 'Kartik' ]
Explanation:
In the above example, we defined a variable named names using the const keyword and assigned it an array which contains names. By utilizing the copyWithin() method, we shallow copied to another location in the same array and logged the output to the console.
11. Array flat() in JavaScript
The flat() method returns a new array with a given depth of sub-array elements collapsed so that they all fit into a single array. The default depth is 1.
Output:
Array Flat: [ 'Yashraj', 'Thomas', 'Kate' ]
Explanation:
In the above example, we defined a variable named nestedArray using the const keyword and assigned it an array consisting of names. It is a nested array, which means arrays inside an array. Here, we defined another variable named flatArray and assigned it nestedArray.flat(2) which reduced the depth of a nested array and the parameter 2 means flatten the array depth 2. Then we logged the result in the console.
12. Array splice() in JavaScript
The splice() method can change the contents of an array by eliminating existing elements and/or adding new elements in place. It returns a new, shallow copy of the portion of the array from start to (but not including) end.
Output:
Removed elements: [ 'Thomas' ] Updated array: [ 'Yashraj', 'Kartik', 'Kate' ]
Explanation:
In the above code, we defined a variable named names utilizing the const keyword and assigned it an array consisting of names. By utilizing the splice() method, we replaced an element from the array and logged the result in the console. Here, in the names.splice(1, 1, "Kartik"), the first parameter 1 means start from index 1, it starts from Thomas, the second parameter means remove 1 element, so it removes Thomas. And the third parameter is the name that we want to replace. Therefore, Thomas is replaced by Kartik.
13. Array toSpliced() in JavaScript
The toSpliced() method returns a new array with the defined changes, like adding an element or deleting an element, without mutating the source array. It is like a slice() method but the slice method change the original array, it does not change the original array, instead, it returns a new array with the modifications.
Note: The toSpliced() is a new method introduced in ES2023. It won't run in an older environment. It will work perfectly in the ES2023-supported environment.
Example
Output:
New array: [ 'Yashraj', 'Kartik', 'Kate' ] Original array: [ 'Yashraj', 'Thomas', 'Kate' ]
Explanation:
In the above code, we defined a variable named names utilizing const keyword and assigned it an array containing names. By using toSpliced, we replaced Thomas with Kartik and logged the output in the console. Here, in the names.toSpliced(1, 1, "Kartik"), the first parameter 1 means start from the index 1. So, it started with Thomas, the second parameter is 1, which means remove 1 element, so it removed the element at the 1 index, that is Thomas. And the third parameter is Kartik, which is the parameter we want to insert in the place of Thomas.
14. Array slice() in JavaScript
Let's take a look at how the slice() method works. It returns a new array, but it does not change the original array. This method takes two optional parameters, which are the start index (inclusive) and end index (exclusive). If no end index is passed, slicing will go until the end of the array. Negative indices count from the end.
Output:
Extracted portion: [ 'Thomas', 'Kate' ] Original array: [ 'Yashraj', 'Thomas', 'Kate', 'Kartik' ]
Explanation:
In this example, we defined a variable named names utilizing const keyword and assigned it an array, which contains names. By utilizing slice() method, we extracted a portion of the array and logged the result in the console.
Searching Methods
1. Array indexOf() in JavaScript
The indexOf() method is used to return the first index where a provided element can be found in the array, or -1 if it is not found. It uses strict equality (===) to test for a found element.
Output:
First occurrence: 1 Index of not present element: -1
Explanation:
In the above code, we defined a variable named names utilizing the const keyword and assigned it an array consisting of names. By utilizing the indexOf() method, we get the first occurrence of Thomas. The first occurrence of Thomas is at index 1, elements from index 1 to 3 and we log it in the console.
2. Array lastIndexOf() in JavaScript
The lastIndexOf() method is utilized to return the last index of an array at which a provided element can be found. If the element is not found, then it returns -1. It checks for strict equality.
Output:
First occurrence: 3 Index of not present element: -1
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and an array is assigned to it, consisting of names. By utilizing lastIndexOf() method, we get the last index of the provided element in the array. The last index of the element was logged and the result was displayed in the console.
3. Array includes() in JavaScript
The includes() method is utilized to check whether the array holds a particular element. It returns true if found and false otherwise. This is a form of strict equality check.
Output:
Include's Thomas: true Include's Rahul: false
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and an array is assigned to it, which consists of names. By utilizing the includes() method, we find out whether the array contains an element or not, and it returns a Boolean value. In names.includes("Thomas")), we check whether Thomas is in the array or not. The array contains Thomas, therefore, it returns true, but it does not contain Rahul, so it returns false.
4. Array find() in JavaScript
The find() method is used to retrieve the first element from the array that passes a testing function. It returns undefined if no element satisfies the condition.
Output:
First matching element: Kate
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and assigned it an array that contains names. By utilizing the find() method, we get the first element from the array that satisfies the given condition. Here, we get the first element, which starts with the letter K, and logged the result to the console.
5. Array findIndex() in JavaScript
Similarly, the findIndex() method is utilized to return the index of the first element in the provided array that fulfils a filter testing function. -1 is returned if no element fulfils the condition.
Output:
Index of the matching element: 1
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and assigned it an array, which contains the names. By utilizing the findIndex() method, we get the index of the first element that satisfies the given condition. Here, we get the index of the first element which starts with K and logged the result in the console.
6. Array findLast() in JavaScript
The findLast() method is utilized to return the last array element that fulfil a testing function. If no element fulfils the condition, then it will return undefined.
Note: The findLast() is a new method introduced in ES2023 (ECMAScript 2023). It won't run in an older environment. It will work perfectly in the ES2023-supported environment.
Example
Output:
Last matching element: Kartik
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and assigned it an array, which contains the names. By utilizing the findLast() method, we get the last element that satisfies the given condition. Here, we get the last element, which starts with K, and logged the result in the console.
7. Array findLastIndex() in JavaScript
The findLastIndex() method is used to return the index of the last element in the array that passes the test provided by the test function. It returns -1 if no element meets the condition.
Note: The Array findLastIndex() is a new method introduced in ES2023 / ECMAScript 2022. It won't run in an older environment. It will work perfectly in ES2023 / ECMAScript 2022.
Example
Output:
Index of the last matching element: 3
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and an array is assigned to it, which contains names. By utilizing the findLastIndex() method, we get the index of the last element in the array that satisfies the condition. Here, we get the last index of an element which starts with K and logged the result in the console.
Sorting Methods
1. Array sort() in JavaScript
The sort() method is utilized to sort the elements of an array and returns the sorted Array. By default, it will sort as strings in ascending order, which will return unpredictable results when sorting numbers. For more complex sorting, a custom compare function can be provided.
Output:
Sorted alphabetically: [ 'Kate', 'Thomas', 'Yashraj' ] Numeric sort: [ 1, 2, 5, 10 ]
Explanation:
In the above example, we defined a variable named names and numbers utilizing the const keyword and an array is assigned to the names variable, which contains names and number in the numbers variable. By utilizing the sort() method, we sort the array, which contains string data types alphabetically and numbers in ascending order.
2. Array reverse() in JavaScript
The reverse() method gets the order of the elements of an array reversed, and it modifies the same original array. In that case, it is mostly used in conjunction with sort() to get descending order.
Output:
Reversed order: [ 'Kate', 'Thomas', 'Yashraj' ]
Explanation:
In the above example, we defined a variable named names utilizing the const keyword. An array is assigned to it, which contains names. By utilizing the reverse() method, we reverse the array and logged the result in the console.
3. Array toSorted() in JavaScript
The toSorted() method gets the order of the elements of an array reversed, and it modifies the same original array. It has similar functionality as sort() but does not change the original array, it returns a new one with the modifications.
Note: The toSorted() is a new method introduced in ES2023. It won't run in an older environment. It will work perfectly in the ES2023-supported environment.
Example
Output:
Sorted array: [ 'Kate', 'Thomas', 'Yashraj' ] Original array: [ 'Yashraj', 'Thomas', 'Kate' ]
Explanation:
In the above example, we defined a variable named names utilizing the const keyword and an array is assigned to it, which contains names. By utilizing the toSorted() method, we sorted the array alphabetically and logged the result in the console.
4. Array toReversed() in JavaScript
The toReversed() method is used to return an array with the elements in reverse order, but does not change the original. This is nice for reversing arrays without actual data mutation.
Note: The toReversed() is a new method introduced in ES2023. It won't run in an older environment. It will work perfectly in the ES2023-supported environment.
Example
Output:
Reversed array: [ 'Kate', 'Thomas', 'Yashraj' ] Original array: [ 'Yashraj', 'Thomas', 'Kate' ]
Explanation:
In the above example, we defined a variable named names utilizing the const keyword. An array is assigned to it which contains names. By utilizing the toReversed() method, we reversed the array and logged it in the console.
5. Sorting Objects
To sort objects in an array, you need a custom comparison function that gets passed to sort(). The sorting logic works by defining this function based on object properties.
Output:
After ascending sorting: [
{ name: 'Thomas', age: 22 },
{ name: 'Kate', age: 23 },
{ name: 'Yashraj', age: 24 }
]
After alphabetically sorting: [
{ name: 'Kate', age: 23 },
{ name: 'Thomas', age: 22 },
{ name: 'Yashraj', age: 24 }
]
Explanation:
In the above example, we defined a variable named students using the const keyword and assigned it an array of objects. Each object contains a name and an age. By utilizing sorting objects, we sorted the array in ascending order of age and logged the result in the console.
Conclusion:
These JavaScript array methods allow us to sort arrays effectively and manipulate data, no matter if we are dealing with simple values or complex objects.