目录

JavaScript Math.sign()

Example 1

let x = Math.sign(3);
Try it Yourself »

Description

The Math.sign() method retuns whether a number is negative, positive or zero.

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

Example 2

let x = Math.sign(-3);
Try it Yourself »

Example 3

let x = Math.sign(0);
Try it Yourself »

Browser Support

Math.sign() is an ECMAScript6 (ES6) feature.

ES6 (JavaScript 2015) is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

Math.sign() is not supported in Internet Explorer 11 (or earlier).


Syntax

Math.sign( x)

Parameters

Parameter Description
x Required. A number.

Return Value

A Number, representing the sign of the specified number:
  • If the number is positive, it returns 1
  • If the number is negative, it returns -1
  • If the number is positive zero, it returns 0
  • If the number is negative zero, it returns -0
  • If the number is not a number, it returns NaN