map()
- Purpose: Creates a new array by applying a function to each element of the original array.
- Example:
let numbers = [1, 2, 3, 4]; let doubled = numbers.map(num => num * 2); console.log(doubled); // [2, 4, 6, 8](code-box)
filter()
- Purpose: Creates a new array with all elements that pass the test implemented by the provided function.
- Example:
let numbers = [1, 2, 3, 4, 5]; let evenNumbers = numbers.filter(num => num % 2 === 0); console.log(evenNumbers); // [2, 4](code-box)
reduce()
- Purpose: Executes a reducer function on each element of the array, resulting in a single output value.
- Example:
let numbers = [1, 2, 3, 4]; let sum = numbers.reduce((total, num) => total + num, 0); console.log(sum); // 10(code-box)
forEach()
- Purpose: Executes a provided function once for each array element.
- Example:
let numbers = [1, 2, 3, 4]; numbers.forEach(num => console.log(num)); // Output: 1 2 3 4 (each on a new line)(code-box)
find()
- Purpose: Returns the value of the first element in the array that satisfies the provided testing function.
- Example:
let numbers = [1, 2, 3, 4]; let found = numbers.find(num => num > 2); console.log(found); // 3(code-box)
findIndex()
- Purpose: Returns the index of the first element in the array that satisfies the provided testing function.
- Example:
let numbers = [1, 2, 3, 4]; let index = numbers.findIndex(num => num > 2); console.log(index); // 2(code-box)
some()
- Purpose: Tests whether at least one element in the array passes the provided function's test.
- Example:
let numbers = [1, 2, 3, 4]; let hasEven = numbers.some(num => num % 2 === 0); console.log(hasEven); // true(code-box)
every()
- Purpose: Tests whether all elements in the array pass the provided function's test.
- Example:
let numbers = [1, 2, 3, 4]; let allEven = numbers.every(num => num % 2 === 0); console.log(allEven); // false(code-box)
concat()
- Purpose: Merges two or more arrays into a single array.
- Example:
let array1 = [1, 2]; let array2 = [3, 4]; let mergedArray = array1.concat(array2); console.log(mergedArray); // [1, 2, 3, 4](code-box)
slice()
- Purpose: Returns a shallow copy of a portion of an array into a new array object.
- Example:
let numbers = [1, 2, 3, 4, 5]; let sliced = numbers.slice(1, 3); console.log(sliced); // [2, 3](code-box)
splice()
- Purpose: Changes the contents of an array by removing, replacing, or adding elements.
- Example:
let numbers = [1, 2, 3, 4]; numbers.splice(1, 2, 'a', 'b'); console.log(numbers); // [1, 'a', 'b', 4](code-box)
push()
- Purpose: Adds one or more elements to the end of an array and returns the new length of the array.
- Example:
let numbers = [1, 2, 3]; numbers.push(4); console.log(numbers); // [1, 2, 3, 4](code-box)
pop()
- Purpose: Removes the last element from an array and returns that element.
- Example:
let numbers = [1, 2, 3]; let last = numbers.pop(); console.log(last); // 3 console.log(numbers); // [1, 2](code-box)
shift()
- Purpose: Removes the first element from an array and returns that element.
- Example:let numbers = [1, 2, 3];
let first = numbers.shift(); console.log(first); // 1 console.log(numbers); // [2, 3](code-box)
unshift()
- Purpose: Adds one or more elements to the beginning of an array and returns the new length of the array.
- Example:
let numbers = [1, 2, 3]; numbers.unshift(0); console.log(numbers); // [0, 1, 2, 3](code-box)
includes()
- Purpose: Determines whether an array includes a certain value among its entries.
- Example:
let numbers = [1, 2, 3]; let hasTwo = numbers.includes(2); console.log(hasTwo); // true(code-box)
indexOf()
- Purpose: Returns the first index at which a given element can be found in the array.
- Example:
let numbers = [1, 2, 3, 2]; let index = numbers.indexOf(2); console.log(index); // 1(code-box)
join()
- Purpose: Joins all elements of an array into a string and returns this string.
- Example:
let numbers = [1, 2, 3]; let joined = numbers.join('-'); console.log(joined); // '1-2-3'(code-box)
Understanding and practicing these methods will help you manipulate and work with arrays more efficiently in JavaScript.