Last Updated On By Khizer Ali
JavaScript Absolute Value: Absolute (i.e., abs()) is a Math class object method in JavaScript used to get the absolute value of a number. It takes one parameter as an argument, a number whose absolute value is what we want to find out.
The user will pass the parameter in the method Math.abs(). If the entered value is positive or a negative number, the Math.abs() will always return a positive nearest integer of the value. And when we pass 0 inside the method, it will return 0.
The absolute method is an inbuilt and static function of the Math class in JavaScript.
Table of Contents
Math.abs(number);
Let’s look at an example with different input parameters (like: positive values, negative values, floating-point value, string values, zero, and so on.) to see the different results discussed above.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<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>
<script>
absVal1 = Math.abs(-1);
absVal2 = Math.abs(1);
absVal3 = Math.abs(0);
absVal4 = Math.abs(null);
absVal5 = Math.abs('');
absVal6 = Math.abs('A');
absVal7 = Math.abs([]);
absVal8 = Math.abs([1]);
absVal9 = Math.abs([1,2]);
absVal10 = Math.abs();
absVal11 = Math.abs(undefined);
document.getElementById("demo1").innerHTML = "Math.abs(-1) = " + absVal1;
document.getElementById("demo2").innerHTML = "Math.abs(1) = " + absVal2;
document.getElementById("demo3").innerHTML = "Math.abs(0) = " + absVal3;
document.getElementById("demo4").innerHTML = "Math.abs(null) = " + absVal4;
document.getElementById("demo5").innerHTML = "Math.abs('') = " + absVal5;
document.getElementById("demo6").innerHTML = "Math.abs('A') = " + absVal6;
document.getElementById("demo7").innerHTML = "Math.abs([]) = " + absVal7;
document.getElementById("demo8").innerHTML = "Math.abs([1]) = " + absVal8;
document.getElementById("demo9").innerHTML = "Math.abs([1,2]) = " + absVal9;
document.getElementById("demo10").innerHTML = "Math.abs() = " + absVal10;
document.getElementById("demo11").innerHTML = "Math.abs(undefined) = " + absVal11;
</script>
</body>
</html>
This article discussed the JavaScript Absolute Function in math library, i.e., Math.abs(). It is an inbuilt function and static method of the Mathematical function in JavaScript. This method is used to convert a given number into its absolute value. It will return inappropriate results if the user passes an invalid number in the parameter.
We saw code samples in which we passed different valid and invalid numbers returning different types of results. JavaScript Math class contains a lot more methods dealing with various mathematical operations on the numbers.
Read Also:
JavaScript parseInt() Function [with Coding Examples]
What is the Spread Operator and how to use it in JavaScript?