How to Find Absolute Value in JavaScript

The abs() is a static math method; it can be used without creating an object. However, the Math module is often helpful when the program deals with mathematical expressions where the |x| value (the absolute value of a variable x) must be calculated.

JavaScript absolute value

To find the absolute value in JavaScript, use the Math.abs() method. The Math.abs() is a built-in method that returns the absolute value of a number. The abs() method takes a number as its parameter and returns its absolute value. 

Syntax

Math.abs(x)

Parameters

The variable x, whose absolute value is to be determined.

Return Value

If the argument is not negative, it returns the value of the argument as it is. Otherwise, it returns the negation of that value.

See the following figure.

Javascript abs() Method

 

Note:

  • If the parameter is negative infinity, this method returns positive infinity.
  • If the parameter is a non-numeric string, this method returns NaN.
  • If the parameter is an array with more than one integer, this method returns NaN.
  • If the parameter is an undefined/empty variable, this method returns NaN.
  • If the parameter is an empty object, this method returns NaN.
  • If the parameter is null, this method returns 0.
  • If the parameter is an empty string, this method returns 0.
  • If the parameter is an empty array, this method returns 0.

Compatibility:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Android webview
  • Chrome for Android
  • Firefox for Android
  • Opera for Android
  • Safari on iOS
  • Samsung Internet
  • Node.js

JavaScript version: ECMAScript 1

Consider the following examples:

The following example demonstrates the use of this method.

// example1.js

var a = 500; // non-negative number
var b = -500; // negative number

console.log(Math.abs(a));
console.log(Math.abs(b));

Output

node example1
500
500

Example 2

The following example demonstrates the case where negative infinity is passed as a parameter.

// example2.js

console.log(Number.NEGATIVE_INFINITY)
console.log(Math.abs(Number.NEGATIVE_INFINITY));

Output

node example2
-Infinity
Infinity

Example 3

The following example demonstrates the cases where NaN is returned.

// example3.js

var a = "JavaScript"; // non-numeric string
var b = [1, 2, 3, 4]; // array with more than one integer
var c; // undefined variable
var d = {}; // empty object


console.log(Math.abs(a));
console.log(Math.abs(b));
console.log(Math.abs(c));
console.log(Math.abs(d));
console.log(Math.abs());

Output

node example3
NaN
NaN
NaN
NaN
NaN

Example 4

The following example demonstrates the cases where 0 is returned.

// example4.js


var a = null; 			// null
var b = ""; 			// empty string
var c = [];			// empty array


console.log(Math.abs(a));
console.log(Math.abs(b));
console.log(Math.abs(c));

Output

node example4
0
0
0

Conclusion

To find absolute value in Javascript, then you should use math.abs() function.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.