How to get the absolute value of a number in JavaScript
Getting the absolute value of a number is essential for distance calculations, difference measurements, range validation, and progress indicators in JavaScript applications.
With over 25 years of experience in software development and as the creator of CoreUI, I’ve used Math.abs() in progress bars, difference meters, and validation systems where magnitude matters more than sign.
For rounding considerations after computing absolute values, see how to round a number in JavaScript.
Use Math.abs() to get the non-negative magnitude of a number.
Math.abs(-42) // 42
Math.abs(42) // 42
Math.abs(0) // 0
Math.abs(-3.14) // 3.14
1. Basic Usage and Edge Cases
Math.abs() handles all numeric types including special values.
Math.abs(-42) // 42
Math.abs(42) // 42 — positive unchanged
Math.abs(0) // 0
Math.abs(-0) // 0 — negative zero becomes 0
Math.abs(-3.14) // 3.14
Math.abs(-Infinity) // Infinity
Math.abs(NaN) // NaN — non-numeric stays NaN
Math.abs(null) // 0 — null coerces to 0
Math.abs(undefined) // NaN
Math.abs('-10') // 10 — numeric strings are coerced
Math.abs('hello') // NaN — non-numeric strings produce NaN
When handling form input or API data, validate with Number.isNaN() before using the result.
2. Measuring Distance Between Two Numbers
Absolute value is the standard way to compute the unsigned distance between two values.
const distance = (a, b) => Math.abs(a - b)
distance(10, 3) // 7
distance(3, 10) // 7 — order doesn't matter
distance(-5, 5) // 10
distance(1.5, 2.8) // 1.2999999999999998 — floating-point; round if needed
3. Progress Bar and Percentage Calculations
Progress values can go negative if a target changes. Math.abs() ensures the bar always shows a non-negative width.
function progressPercent(current, target) {
if (target === 0) return 0
return Math.min(100, Math.abs(current / target) * 100)
}
progressPercent(75, 100) // 75
progressPercent(-20, 100) // 20 — negative progress shown as positive
progressPercent(120, 100) // 100 — capped at 100
CoreUI’s Progress component expects a value between 0 and 100 — this guard ensures safe input.
4. Clamping Differences for Validation
When validating that two values are within a tolerance, absolute difference is the right tool.
function withinTolerance(a, b, tolerance) {
return Math.abs(a - b) <= tolerance
}
withinTolerance(1.0, 1.001, 0.01) // true — within 1% tolerance
withinTolerance(1.0, 1.02, 0.01) // false — exceeds tolerance
withinTolerance(5, -5, 9) // false — difference is 10
This pattern is common in price comparisons, sensor readings, and form validation where exact equality is unreliable.
5. Sorting by Distance from a Value
Sort an array by proximity to a target value using absolute difference as the comparator.
const values = [10, -3, 7, -8, 2, 15]
const target = 5
const byProximity = values.sort((a, b) => Math.abs(a - target) - Math.abs(b - target))
// [7, 2, 10, -3, -8, 15] — sorted by distance from 5
6. Computing Absolute Difference in Arrays
Find the maximum swing, total deviation, or average absolute error across a dataset.
const actual = [100, 95, 102, 88, 105]
const predicted = [98, 97, 100, 90, 103]
const errors = actual.map((v, i) => Math.abs(v - predicted[i]))
// [2, 2, 2, 2, 2]
const mae = errors.reduce((sum, e) => sum + e, 0) / errors.length
// 2 — Mean Absolute Error
Mean Absolute Error (MAE) is a common accuracy metric used in data dashboards and chart annotations.
Best Practice Note:
Math.abs() is the clearest and most portable way to get magnitude — avoid manual conditionals like n < 0 ? -n : n, which are more error-prone.
Always check for NaN after Math.abs() when processing user input or API data, since non-numeric strings produce NaN silently.
For display purposes, chain with toFixed() or Intl.NumberFormat to control decimal precision.



