JavaScript Math.round() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

Math.round(x) rounds a number to the nearest integer. Numbers with a decimal part of .5 or more round up, and anything less rounds down.

Read the syntax below, then try the three examples to see the method in action.

Syntax

Call the method on Math. Start with this line.

JavaScript
Math.round(x)
CallWhat it does
Math.round(x)Returns x rounded to the nearest integer.

Built into JavaScript. Call Math.round(...) on the Math object — no import required. Math is not a constructor, so never write new Math().

Library declarations
JavaScript
Math.round(x)

Parameters

ParameterDescription
xRequired. The number to round. Other values are converted to a number first.

Return Value

Return ValueDescription
NumberThe nearest integer to x. Returns NaN if x is not a number.

JavaScript Examples

Example 1 — Round to the Nearest Integer

Below .5 goes down. From .5 upward goes up.

JavaScript
console.log(Math.round(4.2)); // 4
console.log(Math.round(4.5)); // 5
console.log(Math.round(4.7)); // 5
Try It Yourself

How It Works

  1. Round down. 4.2 is closer to 4, so the result is 4.
  2. Half goes up. 4.5 is exactly in the middle and rounds up to 5.
  3. Round up. 4.7 is closer to 5.
  4. Whole numbers stay. Math.round(5) is just 5.

Example 2 — Negative Numbers and Halves

Negative halves round toward zero, not away from it. This surprises many beginners.

JavaScript
console.log(Math.round(2.5)); // 3
console.log(Math.round(-4.5)); // -4
console.log(Math.round(-4.6)); // -5
console.log(Math.round(-0.4)); // -0
Try It Yourself

How It Works

  1. Positive half. 2.5 rounds up to 3.
  2. Negative half. -4.5 rounds toward the larger number, which is -4.
  3. Not a half. -4.6 is closer to -5.
  4. Minus zero. -0.4 rounds to -0. It behaves like 0 in most code.

Example 3 — Round to 2 Decimal Places

Multiply, round, then divide. Compare it with toFixed(), which returns a string.

JavaScript
const price = 19.456;
console.log(Math.round(price * 100) / 100); // 19.46
console.log(price.toFixed(2)); // 19.46
console.log(Math.round("abc")); // NaN
Try It Yourself

How It Works

  1. Shift the decimal. 19.456 * 100 is about 1945.6.
  2. Round. Math.round(1945.6) is 1946.
  3. Shift back. Dividing by 100 gives 19.46 as a number.
  4. toFixed(). toFixed(2) also gives "19.46" but as text (a string).
  5. Bad input. Math.round("abc") is NaN.

Browser & Runtime Support

Math.round is part of the first JavaScript standard (ES1) and is Baseline Widely available in every browser and in Node.js.

✓ Baseline · ES1

Math.round

Supported in Chrome, Firefox, Safari, Edge, Internet Explorer and Node.js for many years. No polyfill needed.

99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Math.round Excellent

Bottom line: Use Math.round anywhere. It works in all browsers.

Frequently Asked Questions

It rounds a number to the nearest integer. Math.round(4.6) is 5 and Math.round(4.4) is 4.
A .5 rounds up toward positive infinity. Math.round(2.5) is 3 and Math.round(-2.5) is -2.
Use Math.round(x * 100) / 100. You can also use x.toFixed(2), but that returns a string.
round goes to the nearest integer, floor always goes down, ceil always goes up, and trunc cuts off the decimal part.
It returns NaN because the text cannot be converted to a number.
All of them. It is part of the first JavaScript standard and works in every browser and in Node.js.

Next: sign()

Learn how to find if a number is positive, negative or zero.

sign() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful