JavaScript provides a wide range of built-in array functions that allow you to manipulate arrays and perform common operations on them. Here are some of the most commonly used array functions:
push(): adds one or more elements to the end of an array.
const myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray); // [1, 2, 3, 4]
pop(): removes the last element from an array and returns it.
const myArray = [1, 2, 3];
const lastElement = myArray.pop();
console.log(lastElement); // 3
console.log(myArray); // [1, 2]
shift(): removes the first element from an array and returns it.
const myArray = [1, 2, 3];
const firstElement = myArray.shift();
console.log(firstElement); // 1
console.log(myArray); // [2, 3]
unshift(): adds one or more elements to the beginning of an array.
const myArray = [1, 2, 3];
myArray.unshift(0, -1);
console.log(myArray); // [-1, 0, 1, 2, 3]
slice(): returns a new array that contains a portion of an existing array.
const myArray = [1, 2, 3, 4, 5];
const slicedArray = myArray.slice(1, 4);
console.log(slicedArray); // [2, 3, 4]
splice(): adds or removes elements from an array at a specified index.
const myArray = [1, 2, 3];
myArray.splice(1, 1, 4, 5);
console.log(myArray); // [1, 4, 5, 3]
concat(): combines two or more arrays into a new array.
const myArray1 = [1, 2, 3];
const myArray2 = [4, 5];
const concatenatedArray = myArray1.concat(myArray2);
console.log(concatenatedArray); // [1, 2, 3, 4, 5]
indexOf(): returns the first index at which a given element can be found in an array.
const str = "Hello, world!";
const index = str.indexOf("world");
console.log(index); // output: 7
forEach(): executes a provided function once for each array element.
const myArray = [1, 2, 3];
myArray.forEach((element) => {
console.log(element);
});
// Output:
// 1
// 2
// 3
map(): creates a new array with the results of calling a provided function on every element in the array.
const myArray = [1, 2, 3];
const mappedArray = myArray.map((element) => {
return element * 2;
});
console.log(mappedArray); // [2, 4, 6]
filter(): creates a new array with all elements that pass the test implemented by the provided function.
const myArray = [1, 2, 3, 4, 5];
const filteredArray = myArray.filter((element) => {
return element % 2 === 0;
});
console.log(filteredArray); // [2, 4]
reduce(): applies a function against an accumulator and each element in the array to reduce it to a single value.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 15
sort(): sorts the elements of an array in place and returns the sorted array.
const numbers = [4, 2, 7, 1, 9, 5];
// Sort the array in ascending order
numbers.sort((a, b) => a - b);
console.log(numbers); // Output: [1, 2, 4, 5, 7, 9]
reverse(): reverses the order of the elements in an array.
const myArray = [1, 2, 3, 4, 5];
myArray.reverse(); // [5, 4, 3, 2, 1]
const myString = "hello";
myString.reverse(); // This will throw an error because reverse()
is not a method of a string.
const myArray = myString.split(""); // Split the string into an array
of characters
myArray.reverse(); // Reverse the order of the characters
const reversedString = myArray.join(""); // Join the characters back
into a string
console.log(reversedString); // "olleh"
join(): creates a string from an array by concatenating all the elements using a specified separator.
const myArray = ["apple", "banana", "orange"];
const myString = myArray.join(", ");
console.log(myString); // "apple, banana, orange"
These are just some of the many functions available for working with arrays in JavaScript.