Round a Number in JavaScript

Level 129 keystrokes

Uses Math.round() to round 4.6 to the nearest integer and prints 5.

JavaScript code

// Math.round() returns the nearest integer: 4.6 is closer to 5 than to 4
// Exactly .5 rounds up toward +∞: Math.round(2.5) is 3, Math.round(-2.5) is -2
1console.log(Math.round(4.6));

Output

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

  • .× 3
  • (× 2
  • )× 2
  • ;× 1

More JavaScript examples at level 1

← JavaScript typing practice