Array Methods JS
Array Methods JS
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'];
fruits.push('cherry');
console.log(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 = fruits.pop();
console.log(lastFruit); // Output: 'cherry'
console.log(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 = fruits.shift();
console.log(firstFruit); // Output: 'apple'
console.log(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'];
fruits.unshift('apple');
console.log(fruits); // Output: ['apple', 'banana', 'cherry']
```
5. **splice()**
- Adds or removes elements from an array at a specified index.
```javascript
let fruits = ['apple', 'banana', 'cherry'];
fruits.splice(1, 1, 'orange', 'grape');
console.log(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 = fruits.slice(2, 4);
console.log(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 = fruits.concat(vegetables);
console.log(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 = fruits.indexOf('banana');
console.log(index); // Output: 1
```
9. **forEach()**
- Executes a provided function once for each array element.
```javascript
let numbers = [1, 2, 3];
numbers.forEach(function(num) {
console.log(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 = numbers.map(function(num) {
return num * 2;
});
console.log(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.