JavaScript provides many built-in array methods to add, remove, search, sort, and manipulate array elements efficiently.
Array methods are predefined functions that perform operations on arrays.
They help you:
| Method | Purpose |
|---|---|
| push() | Add element at end. |
| pop() | Remove last element. |
| shift() | Remove first element. |
| unshift() | Add element at beginning. |
| concat() | Combine arrays. |
| slice() | Extract array portion. |
| splice() | Add or remove elements. |
| join() | Convert array to string. |
Adds one or more elements to the end of an array.
Output:
Apple, Mango, Orange
Removes the last element from an array.
Output:
Apple, Mango
shift() removes the first element.
unshift() adds elements at the beginning.
Combines two or more arrays.
Output:
1,2,3,4
The slice() method returns selected elements as a new array.
It does not change the original array.
Output:
Mango, Orange
The splice() method adds or removes elements from an array.
Output:
Apple, Banana, Orange
The join() method converts an array into a string.
Output:
Apple-Mango-Orange
Returns the first index of an element.
Output:
1
Returns the last occurrence of an element.
Output:
3
Checks whether an element exists in an array.
Output:
true
Executes a function for every array element.
Output:
10
20
30
Creates a new array by performing an operation on each element.
Output:
2,4,6
The filter() method creates a new array containing elements that satisfy a condition.
Output: 30, 40
find() returns the first matching value.
findIndex() returns the index of the first matching value.
Output:
30
2
some() checks whether at least one element satisfies a condition.
every() checks whether all elements satisfy a condition.
Output:
true
true
sort() arranges elements.
reverse() reverses the order.
The reduce() method combines all array values into a single value.
Output: 60
| Method | Purpose |
|---|---|
| push() | Add Last |
| pop() | Remove Last |
| shift() | Remove First |
| unshift() | Add First |
| map() | Transform Data |
| filter() | Filter Data |
| reduce() | Single Value |
| sort() | Sort Elements |
Which method adds an element to the end of an array?