🌐
W3Schools
w3schools.com › jsref › jsref_abs.asp
W3Schools.com
The Math.abs() method returns the absolute value of a number.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-abs-method
JavaScript Math abs() Method - GeeksforGeeks
July 15, 2024 - JS Tutorial · Web Tutorial · A to Z Guide · Projects · OOP · DOM · Set · Map · Math · Number · Boolean · Exercise · Last Updated : 15 Jul, 2024 · Javascript Math.abs() method is used to return the absolute value of a number.
🌐
Learnsic
learnsic.com › blog › the-javascript-absolute-value-method-how-and-when-to-use-it
Learnsic
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
Find elsewhere
🌐
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.
🌐
Math.js
mathjs.org › docs › reference › functions › abs.html
math.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 ·
🌐
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.
🌐
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.
🌐
Leapcell
leapcell.io › blog › understanding-absolute-value-in-javascript
Understanding Absolute Value in JavaScript | Leapcell
July 25, 2025 - Use Math.abs() to get the absolute (non-negative) value of a number in JavaScript.
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.

🌐
Scaler
scaler.com › home › topics › math abs in javascript
Math abs in Javascript - Scaler Topics
May 4, 2023 - The Math.abs() function in javascript returns the absolute value of a number, learn more about Functions in JavaScript on Scaler Topics.
🌐
Code Highlights
code-hl.com › home › javascript › tutorials
JavaScript Absolute Value - Math.abs() | Code Highlights
December 24, 2023 - For instance, in financial applications, the absolute difference between two currency values can be crucial. By now, you should feel comfortable using the Math.abs() method to compute absolute values in JavaScript. This function is both simple and powerful, making it a valuable addition to your coding toolkit.
🌐
Programiz
programiz.com › javascript › library › math › abs
JavaScript Math.abs()
value1 = Math.abs("57"); console.log(value1); // 57 value = Math.abs("-230"); console.log(value); // 230
🌐
Fueler
fueler.io › blog › guide-to-absolute-value-in-javascript
Math.abs(): A Human's Guide to Absolute Value in JavaScript
March 10, 2025 - It gives you the absolute value of any number you pass to it. Say you have a number like -5. If you pass that to Math.abs(), it will return 5.
🌐
p5.js
p5js.org › reference › p5 › abs
abs
Calculates the absolute value of a number. ... Calculates the closest integer value that is greater than or equal to a number. ... Constrains a number between a minimum and maximum value.
🌐
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.
🌐
LaunchCode
education.launchcode.org › intro-to-professional-web-dev › appendices › math-method-examples › abs-examples.html
Math.abs Examples
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.