Get the absolute value of a number in JavaScript
Question:
How to get the absolute value of a number in JavaScript? Answer:
const num1 = Math.abs(-17);
const num2 = Math.abs(23);
console.log(num1 + ' / ' + num2);
Description:
To get the absolute value of a number in JavaScript, simply use theMath.abs
funtion. The Math.abs()
function returns the absolute value of a number. That is, it returns x if x is positive or zero, and the negation of x if x is negative.
If the parameter is an empty object, an array with more than one member, a non-numeric string or undefined/empty variable returns NaN
. If the parameter is NaN, an empty string or an empty array returns 0.
Reference:
Math.abs reference
Share "How to get the absolute value of a number in JavaScript?"
Related snippets:
- Check if number is odd or even in JavaScript
- Round a number in JavaScript
- Get the absolute value of a number in JavaScript
- Generate random date with JavaScript
- Generate random string in JavaScript
- Generate a random number with fixed length using JavaScript
- Generate a random number in a range with JavaScript
- Get the minimum value in an array using JavaScript
- Get the maximum value in an array using JavaScript
- Convert a float number to integer in JavaScript
- Round a number to the nearest 10 using JavaScript
- Round down a number in JavaScript
- Round up a number in JavaScript
Tags:
absolute, abs, get absolute value, find absolute value, calculate absolute value Technical term:
Get the absolute value of a number in JavaScript