filter method in array

The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.
 avatar
Pranav
javascript
3 years ago
234 B
7
Indexable

//array.filter(function(currentValue, index, arr), thisValue)

const ages = [38, 23, 16, 40, 7];

const result = ages.filter(checkAdult);

function checkAdult(age) {
  return age >= 18;
}

console.log(result)

//output [ 38, 23, 40 ]
Editor is loading...