Math.abs() javascript

In this article, we will cover the basics of using math abs javascript functions. This helpful function returns whether an argument evaluates positive or negative values depending on its sign (abs(x)). It works for numbers only- so you’ll need to convert strings into real numbers before applying any calculations involving them with Math.

What is Math abs javascript?

The math abs javascript function is a math function that can use in JavaScript to return the absolute value of a number. It returns x if x is positive or zero, and the negation of x if x is negative. This math function only applies to numbers, not strings – so you’ll need to convert your string first before using math.abs(). The math.abs() function takes one argument: the number whose absolute value you want to find out about this function!


The math.abs() function takes one argument: the number whose absolute value you want to find out!

Return type:

The absolute value of a number is an important concept to know if your calculations and estimations are accurate. This function returns the magnitude (or distance from positive) in integer form. The output should not have any extra words added to it.

Sample Code:

Math.abs(-100) // will return 100 
math.abs(0) // will return 0 
math.abs("hahahah")//will return NaN, because the string is not a number so can't be used in calculations




<title> math abs javascript </title>

console.log(Math.abs('-1')); // return 1
console.log(Math.abs(NULL)); // return 0
console.log(Math.abs([])); // return 0
console.log(Math.abs([1,2])); // return NAN
console.log(Math.abs('string')); // return NAN
console.log(Math.abs()); // return NAN

How Math abs javascript function works:

To find the absolute value of any number, we have a specified method in Javascript referred to as abs present inside java.lang Math class. Absolute value refers to the corresponding positive representation for whatever is being computed, even if it’s negative! Now you may be wondering what this means, so by simply passing an argument with either a negative or positive value, you will get the corresponding output.

For instance, if we pass -90, which is a relatively large number with the Absolute value of 90, this math function returns 90 because it’s not very big but a positive integer instead! However, for example, let us consider passing 0 (zero) to our math abs javascript function- like I said before, you can’t use math.abs() with strings; so the result will be 0 since it’s neither negative nor positive integer!

What happens If argument double or float type:

  • A positive zero or negative zero is equivalent to a regular number with the opposite sign. A result of – 0 will be converted into 0;
  • Infinite processes lead to positive infinity.
  • If the argument is NaN, then, of course, the result will also be NaN.

Since its invention in ancient India, math has been a part of our lives. The ancient Indian mathematicians used what we now call the Pythagoras theorem and trigonometry. However, math is not just about these major mathematical events in history – it’s also essential to know how math functions work with programming languages such as JavaScript (JS).


There are many different ways to use Math.abs() to do various things, depending on what you need it for. It’s also important not to forget that this function can only be applied with numbers and will return an absolute value depending on the sign of the input argument. If you have any questions about implementing these functions in your work, please don’t hesitate to contact us! Our experts are always happy to help!

Also, read Javascript max()

Follow us on Facebook

Browser Support

Mobile Browsers

ChromeEdgeFirefoxInternet ExplorerOperaSafariNode.js
Full support 45Full support 12Full support
Full support
Full support
Full support
Partial supportPartial

Desktop Browsers

WebView AndroidChrome AndroidFirefox AndroidOpera AndroidSafari on iOSSamsung Internet
Full support

Leave a Comment