JavaScript | Array filter() method

CodeyMaze
1 min readOct 23, 2021

--

Array.filter() method iterates through the array & calls a callback function on each element of the array and creates a new array with those elements only which have passed the test implemented by the callback function.

Syntax:

Array.filter(callback(element, index, array), thisArguments)

The callback function accepts three parameters :

  1. element — This parameter holds the value of array element being processed.
  2. index — This parameter holds the index of array element being processed. It is optional.
  3. array — This parameter holds the array itself. It is also optional.
  4. thisArguments — This parameter holds the context to passed to the callback function.

Example 1 :

In this example, we will filter out those elements for which the length of the string is greater than 3.

const words = ['React', 'javascript', 'angular', 'jquery', 'js']; const result = words.filter(getWord); function getWord() { return word.length > 5; } console.log(result);

Output :

["javascript", "angular", "jquery"]

Subscribe to our YouTube channel for more videos :

https://www.youtube.com/channel/UCEXWe9R6-ppk4zhNo2JbtmQ

--

--

CodeyMaze
CodeyMaze

Written by CodeyMaze

Crafting Solutions Through Code & Words https://codeymaze.com Feel free to follow me :)

No responses yet