Arrays in JavaScript are used to store multiple values in a single variable. Here are some ways in which arrays can be manipulated in JavaScript:
Adding elements:
Arrays in Javascript are dynamic and allow for elements to be added.
```
let array = [1, 2, 3];
array.push(4); // [1, 2, 3, 4]
array.unshift(0); // [0, 1, 2, 3, 4]
```
Removing elements:
You can remove elements from an array using methods like pop() and shift().
```
array.pop(); // [0, 1, 2, 3]
array.shift(); // [1, 2, 3]
```
Iterating over arrays:
You can loop through arrays using for, for…of or forEach() methods.
```
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
for (let element of array) {
console.log(element);
}
array.forEach((element, index, array) => {
console.log(element);
})
```
Merging arrays:
You can merge multiple arrays into a single array.
```
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let merged = array1.concat(array2); // or
let merged = […array1, …array2]; // [1, 2, 3, 4, 5, 6]
```
Sorting and reversing arrays:
You can sort or reverse the order of elements in an array.
```
let array = [3, 1, 4, 1, 5, 9];
array.sort(); // [1, 1, 3, 4, 5, 9]
array.reverse(); // [9, 5, 4, 3, 1, 1]
```
Filtering arrays:
You can create a new array with all elements that pass the test implemented by the provided function.
```
let array = [1, 2, 3, 4, 5];
let filtered = array.filter(value => value % 2 === 0); // [2, 4]
```
Mapping arrays:
You can create a new array with the results of calling a provided function on every element in the array.
```
let array = [1, 2, 3, 4, 5];
let mapped = array.map(value => value * 2); // [2, 4, 6, 8, 10]
```
Reducing arrays:
You can apply a function against an accumulator and each element in the array (from left to right) to reduce it to a single output value.
```
let array = [1, 2, 3, 4, 5];
let reduced = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0); // 15
```
Find elements:
You can find an element in an array that satifies the provided testing function.
```
let array = [1, 2, 3, 4, 5];
let found = array.find(value => value > 3); // 4
```
These are just a few examples of the many ways in which arrays can be manipulated in JavaScript.