How to use absolute value method(Math.abs) in JavaScript
In JavaScript, we can use the Math.abs( )
method to get the absolute value of a given number.
Note: The absolute value of number is never negative.
Examples and Usage
const num = -1233
console.log(Math.abs(num)) // 1233
// The absolute value of -1233 is 1233
console.log(Math.abs(5-8)) // 3
console.log(Math.abs(-1+9)) // 8
The Math.abs()
method returns zero, if you pass an empty array or a string.
console.log(Math.abs('')) // 0
console.log(Math.abs([ ])) // 0