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.
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);