- Instant help with your JavaScript coding problems

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?"