Array Some and Every in JavaScript

Level 3150 keystrokes

Uses some() to check if any element matches and every() to check if all elements match.

JavaScript code

// Create an array of even numbers
1const nums = [2, 4, 6, 8, 10];
// some() returns true if AT LEAST ONE element passes the test
// n % 2 !== 0 checks for odd numbers (none here, so false)
2const hasOdd = nums.some(n => n % 2 !== 0);
// every() returns true only if ALL elements pass the test
// n % 2 === 0 checks if number is even (all are, so true)
3const allEven = nums.every(n => n % 2 === 0);
// Output both results
4console.log(hasOdd, allEven);

Output

false true
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.

  • =× 10
  • ,× 5
  • ;× 4
  • .× 3
  • (× 3
  • )× 3
  • %× 2
  • >× 2
  • !× 1
  • [× 1

More JavaScript examples at level 3

← JavaScript typing practice