javascript / beginner
Snippet
The Array Filter Method
The filter() method creates a new array with all elements that pass the test implemented by the provided function. It does not change the original array.
snippet.js
javascript
1
2
3
4
5
const ages = [12, 18, 25, 30];const adults = ages.filter(function(age) {return age >= 18;});console.log(adults);
nodejs
Breakdown
1
const ages = [12, 18, 25, 30];
An array of numbers representing ages.
2
const adults = ages.filter(function(age) { ... });
Calls filter with a callback function that checks if an age is 18 or older.