JavaScript Math.abs() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

Math.abs(x) returns the absolute value of a number. That means its distance from zero, with no minus sign. So -7 becomes 7, and 7 stays 7.

Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().

Syntax

Call Math.abs() like this. Start with this line.

JavaScript
Math.abs(x)
CallWhat it does
Math.abs(x)Returns the absolute value of x.

Built into JavaScript. Call Math.abs() on the Math object — no import required.

Library declarations
JavaScript
Math.abs(x)
JavaScript
interface Math {
  abs(x: number): number;
}

Parameters

ParameterDescription
xRequired. A number. Other values are converted to a number first.

Return Value

Return ValueDescription
numberThe absolute value of x. Returns NaN if x cannot be turned into a number.

JavaScript Examples

Example 1 — Basic Use

Negative numbers become positive. Positive numbers and zero stay the same.

JavaScript
console.log(Math.abs(-7));    // 7
console.log(Math.abs(7));     // 7
console.log(Math.abs(-2.5));  // 2.5
console.log(Math.abs(0));     // 0
Try It Yourself

How It Works

  1. Negative in. -7 has a minus sign, so Math.abs drops it and returns 7.
  2. Positive stays. 7 is already positive, so nothing changes.
  3. Decimals work. -2.5 becomes 2.5. It works on any number.
  4. Zero is zero. Math.abs(0) returns 0.

Example 2 — Distance Between Two Numbers

Subtract two numbers, then use Math.abs() so the answer is never negative.

JavaScript
var a = 12;
var b = 30;
var distance = Math.abs(a - b);
console.log("Distance:", distance);  // Distance: 18

var temp = 18;
var target = 21;
console.log("Off by " + Math.abs(temp - target) + " degrees");  // Off by 3 degrees
Try It Yourself

How It Works

  1. Subtract. a - b is -18. The order of the numbers does not matter.
  2. Remove the sign. Math.abs(-18) gives 18, which is the distance.
  3. Real use. Use it to see how far a value is from a target, like temperature or score.

Example 3 — Strings, null and NaN

Math.abs() converts its input to a number first. Some values turn into NaN.

JavaScript
console.log(Math.abs("-5"));       // 5
console.log(Math.abs(null));       // 0
console.log(Math.abs(undefined));  // NaN
console.log(Math.abs("hello"));    // NaN
Try It Yourself

How It Works

  1. Numeric text. "-5" becomes the number -5, then 5.
  2. null is 0. null converts to 0, so the result is 0.
  3. undefined is NaN. undefined cannot become a number, so you get NaN.
  4. Check first. For user input, use Number.isFinite(Number(value)) before you trust the result.

Browser & Runtime Support

Math.abs() has been part of JavaScript since ES1 and is Baseline Widely available everywhere.

✓ Baseline · ES1

Math.abs()

Supported in every browser and Node.js version. No polyfill is 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.abs Excellent

Bottom line: Use Math.abs() freely. It works in all browsers and runtimes.

Frequently Asked Questions

Math.abs(x) returns the absolute value of x. It removes the minus sign. Math.abs(-7) is 7 and Math.abs(7) is 7.
Subtract them and wrap the result in Math.abs(). Math.abs(12 - 30) is 18, no matter which number comes first.
It returns 0, because null is converted to 0. Math.abs("hello") returns NaN because the text is not a number.
No. It returns a new number. Save it with let result = Math.abs(x) if you need it later.
Yes. Call it on Math itself, like Math.abs(-3). Math is not a constructor, so you never use new Math().

Next: acos()

Learn how to find the arc cosine of a number.

acos() 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.

8 people found this page helpful