JavaScript arrays have a variety of built-in methods for performing common
operations like adding or removing elements, searching for elements, and iterating
over array elements. Here is a list of some of the most commonly used array methods
along with examples:
1. **push()**
- Adds one or more elements to the end of an array and returns the new length of
the array.
```javascript
let fruits = ['apple', 'banana'];
[Link]('cherry');
[Link](fruits); // Output: ['apple', 'banana', 'cherry']
```
2. **pop()**
- Removes the last element from an array and returns that element.
```javascript
let fruits = ['apple', 'banana', 'cherry'];
let lastFruit = [Link]();
[Link](lastFruit); // Output: 'cherry'
[Link](fruits); // Output: ['apple', 'banana']
```
3. **shift()**
- Removes the first element from an array and returns that element.
```javascript
let fruits = ['apple', 'banana', 'cherry'];
let firstFruit = [Link]();
[Link](firstFruit); // Output: 'apple'
[Link](fruits); // Output: ['banana', 'cherry']
```
4. **unshift()**
- Adds one or more elements to the beginning of an array and returns the new
length of the array.
```javascript
let fruits = ['banana', 'cherry'];
[Link]('apple');
[Link](fruits); // Output: ['apple', 'banana', 'cherry']
```
5. **splice()**
- Adds or removes elements from an array at a specified index.
```javascript
let fruits = ['apple', 'banana', 'cherry'];
[Link](1, 1, 'orange', 'grape');
[Link](fruits); // Output: ['apple', 'orange', 'grape', 'cherry']
```
6. **slice()**
- Returns a shallow copy of a portion of an array into a new array object.
```javascript
let fruits = ['apple', 'banana', 'cherry', 'orange', 'grape'];
let citrus = [Link](2, 4);
[Link](citrus); // Output: ['cherry', 'orange']
```
7. **concat()**
- Combines two or more arrays and returns a new array.
```javascript
let fruits = ['apple', 'banana'];
let vegetables = ['carrot', 'potato'];
let combined = [Link](vegetables);
[Link](combined); // Output: ['apple', 'banana', 'carrot', 'potato']
```
8. **indexOf()**
- Returns the first index at which a given element can be found in the array, or
-1 if it is not present.
```javascript
let fruits = ['apple', 'banana', 'cherry', 'banana'];
let index = [Link]('banana');
[Link](index); // Output: 1
```
9. **forEach()**
- Executes a provided function once for each array element.
```javascript
let numbers = [1, 2, 3];
[Link](function(num) {
[Link](num * 2);
});
// Output:
// 2
// 4
// 6
```
10. **map()**
- Creates a new array populated with the results of calling a provided function
on every element in the calling array.
```javascript
let numbers = [1, 2, 3];
let doubled = [Link](function(num) {
return num * 2;
});
[Link](doubled); // Output: [2, 4, 6]
```
These are just a selection of the many array methods available in JavaScript. Each
method has its own specific purpose and can be used in various scenarios to
manipulate and work with arrays efficiently.