Javascript array methods
Created By: chatGPT
JavaScript provides several built-in methods for working with arrays. These methods allow you to manipulate, transform, and traverse arrays easily. Here are some commonly used array methods:
push(): Adds one or more elements to the end of an array and returns the new length of the array.
let fruits = ['apple', 'banana'];
fruits.push('orange');
console.log(fruits); // ['apple', 'banana', 'orange']
pop(): Removes the last element from an array and returns that element. This method changes the length of the array.
let fruits = ['apple', 'banana', 'orange'];
let lastFruit = fruits.pop();
console.log(lastFruit); // 'orange'
console.log(fruits); // ['apple', 'banana']
shift(): Removes the first element from an array and returns that element. This method changes the length of the array.
let fruits = ['apple', 'banana', 'orange'];
let firstFruit = fruits.shift();
console.log(firstFruit); // 'apple'
console.log(fruits); // ['banana', 'orange']
unshift(): Adds one or more elements to the beginning of an array and returns the new length of the array.
let fruits = ['banana', 'orange'];
fruits.unshift('apple');
console.log(fruits); // ['apple', 'banana', 'orange']
map(): Creates a new array populated with the results of calling a provided function on every element in the calling array.
let numbers = [1, 2, 3];
let squares = numbers.map(num => num * num);
console.log(squares); // [1, 4, 9]
filter(): Creates a new array with all elements that pass the test implemented by the provided function.
let numbers = [1, 2, 3, 4, 5];
let evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4]
reduce(): Executes a reducer function on each element of the array, resulting in a single output value.
let numbers = [1, 2, 3, 4];
let sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 10
forEach(): Executes a provided function once for each array element. It is used for executing side effects rather than returning a new array.
let numbers = [1, 2, 3];
numbers.forEach(num => {
console.log(num);
}); // Output: 1 2 3
splice(): Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
let fruits = ['apple', 'banana', 'orange'];
fruits.splice(1, 1, 'kiwi');
console.log(fruits); // ['apple', 'kiwi', 'orange']
slice(): Returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included).
let fruits = ['apple', 'banana', 'orange'];
let citrus = fruits.slice(1, 3);
console.log(citrus); // ['banana', 'orange']
concat(): Used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
let fruits1 = ['apple', 'banana'];
let fruits2 = ['orange', 'kiwi'];
let allFruits = fruits1.concat(fruits2);
console.log(allFruits); // ['apple', 'banana', 'orange', 'kiwi']
join(): Joins all elements of an array into a string, separating the elements with a specified separator.
let fruits = ['apple', 'banana', 'orange'];
let fruitString = fruits.join(', ');
console.log(fruitString); // 'apple, banana, orange'