JavaScript Math.abs() Method
You are Here:
JavaScript Math.abs() Method
The Math.abs()
method returns the absolute value of the given number.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var result = Math.abs(-125.50);
document.write(result);
</script>
</body>
</html>
Syntax
Math.abs(x)
Parameter Values
Value | Type | Explanation |
---|---|---|
x | Required | Specifies a number. |
Return Value
Value | Explanation |
---|---|
Number | Returns the absolute value of the given number. |
More Examples
Example
HTML Online Editor
Math.abs("String"); // NaN
Math.abs(null); // 0
Math.abs(1+2); // 3
Math.abs(-125.50); // 125.5
Math.abs(125.50); // 125.5
In the following example, we will find the difference between any two given numbers using Math.abs() method.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
function difference(a, b){
return Math.abs(a - b);
}
console.log(difference(-1, 5));
console.log(difference(125.5, -150));
console.log(difference(5, 10));
console.log(difference(10, 5));
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.