Javascript Math abs()

Introduction

The Math.abs() function returns the absolute value of a number.

Math.abs(x)  // x is a number.

abs() is a static method of Math.

Math.abs(x) returns:

Value Return
array with more than one member NaN
empty array 0
empty object NaN
empty string 0
empty variable NaN
non-numeric string NaN
null 0
undefinedNaN
let a = Math.abs('-1');     // 1
console.log(a);//from w w  w  .  ja va 2 s.  co m
a = Math.abs(-2);       // 2
console.log(a);
a = Math.abs(null);     // 0
console.log(a);
a = Math.abs('');       // 0
console.log(a);
a = Math.abs([]);       // 0
console.log(a);
a = Math.abs([2]);      // 2
console.log(a);
a = Math.abs([1,2,3]);  // NaN
console.log(a);
a = Math.abs({});       // NaN
console.log(a);
a = Math.abs('demo');   // NaN
console.log(a);
a = Math.abs();         // NaN
console.log(a);



PreviousNext

Related