Array Filter in JavaScript

Level 393 keystrokes

This uses filter to create a new array containing only even numbers.

JavaScript code

// Create an array of numbers
1let nums = [1, 2, 3, 4, 5, 6];
// filter() keeps only the elements for which the callback returns true
// n % 2 === 0 is true for even numbers
2let evens = nums.filter(n => n % 2 === 0);
// Outputs [2, 4, 6]
3console.log(evens);

Output

[ 2, 4, 6 ]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • =× 6
  • ,× 5
  • ;× 3
  • .× 2
  • (× 2
  • )× 2
  • [× 1
  • ]× 1
  • %× 1
  • >× 1

More JavaScript examples at level 3

← JavaScript typing practice