filter() array method in javascript

Array methods were introduced in javascript in ES5. Before this, every array iteration was performed by loops. But after the introduction of array methods to perform array operations, work on an array has become very easy without the implementation of extra coding.

Here we will discuss some array methods.

filter()

As the name, It filters the array by such types of conditions. It creates a new array of elements which passes the condition test. It does not change the original array. It is very useful to obtain such type of data which satisfies the given criteria.

syntax

array.filter(callbackfunction(element,index,array)=>statements);

parameters

callbackfunction

A function that executes elements of the array.

arguments

  1. element: current element is being processed in the array.

  2. index: the current index of the current element is being processed in the array.

  3. array: This is the main array on which this method is being performed.

Return Value

It returns a new array of elements which passes the given condition.

Example

Let, us imagine you have an array of id numbers if you want to filter the id list by removing invalid id numbers.

Output: