JavaScript (ES2026) reference · Math Methods and Constants

JavaScript Math.abs() Method

Return the absolute value of a number: its distance from zero, so Math.abs(-5) is 5.

JavaScript (ES2026) Since ES1

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Math.abs(x)

Parameters

ParameterDescription
x A number.

Return value

A non-negative number (or NaN).

Examples

Absolute valuesTry it yourself
console.log(Math.abs(-7.5), Math.abs(3), Math.abs("-2"), Math.abs("x"));

Output

7.5 3 2 NaN
Compare floats with a toleranceTry it yourself
const a = 0.1 + 0.2, b = 0.3;
console.log(a === b, Math.abs(a - b) < Number.EPSILON);

Output

false true

Version history

VersionChange
JavaScript ES1Added

In other languages

Was this page helpful?