JavaScript sign() method

Definition and usage

sign() The method checks whether the number is negative, positive, or zero.

  • If the number is positive, this method returns 1.
  • If the number is negative, returns -1.
  • If the number is zero, returns 0.

Example

Determine whether a number is negative or positive:

var a = Math.sign(3);    // Returns 1 (positive)
var b = Math.sign(-3);   // Returns -1 (negative)
var c = Math.sign(0);    // Returns 0 (zero)

Try it yourself

Syntax

Math.sign(x)

Parameter value

Parameter Description
x Required. Number.

Technical details

Return value:

Number, indicating the sign of the specified number:

  • If the number is positive, returns 1
  • If the number is negative, returns -1
  • If the number is positive zero, returns 0
  • If the number is negative zero, returns -0
  • If not a number, returns NaN
JavaScript version: ECMAScript 2015

Browserstøtte

Metoder Chrome Edge Firefox Safari Opera
sign() 38.0 12.0 25.0 9.0 25.0

Relaterede sider

Tutorial:JavaScript matematik