Home JavaScript Math Methods sign() JavaScript Math.sign() 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.
Call What 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 Parameters Parameter Description x Required. The number to check. Other values are converted to a number first.
Return Value Return Value Description Number 1 if x is positive, -1 if x is negative, 0 or -0 if x is zero, and NaN if x is not a number.
Important
The result is always 1, -1, 0, -0 or NaN. It does not return the number itself. Math.sign was added in ES2015.
JavaScript Examples 1. Positive, negative, zero 2. -0, NaN and text 3. Describe a number Example 1 — Positive, Negative and Zero The result tells you which side of zero the number is on.
console.log(Math.sign(25)); // 1
console.log(Math.sign(-7)); // -1
console.log(Math.sign(0)); // 0 How It Works Positive. 25 is above zero, so the sign is 1.Negative. -7 is below zero, so the sign is -1.Zero. 0 stays 0.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.
console.log(Math.sign(-0)); // -0
console.log(Math.sign(NaN)); // NaN
console.log(Math.sign("5")); // 1
console.log(Math.sign("abc")); // NaN How It Works Minus zero. -0 is kept as -0.NaN in, NaN out. Math.sign(NaN) is NaN.Text numbers. "5" becomes 5, so the sign is 1.Bad text. "abc" becomes NaN.Example 3 — Describe a Number in Words A small function uses the sign to return a friendly label.
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 positive
negative
zero
not a number How It Works Get the sign. Math.sign(n) is stored in s.Check 1 and -1. Positive and negative numbers return their label.Check zero. 0 === 0 is true, so zero returns "zero".Fallback. NaN matches none of the checks, so the last line returns "not a number".Compatibility
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 What does Math.sign() return? 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.
Does Math.sign() change my number? No. It only tells you the sign. Math.sign(500) is 1, not 500.
How is Math.sign(0) different from Math.sign(-0)? 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.
What does Math.sign("abc") return? It returns NaN because the text cannot be converted to a number.
Is Math.sign() the same as x > 0? No. x > 0 gives true or false. Math.sign(x) gives 1, -1, 0 or NaN, which is handy for sorting and direction.
Which browsers support Math.sign()? All modern browsers and Node.js. It was added in ES2015. Internet Explorer does not support it.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion