JavaScript Math.sign() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

Math.sign(x) tells you the sign of a number. It returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.

Read the syntax below, then try the three examples to see the method in action.

Syntax

Call the method on Math. Start with this line.

JavaScript
Math.sign(x)
CallWhat it does
Math.sign(x)Returns 1, -1, 0, -0 or NaN.

Built into JavaScript. Call Math.sign(...) on the Math object — no import required. Math is not a constructor, so never write new Math().

Library declarations
JavaScript
Math.sign(x)

Parameters

ParameterDescription
xRequired. The number to check. Other values are converted to a number first.

Return Value

Return ValueDescription
Number1 if x is positive, -1 if x is negative, 0 or -0 if x is zero, and NaN if x is not a number.

JavaScript Examples

Example 1 — Positive, Negative and Zero

The result tells you which side of zero the number is on.

JavaScript
console.log(Math.sign(25)); // 1
console.log(Math.sign(-7)); // -1
console.log(Math.sign(0)); // 0
Try It Yourself

How It Works

  1. Positive. 25 is above zero, so the sign is 1.
  2. Negative. -7 is below zero, so the sign is -1.
  3. Zero. 0 stays 0.
  4. Size does not matter. Math.sign(1000000) is still just 1.

Example 2 — Minus Zero, NaN and Text

Math.sign keeps minus zero, and it converts other values to numbers first.

JavaScript
console.log(Math.sign(-0)); // -0
console.log(Math.sign(NaN)); // NaN
console.log(Math.sign("5")); // 1
console.log(Math.sign("abc")); // NaN
Try It Yourself

How It Works

  1. Minus zero. -0 is kept as -0.
  2. NaN in, NaN out. Math.sign(NaN) is NaN.
  3. Text numbers. "5" becomes 5, so the sign is 1.
  4. Bad text. "abc" becomes NaN.

Example 3 — Describe a Number in Words

A small function uses the sign to return a friendly label.

JavaScript
function describe(n) {
  const s = Math.sign(n);
  if (s === 1) return "positive";
  if (s === -1) return "negative";
  if (s === 0) return "zero";
  return "not a number";
}

console.log(describe(12)); // positive
console.log(describe(-3)); // negative
console.log(describe(0)); // zero
console.log(describe("hi")); // not a number
Try It Yourself

How It Works

  1. Get the sign. Math.sign(n) is stored in s.
  2. Check 1 and -1. Positive and negative numbers return their label.
  3. Check zero. 0 === 0 is true, so zero returns "zero".
  4. Fallback. NaN matches none of the checks, so the last line returns "not a number".

Browser & Runtime Support

Math.sign was added in ES2015 and is Baseline Widely available in modern browsers and Node.js. It does not work in Internet Explorer.

✓ Baseline · ES2015

Math.sign

Supported in Chrome, Firefox, Safari, Edge and Node.js for years. Only very old browsers such as Internet Explorer lack it.

98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Math.sign Excellent

Bottom line: Use Math.sign in modern code. Add a small polyfill only if you must support Internet Explorer.

Frequently Asked Questions

It returns 1 for positive numbers, -1 for negative numbers, 0 for zero, -0 for minus zero, and NaN for values that are not numbers.
No. It only tells you the sign. Math.sign(500) is 1, not 500.
Math.sign(0) is 0 and Math.sign(-0) is -0. Most code treats them as the same, but Object.is can tell them apart.
It returns NaN because the text cannot be converted to a number.
No. x > 0 gives true or false. Math.sign(x) gives 1, -1, 0 or NaN, which is handy for sorting and direction.
All modern browsers and Node.js. It was added in ES2015. Internet Explorer does not support it.

Next: sin()

Learn how to sine of an angle in radians.

sin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful