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
W3Schools.com
The Math.abs() method returns the absolute value of a number.
Discussions

Which is faster: Math.abs(value) or value * -1 ?
Pretty straightforward, but I just want to know which is faster. I think simply multiplying a number by -1 is much faster than calling a predefined method, provided that you are sure that value is More on stackoverflow.com
🌐 stackoverflow.com
What is math.abs exactly doing?
What is math.abs exactly doing? It does return the absolute value then 1.33333 might be 1 and 1.99999 might be 2. Or am I wrong. but when does it round the absolute value? More on devforum.roblox.com
🌐 devforum.roblox.com
1
3
April 4, 2021
What is the real purpose of an absolute value?

The absolute value of the difference between two numbers (like |x-y|) gives the distance between two real numbers on the number line. This is important especially in calculus and real analysis. To give you some terms to look up if you're interested, the absolute value is a metric if we want to treat the real numbers as a metric space (technically incorrect, it is the absolute difference of two numbers which is a metric), and it is an example of a norm if we want to treat the real numbers as a vector space over themselves. Hope that answers your question!

More on reddit.com
🌐 r/math
27
8
March 25, 2013
Should I be using Math.abs?
I'm pretty new to java myself, but doesn't Math.abs return the absolute value, so it returns negative numbers as positive? I'd have gone with else if (18 < age > 24) More on reddit.com
🌐 r/learnjava
4
1
July 29, 2015
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.math.abs
Math.Abs Method (System) | Microsoft Learn
Public Shared Function Abs (value As IntPtr) As IntPtr · value · IntPtr · nint · nativeint · A number that is greater than MinValue, but less than or equal to MaxValue. IntPtr · nativeint · A native signed integer, x, such that 0 ≤ ...
🌐
W3Schools
w3schools.com › java › ref_math_abs.asp
Java Math abs() Method
The abs() method returns the absolute (positive) value of a number.
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.

🌐
Electricimp
developer.electricimp.com › squirrel › math › abs
math.abs() | Dev Center
This function returns the absolute value of the passed number: its magnitude without the sign. Negative numbers become positive; positive numbers stay as they are. So the absolute value of -10 is 10, while the absolute value of 42 is 42. This function returns an integer, so if -10.014 is passed, ...
Find elsewhere
🌐
LogScale
library.humio.com › data-analysis › functions-math-abs.html
math:abs() | Data Analysis 1.220.0-1.230.0 | LogScale Documentation
In this example, the math:abs() function is used to calculate the absolute value of a negative number, demonstrating how it returns the positive version of that number.
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › math › abs
abs()
April 25, 2025 - Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure · For controlling the Arduino board and performing computations
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-abs-method-examples
Java Math abs() method with Examples - GeeksforGeeks
July 11, 2025 - The java.lang.Math.abs() returns the absolute value of a given argument.
🌐
Real Python
realpython.com › ref › builtin-functions › abs
abs() | Python’s Built-in Functions – Real Python
The abs() function in Python returns a number’s absolute value, which is its distance from zero on the number line, regardless of its sign.
🌐
Wikipedia
en.wikipedia.org › wiki › Absolute_value
Absolute value - Wikipedia
2 weeks ago - {\displaystyle |0|=0} . For example, the absolute value of 3 is 3, and the absolute value of −3 is also 3. The absolute value of a number may be thought of as its distance from zero.
🌐
Codecademy
codecademy.com › docs › c# › math functions › .abs()
C# (C Sharp) | Math Functions | .Abs() | Codecademy
November 19, 2025 - The C# Math.Abs() method is a static method that returns the absolute value of a specified number.
🌐
CodeGym
codegym.cc › java blog › java math › java math abs() method
Java Math abs() method
January 9, 2025 - Whereas, the absolute of -5 is also 5. The java.lang.Math class provides a static method Math.abs(parameter) to find the “absolute value” of the parameter. So, if you pass any positive number let’s say Math.abs(5) it will return 5.
🌐
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 ·
🌐
InfluxData Documentation
docs.influxdata.com › flux › v0 › stdlib › math › abs
math.abs() function | Flux Documentation
math.abs() returns the absolute value of x. (x: float) => float · For more information, see Function type signatures. (Required) Value to operate on. Return the absolute value · Use math.abs in map · math.abs(x: -1.22)// 1.22 · sampledata.float() |> map(fn: (r) => ({r with _value: math.abs(x: ...
🌐
Programmers
programmers.io › home › blog › absolute value abs () in java
Absolute Value Abs () In Java - Programmers.io
June 26, 2025 - For int and long: Returns the absolute value of the number. If the input is the most negative number (Integer.MIN_VALUE or Long.MIN_VALUE), the result remains negative due to overflow.