Kodeclik Blog
How to find the Absolute Value in Javascript
You likely want to at some point find the absolute value of a number in Javascript. What is the absolute value you ask?
The absolute value of a number is its distance from zero on the number line, regardless of whether the number is positive or negative. In other words, it is always a non-negative value.
The absolute value concept is widely used in math and programming, and various fields of science to represent magnitudes without considering direction.
Below are three different ways to compute the absolute value of a number in JavaScript.
Method 1: Using the Built-In Math.abs() Function
The Math.abs() function in JavaScript returns the absolute value of a number. This means it will always return a non-negative value, regardless of whether the input number is positive or negative. If the input is not a number, it returns NaN.
const number = -10;
const absoluteValue = Math.abs(number);
console.log(absoluteValue); // Outputs: 10
In this approach, the built-in Math.abs() function is used to directly compute the absolute value of a given number. This method is concise and efficient, as Math.abs() internally handles all necessary checks for whether the number is negative, zero, or positive, and returns its non-negative magnitude. It is the simplest and most commonly used method for obtaining the absolute value in JavaScript.
Method 2: Writing your own Custom Function with an if/else Statement
This is our DIY (Do it yourself) way!
function getAbsoluteValue(num) {
if (num < 0) {
return -num;
} else {
return num;
}
}
console.log(getAbsoluteValue(-10)); // Outputs: 10
In this method, a custom function named getAbsoluteValue is defined to manually compute the absolute value. The function checks if the input number is less than zero; if it is, it multiplies the number by -1 to convert it to a positive value. If the number is zero or already positive, it returns the number as is. This explicit approach makes the logic clear and can be easily modified or extended for additional functionality if needed.
Method 3: Use the Square and Square Root Approach
This is a little bit of a mathy approach!
function absValueUsingSqrt(num) {
return Math.sqrt(num * num);
}
console.log(absValueUsingSqrt(-10)); // Outputs: 10
This method computes the absolute value by first squaring the number, which always results in a non-negative value, and then taking the square root of that squared number. The mathematical operations of squaring and then taking the square root effectively remove any negative sign, yielding the absolute value of the original number. Although this approach is slightly more computationally intensive than using Math.abs(), it demonstrates an alternative way to achieve the same result using basic arithmetic operations.
What are the different applications of absolute value?
Here are numerous areas where you will need to calculate the absolute value.
Want to learn Javascript with us? Sign up for 1:1 or small group classes.