Filter method in JavaScript

Nilesh Saini
1 min readMay 7, 2022

--

concise explanation of filter method in Js

Photo by Devin Avery on Unsplash

The filter() method creates a new array with all elements that pass the condition provided by the callback function.

  1. Creates a new array.
  2. Iterates through the given array.
  3. Runs a callback function on each element of the given array.
  4. If the callback function returns true, that value will be added to the new array.
  5. If the callback function returns false, that value will be ignored from the new array.

Note: the result of the callback function always returns a boolean.

How does it work?

Note: learn more about callback function parameters here.

Example:

Write a function to return a new array of all the numbers divisible by three.

--

--

Nilesh Saini
Nilesh Saini

Written by Nilesh Saini

Web Developer/ Front-end engineer who loves solving Rubik's Cube

No responses yet