JavaScript Math.abs() Method

Shubham Vora Jan 30, 2023
  1. Syntax of JavaScript Math.abs():
  2. Example Code: Use the Math.abs() Method to Get the Absolute Value of a Given Number
  3. Example Code: Use the Math.abs() Method to Return the Absolute Value of the Subtraction Result
JavaScript Math.abs() Method

In JavaScript, the Math.abs() method is used to obtain the absolute value of the given number. If the number provided is positive, the absolute value will be the same; if the number is negative, the absolute value will be its negation.

Syntax of JavaScript Math.abs():

Math.abs(number);

Parameter

number The number for which we need to find the absolute value.

Return

This method’s return value is the absolute value of the given number. This method will return NaN if the value is a string, an empty object, or an array with more than one value or no value.

Example Code: Use the Math.abs() Method to Get the Absolute Value of a Given Number

In this program, we have taken 4 sets of inputs:

  1. The first set has integer values.
  2. The second set contains a null, an empty value, and an empty array.
  3. The third set consists of an empty object, string, two elements array, and no argument.
  4. The final set is the Math function’s NEGATIVE_INFINITY.

These inputs are processed by the Math.abs() method, and we get the output displayed below.

let x1 = "3.42";
let x2 = "-3.42";
console.log("Math.abs(3.42) = " + Math.abs(x1));
console.log("Math.abs(-3.42) = " + Math.abs(x2));
let n1 = "null";
let n2 = "''";
let n3 = "[]";
console.log("Math.abs(null) = " + Math.abs(n1));
console.log("Math.abs('') = " + Math.abs(n2));
console.log("Math.abs([]) = " + Math.abs(n3));
let p1 = "'Tutorial'";
let p2 = "{}";
let p3 = "[9,1]";
console.log("Math.abs(Tutorial) = " + Math.abs(p1));
console.log("Math.abs({}) = " + Math.abs(p2));
console.log("Math.abs([9,1]) = " + Math.abs(p3));
let a1 = "Number.NEGATIVE_INFINITY";
console.log("The absolute of Number.NEGATIVE_INFINITY = " + Math.abs(a1));

Output:

Math.abs(3.42) = 3.42
Math.abs(-3.42) = 3.42
Math.abs(null) = NaN
Math.abs('') = NaN
Math.abs([]) = NaN
Math.abs(Tutorial) = NaN
Math.abs({}) = NaN
Math.abs([9,1]) = NaN
The absolute of Number.NEGATIVE_INFINITY = NaN

Example Code: Use the Math.abs() Method to Return the Absolute Value of the Subtraction Result

In this example, we have taken the input set to perform subtraction. For example, we give two inputs, say x and y.

Math.abs(x-y) performs the subtraction and returns the absolute value.

function doDiff(x, y) {
    return Math.abs(x - y);
}

let x = 2;
let y = 4;
console.log("Math.abs(2 - 4) = " + doDiff(x, y));

x = 4;
y = 2;
console.log("Math.abs(4 - 2) = " + doDiff(x, y));

x = 2.1234;
y = 4.5432;
console.log("Math.abs(2.1234 - 4.5432) = " + doDiff(x, y));

Output:

Math.abs(2 - 4) = 2
Math.abs(4 - 2) = 2
Math.abs(2.1234 - 4.5432) = 2.4197999999999995

The Math.abs() method is a built-in function of the Math object; hence, we must use it as it is. We have understood various outputs that this method returns when distinct inputs are given.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - JavaScript Math