Array.map(JavaScript array method) under the hood

danities316

Danities Ichaba

Posted on May 9, 2023

Array.map(JavaScript array method) under the hood

Array.map() is a useful built-in function in JavaScript that is used to create a new array by applying a transformation function to each element in an existing array. The map() function takes two arguments, one an array and the other a callback function that specifies how each element in the array should be transformed.

In this article, we will dive under the hood and explore how the map() function works in JavaScript.

The basics of map()
Before we dive into the technical details of how map() works, let's start by reviewing the basic syntax and usage of the function. Here's an example of how map() can be used to create a new array of squared values:

const originalArray = [1, 2, 3, 4, 5];

const squaredArray = originalArray.map((element)=> element * element; );

console.log(squaredArray); // Output: [1, 4, 9, 16, 25]
Enter fullscreen mode Exit fullscreen mode

In this example, we start with an array of numbers and use the map() function to create a new array where each element is the square of the corresponding element in the original array.

How map() works under the hood
Now let's take a closer look at how the map() function works behind the scenes.

When you call map() on an array, JavaScript creates a new array with the same length as the original array but never change(mutate) the origanal array. The new array is initially empty, but JavaScript will fill it in with values as it iterates through each element in the original array.

For each element in the original array, map() calls the callback function that you passed as an argument. This callback function takes three arguments:

  1. The current element being processed

  2. The index of the current element

  3. The original array that map() was called on

The callback function returns a new value that should be used for the corresponding element in the new array. Once the callback function has been called for every element in the original array, map() returns the new array.

Here's a simplified version of what happens under the hood when you call map()

function map(array, callback) {

const newArray = [];

for (let i = 0; i < array.length; i++) {

const currentElement = array[i];

const transformedElement = callback(currentElement, i, array); newArray.push(transformedElement);

}

return newArray;

}

Enter fullscreen mode Exit fullscreen mode

This code is a simplified version of what happens when you call map(). In reality, the map() function is implemented more efficiently and with more error checking, but this code captures the basic idea.

Conclusion
The map() function is a powerful tool that makes it easy to transform arrays in JavaScript. By understanding how map() works under the hood, you can better appreciate the elegance and efficiency of this built-in function. Whether you're working on a small JavaScript project or a large-scale web application, map() is a valuable tool to have in your arsenal.

💖 💪 🙅 🚩
danities316
Danities Ichaba

Posted on May 9, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related