If-Else Statement in JavaScript

Level 281 keystrokes

This checks if n is even or odd and outputs 'odd' since 7 is odd.

JavaScript code

// Declare a variable n with value 7
1let n = 7;
// The % operator gives the remainder after division
// n % 2 === 0 checks if n is divisible by 2 (even)
2if (n % 2 === 0) {
3 console.log("even");
// else runs when the if condition is false
4} else {
// 7 % 2 is 1, not 0, so this runs
5 console.log("odd");
6}

Output

odd
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.

  • "× 4
  • =× 4
  • ;× 3
  • (× 3
  • )× 3
  • .× 2
  • {× 2
  • }× 2
  • %× 1

More JavaScript examples at level 2

← JavaScript typing practice