How to get the absolute value of a number in JavaScript?


The space on the number line between any integer and zero is known as its absolute value. No negative values exist because it is the distance. This tutorial will help you find a number's absolute value in JavaScript.

Next, we will see the method to find the absolute value of a number in JavaScript.

Using Math.abs()

A number's absolute value is returned using the Math.abs() function. If n is positive or zero, it returns n, and if n is negative, it returns the negation of n. The Math.abs() is a static function of Math. Hence, we use it as it is.

Syntax

Math.abs(n)

The abs() method returns 0 if the n value is ‘’, built-in an empty array. The method returns NaN if the value is a string, an empty object, or an array with more than one value or no value.

The method is compatible with Google Chrome, node.js, Samsung internet, Safari on ios, Opera for Android, Firefox for Android, Chrome for Android, Android web view, Safari, Opera, Firefox, and Internet explorer. The method is the feature of ECMAScript1(ES1).

Parameters

  • n − It is the number for which we need to find the absolute value.

Algorithm

Step 1 − Assign a different set of input to pass as an argument to Math.abs().

Step 2 − Do Math.abs() of all the inputs.

Step 3 − Store the return value or display the output.

Example 1

In this program, we have taken 4 sets of inputs. The first set has integer values. The second set contains thanks, an empty value, and an empty array. The third set consists of an empty object, string, two elements array and no argument. The final set is the Math function's NEGATIVE_INFINITY. These inputs are processed by Math.abs() and we get the output below.

<html> <body> <h3> Finding the absolute value of a number in JavaScript using <i> Math.abs() </i> </h3> <p id="absDisp1"> </p> <p id="absDisp2"> </p> <p id="absDisp3"> </p> <p id="absDisp4"> </p> <script> let x1 = Math.abs(3.42); let x2 = Math.abs(-3.42); let x3 = Math.abs(-9); let x4 = Math.abs(0); let x5 = Math.abs(2); let n1 = Math.abs(null); let n2 = Math.abs(''); let n3 = Math.abs([]); let p1 = Math.abs("Tutorial"); let p2 = Math.abs(); let p3 = Math.abs({}); let p4 = Math.abs([9, 1]); let a1 = Math.abs(Number.NEGATIVE_INFINITY); var absDispEl1 = document.getElementById("absDisp1"); absDispEl1.innerHTML = "Math.abs(3.42) = " + x1 + " <br > " + " Math.abs(-3.42) = " + x2 + " <br > " + "Math.abs(-9) = " + x3 + " <br > " + "Math.abs(0) = " + x4 + " <br> " + "Math.abs(2) = " + x5 + " <br > "; var absDispEl2 = document.getElementById("absDisp2"); absDispEl2.innerHTML = "Math.abs(null) = " + n1 + "<br>" + "Math.abs('') = " + n2 + "<br>" + "Math.abs([]) = " + n3 + "<br>"; var absDispEl3 = document.getElementById("absDisp3"); absDispEl3.innerHTML = "Math.abs('Tutorial') = " + p1 + "<br>" + "Math.abs() = " + p2 + "<br>" + "Math.abs({}) = " + p3 + "<br>" +"Math.abs([9,1]) = " + p4 + " <br> "; var absDispEl4 = document.getElementById("absDisp4"); absDispEl4.innerHTML = "Math.abs( Number.NEGATIVE_INFINITY ) = " + a1; </script> </body> </html>

Example 2

In this example, we have taken the input set to perform subtraction. For example, we give two inputs, say x and y. Math.abs(x-y) does the subtraction and returns the absolute value.

<html> <body> <h4> Finding the absolute value of difference of two numbers using <i> Math.abs() </i> Math operation.</h4> <p id="absNumOut"> </p> <script> function doDiff(x, y) { return Math.abs(x - y); } var absNumOutEl = document.getElementById("absNumOut"); absNumOutEl.innerHTML = "absolute(2-6) = " + doDiff(2, 6) + " <br> " + " absolute(4 - 2) = " + doDiff(4,2) + " <br > " + "absolute(2.12557- 8.99682) = " + doDiff(2.12557, 8.99682); </script> </body> </html>

This tutorial guided us to learn Math.abs() to find the absolute of a number in JavaScript. This is a built-in feature of the Math function; hence, we must use it as it is. We have understood various outputs that this method returns when distinct inputs are given. This method also performs mathematics operations as well and returns the absolute value of it.

Updated on: 31-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements