Middle
What array methods do you know and have used?
sobes.tech AI
Answer from AI
In JavaScript, the following methods are often used for working with arrays:
push()— adds an element to the end of the array.pop()— removes the last element and returns it.shift()— removes the first element and returns it.unshift()— adds an element to the beginning of 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 an accumulator function.forEach()— executes a function for each element without creating a new array.find()— returns the first element that satisfies a condition.some()— checks if there is an element that satisfies a condition.every()— checks if all elements satisfy a condition.slice()— returns a copy of a portion of the array.splice()— changes the contents of the array by removing or adding elements.
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 evens = numbers.filter(n => n % 2 === 0); // [2, 4]