JavaScript Math sign()

The JavaScript Math.sign() function returns the sign of a number.

The syntax of the Math.sign() function is:

Math.sign(x)

sign(), being a static method, is called using the Math class name.


Math.sign() Parameters

The Math.sign() function takes in:

  • x - A number

Return value from Math.sign()

Math.sign() returns a number representing the sign of a given number.

  • Returns 1 if x is positive.
  • Returns -1 if x is negative.
  • Returns 0 if x is positive zero.
  • Returns -0 if x is negative zero.
  • Returns NaN for others.

Example: Using Math.sign()

// Using Math.sign()
var value = Math.sign(16);
console.log(value); // 1

var value = Math.sign(-2);
console.log(value); // -1

var value = Math.sign(0);
console.log(value); // 0

var value = Math.sign(-0);
console.log(value); // -0

var value = Math.sign("JS");
console.log(value); // NaN

Output

1
-1
0
-0
NaN

Recommended readings: