Updated June 16, 2023
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 positive or negative, 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 produces an equal positive number. In this article, we will learn about the Math.absolute function, its syntax, usage, working, and some related examples.
Syntax:
Math.abs(numericalValue);
The Math object is a built-in object in JavaScript that contains multiple functions and properties for performing various mathematical operations. 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() way. When the Math.abs() method is given a two-element array, an empty object, a non-numeric string, an empty variable, or an undefined variable as an argument, it returns NaN (Not a Number).
If we pass an empty array, empty string, or even the null value as the parameter to the Math.abs() method, it returns a zero(0) value as the output.
Example of JavaScript Absolute Value
Let us consider an example that will cover all the possible types of parameter values that you can pass to the 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: From the above output, we can observe that all the positive/negative numeric values return their 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 the passed parameter is an array having multiple numbers as the elements, blank object, or string value that cannot be deduced to a numerical value or undefined or left blank as the parameter, then it will return NaN, which is Not a Number value.
Conclusion
In JavaScript, we use the Math.abs() method to obtain the absolute value of a given integer value. However, it is important to exercise caution when using this method, as inappropriate inputs, such as arrays, strings, objects, etc., can result in unexpected 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.
Recommended Articles
We hope that this EDUCBA information on “JavaScript Absolute Value” was beneficial to you. You can view EDUCBA’s recommended articles for more information.