API · JavaScript
JavaScript Math.abs() Function
Learn how JavaScript Math.abs() returns absolute values, handles common inputs, and calculates the distance between two numbers.
On this page
JavaScript’s Math.abs() returns the absolute value of a number: a nonnegative value stays the same, while a negative value becomes positive. This is useful when the direction of a difference does not matter, such as measuring the distance between two numeric values. Math.abs() is a function on the Math object, so call it with parentheses; it is not an instance method on a number.
What Does Math.abs() Do?
Absolute value describes a number’s magnitude without its sign. For example, Math.abs(7) is 7, and Math.abs(-7) is also 7. Positive zero and negative zero both produce 0. Positive infinity stays positive infinity, and negative infinity becomes positive infinity. For inputs whose numeric conversion yields NaN, Math.abs() returns NaN, which means “not a number.” Some values instead make the call throw a TypeError: Symbol values and BigInt values cannot be used by Math.abs().
Math.abs() accepts one argument. JavaScript converts that argument to a number before computing the result, so some non-number inputs have useful numeric results. This conversion is automatic; if inputs come from forms or other untrusted sources, validate them separately when the application needs to distinguish a numeric value from a value that merely converts to one.
Syntax of Math.abs()
Call the function with the value whose magnitude you need: Math.abs(value).
value may be a number or another JavaScript value that can be converted to a number. The function returns a number, including NaN when conversion yields NaN; it throws a TypeError for Symbol and BigInt arguments. An object whose primitive conversion throws can also make the call throw. There is no separate Math instance to create and no need to import the built-in object.
How Math.abs() Handles Inputs
The result depends on how JavaScript converts the argument. Actual null converts to 0; an empty string also converts to 0. An empty array converts to 0 as well. These results can surprise readers who expect every non-number input to produce NaN, so use the actual value rather than a string that only looks like JavaScript syntax. Conversion may also throw instead of returning a number: Math.abs(Symbol()) and Math.abs(1n) throw a TypeError, and an object whose primitive conversion throws propagates that error.
A string containing a numeric value, such as "-3.42", converts to -3.42, and the function returns 3.42. A nonnumeric string such as "Tutorial" produces NaN. An empty object and an array with multiple elements also produce NaN under ordinary conversion. Calling the function with no argument produces NaN, because the missing value is undefined and its conversion yields NaN.
These conversions are convenient for simple inputs, but they are not input validation. If an application should reject blank fields instead of treating them as zero, check for blank input before calling Math.abs().
Examples of Math.abs()
Run this example:
console.log(Math.abs(-7));
7
The function can calculate an absolute difference by applying it to a subtraction result. The order of the operands does not change the nonnegative distance returned.
Run this example:
function absoluteDifference(first, second) {
return Math.abs(first - second);
}
console.log(absoluteDifference(2, 4));
console.log(absoluteDifference(4, 2));
For individual values, pass JavaScript values directly. These calls illustrate numeric strings, actual null, an empty string, an empty array, a multi-element array, a nonnumeric string, and negative infinity:
const values = ["-3.42", null, "", [], [9, 1], "Tutorial", -Infinity];
for (const value of values) {
console.log(Math.abs(value));
}
The loops print one result for each input in the same order. NaN appears for values that do not convert to a number; negative infinity produces positive infinity.
Common Mistakes and Edge Cases
Do not pass quoted text when you intend to test a JavaScript value. For example, "null" is a string and produces NaN, while null is the null value and produces 0. Likewise, "[]" is a string, whereas [] is an empty array that converts to zero.
Do not describe Math.abs() as a method called on the number itself. The call form is Math.abs(value), because abs belongs to the built-in Math object. Also avoid assuming that all arrays, objects, or strings behave alike: conversion determines the result, and unusual objects can define their own primitive conversion behavior.
Finally, NaN is a numeric value with special behavior, not an exception thrown by Math.abs(). If later logic must detect it, use Number.isNaN(result) rather than comparing the result with NaN.