Home JavaScript Math Methods abs() JavaScript Math.abs() Method 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.
Call What 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 interface Math {
abs(x: number): number;
} Parameters Parameter Description x Required. A number. Other values are converted to a number first.
Return Value Return Value Description number The absolute value of x. Returns NaN if x cannot be turned into a number.
JavaScript Examples 1. Basic use 2. Distance between numbers 3. Strings, null and NaN Example 1 — Basic Use Negative numbers become positive. Positive numbers and zero stay the same.
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 How It Works Negative in. -7 has a minus sign, so Math.abs drops it and returns 7.Positive stays. 7 is already positive, so nothing changes.Decimals work. -2.5 becomes 2.5. It works on any number.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.
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 Distance: 18
Off by 3 degrees How It Works Subtract. a - b is -18. The order of the numbers does not matter.Remove the sign. Math.abs(-18) gives 18, which is the distance.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.
console.log(Math.abs("-5")); // 5
console.log(Math.abs(null)); // 0
console.log(Math.abs(undefined)); // NaN
console.log(Math.abs("hello")); // NaN How It Works Numeric text. "-5" becomes the number -5, then 5.null is 0. null converts to 0, so the result is 0.undefined is NaN. undefined cannot become a number, so you get NaN.Check first. For user input, use Number.isFinite(Number(value)) before you trust the result.Compatibility
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 What does Math.abs() do? 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.
How do I find the distance between two numbers? Subtract them and wrap the result in Math.abs(). Math.abs(12 - 30) is 18, no matter which number comes first.
What does Math.abs(null) return? It returns 0, because null is converted to 0. Math.abs("hello") returns NaN because the text is not a number.
Does Math.abs() change the original variable? No. It returns a new number. Save it with let result = Math.abs(x) if you need it later.
Is Math.abs() a static method? Yes. Call it on Math itself, like Math.abs(-3). Math is not a constructor, so you never use new Math().
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion