Updated August, 2012:

I did some profiling with these implementations:

/* Test 1: */ b = Math.abs(a);
/* Test 2: */ b = abs(a); //local copy: abs = Math.abs;
/* Test 3: */ b = a < 0 ? a * -1 : a;
/* Test 4: */ b = a < 0 ? -a : a;

I got the following result on Windows 7. Values are normalized after the fastest result per browser to make it easier to compare which method is faster:

        1:Math 2:abs 3:*-1  4:-    1.0=   Version
Chrome    1.0   1.0   1.0   1.0    111ms  21.0.1180.75 m
Firefox   1.0   1.0   1.2   1.2    127ms  14.0.1
IE        1.4   1.0   1.1   1.0    185ms  9.0.8112
Opera     1.9   1.6   1.1   1.0    246ms  12.00
Safari    1.6   1.6   1.1   1.0    308ms  5.1.7

Conclusion: When I did this test 3 years ago, -a was fastest, but now Math.abs(x) is faster in Firefox! In Chrome abs(a) and -a got the same time and it was only 3 ms difference to the slowest method when I tested it with 10 000 000 numbers.

My Recommendation: Use Math.abs(a). If you are in a tight loop and by profiling has found it to be too slow, you can use a local reference to the abs function:

var abs=Math.abs; //A local reference to the global Math.abs function
for (i=0;i<1234567890;++i) if ( abs( v[i] ) > 10) ++x;
Answer from some on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_abs.asp
JavaScript Math abs() Method
The Math.abs() method returns the absolute value of a number.
๐ŸŒ
Learnsic
learnsic.com โ€บ blog โ€บ the-javascript-absolute-value-method-how-and-when-to-use-it
The Javascript Absolute Value Method: How and When to Use It
May 29, 2024 - In other words, the absolute value provides the magnitude or non-negative value of a given number. JavaScript provides a built-in method, Math.abs(), which calculates the absolute value of a number.
๐ŸŒ
Math.js
mathjs.org โ€บ docs โ€บ reference โ€บ functions โ€บ abs.html
math.js | an extensive math library for JavaScript and Node.js
Calculate the absolute value of a number. For matrices, the function is evaluated element wise. math.abs(x) Type | Description โ€”- | โ€”โ€”โ€”โ€“ ยท math.abs(3.5) // returns number 3.5 math.abs(-4.2) // returns number 4.2 math.abs([3, -5, -1, 0, 2]) // returns Array [3, 5, 1, 0, 2] sign ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-math-abs-method
JavaScript Math abs() Method - GeeksforGeeks
July 15, 2024 - Javascript Math.abs() method is used to return the absolute value of a number.
Find elsewhere
Top answer
1 of 5
83

Updated August, 2012:

I did some profiling with these implementations:

/* Test 1: */ b = Math.abs(a);
/* Test 2: */ b = abs(a); //local copy: abs = Math.abs;
/* Test 3: */ b = a < 0 ? a * -1 : a;
/* Test 4: */ b = a < 0 ? -a : a;

I got the following result on Windows 7. Values are normalized after the fastest result per browser to make it easier to compare which method is faster:

        1:Math 2:abs 3:*-1  4:-    1.0=   Version
Chrome    1.0   1.0   1.0   1.0    111ms  21.0.1180.75 m
Firefox   1.0   1.0   1.2   1.2    127ms  14.0.1
IE        1.4   1.0   1.1   1.0    185ms  9.0.8112
Opera     1.9   1.6   1.1   1.0    246ms  12.00
Safari    1.6   1.6   1.1   1.0    308ms  5.1.7

Conclusion: When I did this test 3 years ago, -a was fastest, but now Math.abs(x) is faster in Firefox! In Chrome abs(a) and -a got the same time and it was only 3 ms difference to the slowest method when I tested it with 10 000 000 numbers.

My Recommendation: Use Math.abs(a). If you are in a tight loop and by profiling has found it to be too slow, you can use a local reference to the abs function:

var abs=Math.abs; //A local reference to the global Math.abs function
for (i=0;i<1234567890;++i) if ( abs( v[i] ) > 10) ++x;
2 of 5
33

I would suggest picking the method that more clearly shows your intention, rather than worrying about the performance. In this case, the performance gain of multiplying by -1 is probably minimal.

