Open In App

How to Filter an Array in JavaScript ?

Last Updated : 08 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The array.filter() method is used to filter array in JavaScript. The filter() method iterates and check every element for the given condition and returns a new array with the filtered output.

Syntax

const filteredArray = array.filter( callbackFunction ( element [, index [, array]])[, thisArg]);

Note: It is an in-built JavaScript method to filter an array.

JavaScript
// Given array
const a = [ 1, 2, 3, 4, 5, 6];

// Filter the even numbers
const a1 = a.filter( number => number % 2 === 0);

// Display output
console.log("Filtered Array:", a1);