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.Syntaxconst 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); OutputFiltered Array: [ 2, 4, 6 ] Please Read JavaScript Array Tutorial for complete understanding of JavaScript Array.Table of ContentUsing for LoopUsing array.reduce() MethodUsing for LoopThe for loop iterates and repeatedly execute the given code until the condition is false. We can add the condition filter element in each iteration and use the array.push() to create new array of filtered values. JavaScript // Given array const a = [ 1, 2, 3, 4, 5, 6]; // Initialize empty array to store the results const a1 = [] for( let i = 0; i < a.length ; i++){ if( a[i] % 2 === 0 ) // Condition for even number a1.push(a[i]); // Push element to new array } // Display output console.log("Filtered Array:", a1) OutputFiltered Array: [ 2, 4, 6 ] Using array.reduce() MethodThe array.reduce() method executes a reducer function (that you will provide) on each element of the array, resulting in a single output value.Syntaxarray.reduce( function( acc , currentValue, currentIndex, arr), initialValue ) JavaScript // Given array const a = [ 1, 2, 3, 4, 5, 6]; // Filter the even numbers const a1 = a.reduce((acc, number) => { if (number % 2 === 0) { acc.push(number); // Only push the number if it is even } return acc; }, []); // Initialize acc as an empty array // Display output console.log("Filtered Array:", a1); OutputFiltered Array: [ 2, 4, 6 ] Comment More infoAdvertise with us Next Article How to Filter an Array in JavaScript ? R rituali8i63 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript Array filter() Method The filter() method creates a new array containing elements that satisfy a specified condition. This method skips empty elements and does not change the original array.Create a new array consisting of only those elements that satisfy the condition checked by canVote() function.JavaScript// JavaScrip 3 min read How to filter Nested Array using Key in JavaScript ? Filtering a nested array in JavaScript involves the process of selectively extracting elements from an array of objects, where each object contains another array. We will discuss how can we filter Nested Array using the key in JavaScript. Filtering a nested array based on a key in JavaScript can be 3 min read How to filter an array of objects in ES6 ? In this article, we will try to understand how we could filter out or separate certain data from the array of objects in ES6.Let us first try to understand how we could create an array of objects by following certain syntax provided by JavaScript.Table of ContentJavascript Array of ObjectsJavascript 4 min read How to use map() filter() and reduce() in JavaScript? The map(), filter(), and reduce() are the array functions that allow us to manipulate an array according to our logic and return a new array after applying the modified operations on it. 1. JavaScript map() MethodThe map() method in JavaScript is used to create a new array by applying a function to 2 min read How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja 6 min read Like