## Introduction of JavaScript Absolute Value

Javascript absolute is the method of the Math object in Javascript that helps us to retrieve the absolute value of the number. Whether the passed number is a positive number or negative number, the Math.abs() function always returns the equivalent numerical value with no negative sign. That means irrespective of whether we pass a positive or negative number it always returns the equivalent positive number. When 0 (zero) number is passed to the Math.abs() function then it will return the zero value itself. Math.abs() method was introduced and came into usage from the ECMAScript 1 version of the Javascript version. In this article, we will learn about the Math.absolute function, its syntax, usage, working, and some examples related to it.

**Syntax:**

`Math.abs(numericalValue);`

The syntax of Javascript absolute function is defined below:

Math is the object that is present in Javascript that has multiple functions and properties that are defined in it that help to carry out various mathematical operations. abs are one such function present in the Math object and it is the static method. Hence, whenever, you want to use the abs() function then you will need to mention the Math word before the abs() method separated by. Such as Math.abs();

**numericalValue**: It is any number whose absolute value is to be retrieved.**ReturnValue**: It is the absolute value of any numericalValue that is passed as the parameter to the abs() function.

### Working and Usage of JavaScript Absolute Value

Math is an object in javascript and not a constructor while abs() is one of its static methods. Hence, we always use the absolute method using the Math.abs() method. Whenever an array having two or more elements or an empty object is used or in case if the non-numeric string is supplied as the parameter to the Math.abs() method or even in case if an empty variable or undefined variable is supplied as the parameter to abs() method then the return value of the absolute function is Nan that stands for Not a Number value.

In case, if we pass an empty array or empty string or even the null value as the parameter to Math.abs() method, it returns zero(0) value as the output. In all other cases where a numerical non-zero positive or negative value is passed as a parameter, the Math.abs() function will return the equivalent positive numerical value.

### Example of JavaScript Absolute Value

Let us consider an example that will cover all the possible type of parameter values that you can pass to Math.abs() method as a parameter in the following example:

**Code:**

`<!DOCTYPE html>`

<html>

<body>

<h2>JavascriptMath.abs() method demonstration</h2>

<p id="demo1"></p>

<p id="demo2"></p>

<p id="demo3"></p>

<p id="demo4"></p>

<p id="demo5"></p>

<p id="demo6"></p>

<p id="demo7"></p>

<p id="demo8"></p>

<p id="demo9"></p>

<p id="demo10"></p>

<p id="demo11"></p>

<p id="demo12"></p>

<script>

// Any negative or positive number in the string format

document.getElementById("demo1").innerHTML = "Absolute value of string having numerical value -15 when evaluated using Math.abs() method is = "+ Math.abs('-15');

// Any negative number

document.getElementById("demo2").innerHTML = "Absolute value ofnegative number -29 when evaluated using Math.abs() method is = "+ Math.abs(-29);

// null value

document.getElementById("demo3").innerHTML = "Absolute value of null value when evaluated using Math.abs() method is = "+ Math.abs(null);

// blank string

document.getElementById("demo4").innerHTML = "Absolute value of blank string when evaluated using Math.abs() method is = "+ Math.abs('');

// blank array

document.getElementById("demo5").innerHTML = "Absolute value of blank array when evaluated using Math.abs() method is = "+ Math.abs([]);

//Array containing single numerical value in it

document.getElementById("demo6").innerHTML = "Absolute value of single number value in an array when evaluated using Math.abs() method is = "+ Math.abs([2]);

//Array containing Multiple Numbers in it as elements

document.getElementById("demo7").innerHTML = "Absolute value of multiple number value in an array when evaluated using Math.abs() method is = "+ Math.abs([12,23]);

// Blank Object

document.getElementById("demo8").innerHTML = "Absolute value of blank object when evaluated using Math.abs() method is = "+ Math.abs({});

// Any string value

document.getElementById("demo9").innerHTML = "Absolute value of string value when evaluated using Math.abs() method is = "+ Math.abs('string');

// With no parameter value

document.getElementById("demo10").innerHTML = "Absolute value of no parameter when evaluated using Math.abs() method is = "+ Math.abs();

// With 0 (zero) as the parameter value

document.getElementById("demo11").innerHTML = "Absolute value of zero value when evaluated using Math.abs() method is = "+ Math.max(0);

// With undefined value

document.getElementById("demo12").innerHTML = "Absolute value of undefined value when evaluated using Math.abs() method is = "+ Math.max(undefined);

</script>

</body>

</html>

**Output:**

**Explanation:** We can observe from the above output that all the positive/negative numeric values return its equivalent positive value as the absolute value. In the case of zero, null or blank string or a blank array as the parameter to the Math.abs() method will return a zero value (0) as the output. While in case if the passed parameter is an array having multiple numbers as the elements, blank object, or the string value that cannot be deduced to a numerical value or undefined or left blank as the parameter then it will return NaN that is Not a Number value.

### Conclusion

Math.abs() method is used in javascript to get the absolute value of the passed integer value. We need to be careful while using it as sometimes when inappropriate values such as arrays, strings, objects, etc are used then it may lead to inappropriate outputs. We always need to call the abs() function by using the Math keyword pretended to it as in the Math.abs() method. Also, we need to consider the browser compatibility and support for the Math.abs() function while using it in your context. This method was introduced in the ECMAScript 1 version of the Javascript version for usage.

### Recommended Articles

This is a guide to JavaScript Absolute Value. Here we discuss the introduction and working and usage of javascript absolute value along with an example and its code implementation. You may also have a look at the following articles to learn more –