Array methods : filter() - 05

The array method filter() executes or runs a callback function on each of the array elements and the elements that fulfill the condition provided by the function will be added to the new array.

The elements will be removed that do not pass the test provided by the callback function.
filter() method will not execute or run the callback function for empty elements. This method doesn't change the original array.

For numbers:

const numbers = [10, 20, 30, 40, 50, 60, 70, 80];
  const result = numbers.filter((number) => number > 30);

  console.log(result);
  // Result:
  // [40, 50, 60, 70, 80];


For strings:

const words = ["Mozahedul", "shahin", "rafiq", "john", "elton"];
const result = words.filter((word) => word.length > 5);

console.log(result);
// Result:
// ["Mozahedul", "shahin"]


Post a Comment

Previous Post Next Post