AppDividend
Latest Code Tutorials

Javascript abs() Function | Math.abs() in Javascript Example

0

Javascript Math.abs() is an inbuilt function that returns the absolute value of a number. It takes a number as its parameter and returns its absolute value. Parameters: The number whose absolute value is to be found is passed as the parameter to the math.abs() function.

Javascript abs() Function

To find the absolute value of a given argument in JavaScript, Math.abs() method is used. This method is often helpful when the program deals with mathematical expressions where the |x| value (the absolute value of a variable x) is required to be calculated. Since abs() is a static method of Math, it can be used without creating an object.

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

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

See also

Javascript math round()

Javascript math random()

Leave A Reply

Your email address will not be published.

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