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 ...
May 29, 2024 - This website uses cookies to ensure you get the best experience & by continuing to use our website, you agree to our Privacy and Cookie Policy · Got it
🌐
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.