When you use Math.abs(), it is very clear that you want a positive value. When you use * -1 it is not clear, and requires more investigation to determine if the input value is always negative.

๐ŸŒ
p5.js
p5js.org โ€บ reference โ€บ p5 โ€บ abs
abs
A number's absolute value is its distance from zero on the number line. -5 and 5 are both five units away from zero, so calling abs(-5) and abs(5) both return 5.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ standard-library โ€บ Math โ€บ abs
JavaScript Math abs() - Absolute Value Calculation | Vultr Docs
November 27, 2024 - The Math.abs() function in JavaScript is a straightforward yet powerful utility that calculates the absolute value of a given number. Absolute value refers to the non-negative value of a number without regard to its sign.
๐ŸŒ
Medium
medium.com โ€บ @aniksarkar0177 โ€บ what-is-math-abs-in-javascript-040ce9a56aac
What is Math.abs in Javascript? - Aniksarkar - Medium
October 4, 2023 - In JavaScript, Math.abs() is a built-in Math object function used to calculate the absolute value of a number. The absolute value of a number is its distance from zero on the number line, regardless of whether the number is positive or negative.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ library โ€บ math โ€บ abs
JavaScript Math.abs()
Become a certified JavaScript programmer. Try Programiz PRO! ... The abs() method finds the absolute value of the specified number (without any sign) and returns it. // find absolute value of -2 number= Math.abs(-2); console.log(number); // Output: 2
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ javascript โ€บ javascript math.abs() method
JavaScript Math.abs() Method
September 1, 2008 - The JavaScript Math.abs() method accepts a number as a parameter and returns the absolute value of the provided number. If the provided value is not a valid number or cannot be converted to a number, the result is NaN.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ javascript: absolute value - [math.abs() with examples]
r/learnjavascript on Reddit: JavaScript: Absolute Value - [Math.abs() with Examples]
March 17, 2021 -

Math.abs() is an object method in JavaScript Math class , used to get the absolute value of a number. Read this article to understand it more...

#JavaScript #AbsoluteValue #Math_abs()

๐ŸŒ
MDN
mdn2.netlify.app โ€บ en-us โ€บ docs โ€บ web โ€บ javascript โ€บ reference โ€บ global_objects โ€บ math โ€บ abs
Math.abs() - JavaScript | MDN
The Math.abs() function returns the absolute value of a number. That is, it returns x if x is positive or zero, and the negation of x if x is negative.
๐ŸŒ
Code Highlights
code-hl.com โ€บ home โ€บ javascript โ€บ tutorials
JavaScript Absolute Value - Math.abs() | Code Highlights
December 24, 2023 - In this tutorial, we'll explore how to harness the Math.abs() method to calculate absolute values, ensuring that you're well-equipped to tackle any challenges that come your way. If you're eager to learn more about JavaScript, consider checking out this comprehensive course.
๐ŸŒ
Fueler
fueler.io โ€บ blog โ€บ guide-to-absolute-value-in-javascript
Math.abs(): A Human's Guide to Absolute Value in JavaScript
March 10, 2025 - Learn how to use the Math. bs() function in JS to get the absolute value of a number. This simple yet useful function will return a positive value regardless of whether the input is positive or negative.
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-get-the-absolute-value-of-a-number-in-javascript
How to get the absolute value of a number in JavaScript ยท CoreUI
September 28, 2025 - Use Math.abs() to get the absolute value of a number, removing the sign and returning the magnitude in JavaScript.
๐ŸŒ
LaunchCode
education.launchcode.org โ€บ intro-to-professional-web-dev โ€บ appendices โ€บ math-method-examples โ€บ abs-examples.html
Math.abs Examples โ€” Introduction to Professional Web Development in JavaScript documentation
This method returns the positive value of a number, which can be printed or stored in a variable. abs works on both integer and decimal values. Numerical strings can also be evaluated, but should be avoided as a best practice. ... Math.abs also works on arrays, but to make the process work, we must combine it with the map array method.
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ js โ€บ math_abs.php
JavaScript: Math abs() function
In JavaScript, abs() is a function that is used to return the absolute value of a number. Because the abs() function is a static function of the Math object, it must be invoked through the placeholder object called Math.