170k views
2 votes
The _____ method creates a new array by passing the original array items to the callback function, which returns the equivalent value of the array items.

A) map()
B) slice()
C) splice()
D) reduce()

1 Answer

2 votes

Final answer:

The map() method creates a new array by applying a callback function to each element of the original array, returning an array with the transformed elements without modifying the original array.

Step-by-step explanation:

The map() method creates a new array by passing the original array items to the callback function, which returns the equivalent value of the array items. This method is commonly used when you want to transform the data in an array by applying a function to each element, resulting in a new array with the applied changes. For example, if you want to square each number in an array, you could use map() as follows:

const numbers = [1, 2, 3, 4];
const squaredNumbers = numbers.map(num => num × num); // [1, 4, 9, 16]
Note that the map() method does not modify the original array but creates a new one with the results. Other methods such as slice(), splice(), and reduce() serve different purposes. slice() is used to return a shallow copy of a portion of an array, splice() is used to change the contents of an array by removing or replacing existing elements, and reduce() is used to accumulate values within an array to a single value by applying a function.
User Padmalcom
by
8.2k points