JavaScript Math sign()

The sign() method computes the sign of the specified number and returns 1 if the number is positive and -1 if it's negative.

Example


sign() Syntax

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

Math.sign(number)

Here, sign() is a static method. Hence, we are accessing the method using the class name, Math.


sign() Parameter

The Math.sign() method takes a single parameter:

  • number - value whose sign is to be determined

sign() Return Value

The sign() method returns:

  • 1 if the argument is positive
  • -1 if the argument is negative
  • NaN (Not a Number) for a non-numeric argument

Example 1: JavaScript Math.sign()

Here, the Math.sign() returns

  • -1 - for the negative number -27
  • 1 - for the positive number 16

Example 2: Math.sign() with Non-Numeric Arguments

In the above example, we have used the Math.sign() method with a string value "Harry". Hence, we get NaN as output.


Example 3: Math.sign() with Zero Values


Recommended readings: