Middle
What array methods do you know in JavaScript?
sobes.tech AI
Answer from AI
In JavaScript, arrays have many useful methods for working with data. Here are some of them:
push()— adds element(s) to the end of the array.pop()— removes the last element and returns it.shift()— removes the first element and returns it.unshift()— adds element(s) to the beginning of the array.forEach()— executes a function for each element in the array.map()— creates a new array by applying a function to each element.filter()— creates a new array with elements that pass a test.reduce()— reduces the array to a single value by applying a function.find()— returns the first element that satisfies a condition.some()— checks if at least one element satisfies a condition.every()— checks if all elements satisfy a condition.includes()— checks if the array contains a certain element.slice()— returns a copy of a portion of the array.splice()— changes the array by adding or removing elements.sort()— sorts the elements of the array.
Example of using map and filter:
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2); // [2, 4, 6, 8, 10]
const even = numbers.filter(n => n % 2 === 0); // [2, 4]