Math.sign() - JavaScript | MDN

Try it

console.log(Math.sign(3));
// Expected output: 1

console.log(Math.sign(-3));
// Expected output: -1

console.log(Math.sign(0));
// Expected output: 0

console.log(Math.sign("-3"));
// Expected output: -1

Syntax

Parameters

x

A number.

Return value

A number representing the sign of x:

  • If x is positive, returns 1.
  • If x is negative, returns -1.
  • If x is positive zero, returns 0.
  • If x is negative zero, returns -0.
  • Otherwise, returns NaN.

Description

Because sign() is a static method of Math, you always use it as Math.sign(), rather than as a method of a Math object you created (Math is not a constructor).

Examples

Using Math.sign()

js

Math.sign(3); // 1
Math.sign(-3); // -1
Math.sign("-3"); // -1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
Math.sign("foo"); // NaN
Math.sign(); // NaN

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-math.sign

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.