Javascript Math sign()

Introduction

The Math.sign() function returns either a positive 1 or negative 1, indicating the sign of the argument.

If the number passed into Math.sign() is 0, it will return a 0.

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

If this argument is not a number, it is implicitly converted to one.

  • If the argument is positive, returns 1.
  • If the argument is negative, returns -1.
  • If the argument is positive zero, returns 0.
  • If the argument is negative zero, returns -0.
  • Otherwise, NaN is returned.
let a = Math.sign(3);     //  1
console.log(a);/*from   www. j av  a  2s  .co m*/
a = Math.sign(-3);    // -1
console.log(a);
a = Math.sign('-3');  // -1
console.log(a);
a = Math.sign(0);     //  0
console.log(a);
a = Math.sign(-0);    // -0
console.log(a);
a = Math.sign(NaN);   // NaN
console.log(a);
a = Math.sign('foo'); // NaN
console.log(a);
a = Math.sign();      // NaN
console.log(a);



PreviousNext

Related