JavaScript

JavaScript Math.abs() Method | Explained

While programming in JavaScript, you may face the situation where it is required to only use positive numbers. For instance, when the program is about solving inequalities, or any problem related to finding distance. In such scenarios, you can save your precious time and effort by simply invoking the JavaScript “Math.abs()” method and getting the “absolute” value of the specified number.

Don’t know anything about the JavaScript “Math.abs()” method? No worries! This write-up will guide you related to the working of the “Maths.abs()” method. So, let’s start!

JavaScript Math.abs() method

The JavaScript “Math.abs()” method can be utilized to retrieve the absolute value of a number that refers to its distance from the “0” on the number line. “Math.abs()” is a static method of the JavaScript “Math” class, so you should add “Math” as a prefix to execute it.

Syntax

Math.abs(x)

Here, the “Math.abs()” method returns the absolute value of the “x” argument.

Now, let’s check out some examples to understand the working of the JavaScript Math.abs() method.

Example 1

When a positive number is specified as an argument to the JavaScript “Math.abs()” method, it will return the number itself. For instance, we have passed “3” as an argument in the following “Math.abs()” method:

console.log(Math.abs(3));

As the value is “non-negative”, the “Math.abs()” will return the value as it is:

Example 2

For a “negative” number, the “Maths.abs()” method performs the negation and return the absolute value:

console.log(Math.abs(-3));

The above-give “Maths.abs()” method will output the absolute value of “-3” as “3”:

Example 3

For a numeric single element array, the “Math.abs()” method sets the absolute value of the added element as its return case:

console.log(Math.abs([-10]));

Output

Example 4

In case the accepted argument is a numeric string such as “-23”, then the JavaScript “Math.abs()” method will return the absolute value of the number present within the string:

console.log(Math.abs("-23"));

Execution of the given statement will display “23” as the absolute value of “-23”:

Example 5

In the following example, we will execute the JavaScript “Math.abs()” method for a non-numeric string which will result “NaN” (Not a Number):

console.log(Math.abs("linuxhint"));

Output

Example 6

As the value “undefined” is also a non-numeric argument, so upon executing the “Math.abs()” method, the result will be shown as “NaN”:

console.log(Math.abs(undefined));

Output

Example 7

The JavaScript “Maths.abs()” method only works for a non-numeric single element array. That’s the reason if the specified array comprises more than one element, “Math.abs()” method will not process it and return “NaN”:

console.log(Math.abs([4, 5, 7]));

Output

Example 8

When an empty string “” is passed as an argument, the JavaScript “Math.abs()” method return “0” as the absolute value:

console.log(Math.abs(""));

Output

Example 9

Also, for “null” objects, the return case of the “Math.abs()” method is set to “0”:

console.log(Math.abs(null));

Output

Example 10

Similarly, if an empty array “[]” is specified as an argument for the “Maths.abs()” method, then it will return “0”:

console.log(Math.abs([]));

Output

That was essential information about the JavaScript Math.abs() method. You can further research according to your preferences.

Conclusion

The JavaScript Math.abs() method can be utilized to get the absolute value of a number that refers to its distance from the “0” on the number line. For positive, negative numbers, number single element arrays, and numeric strings, the JavaScript Maths.abs() method returns their absolute value, whereas, for non-numeric arguments, it sets its return case as NaN or zero, depending upon the passed value. This write-up discussed the working of the JavaScript Math.abs() method.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.