DEV Community

Cover image for Unlock the power of array.filter() with a deep dive into its inner workings
Danities Ichaba
Danities Ichaba

Posted on

Unlock the power of array.filter() with a deep dive into its inner workings

Array.filter() is another useful built-in function in JavaScript that is used to create a new array by filtering out elements from an existing array based on a certain condition. The filter() function takes one argument, which is a callback function that specifies the condition to filter the elements.

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

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

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

const evenArray = originalArray.filter((element)=>element % 2 ===0;);

console.log(evenArray); // Output: [2, 4]
Enter fullscreen mode Exit fullscreen mode

In this example, we start with an array of numbers and use the Array.filter() function to create a new array where only the even numbers are included.

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

When you call Array.filter() on an array, JavaScript creates a new array with the same length as the original 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, Array.filter() 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 Array.filter() was called on

The callback function returns a Boolean value that indicates whether the current element should be included in the new array. If the callback function returns true, the current element is included in the new array. If the callback function returns false, the current element is excluded from the new array.

Once the callback function has been called for every element in the original array, Array.filter() returns the new array.

Here's a simplified version of what happens under the hood when you call Array.filter():

function filter(array, callback) {
  const newArray = [];

  for (let i = 0; i < array.length; i++) {
    const currentElement = array[i];
    if (callback(currentElement, i, array)) {
      newArray.push(currentElement);
    }
  }

  return newArray;
}
Enter fullscreen mode Exit fullscreen mode

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

Conclusion
The Array.filter() function is a powerful tool that makes it easy to filter out elements from arrays in JavaScript. By understanding how Array.filter() 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, Array.filter() is a valuable tool to have in your arsenal.

Top comments (0)