JavaScript includes Math.sign to find out if a number is positive. It also helps detect negative values or zero.
Table of Content
It tests sign values in code. You do not need extra logic. You use it to sort values or handle direction.
What Is Math.sign in JavaScript?
Math.sign in JavaScript checks the sign of a number. It returns a value for each type. You get 1
for positive numbers and -1
for negative numbers. Zero gives 0
. Negative zero gives -0
. Non-numbers give NaN
.
The syntax looks like this:
Math.sign(x)
x
is any number- Returns one of these values: (
1
,-1
,0
,-0
,NaN
)
The method reads the input and checks its numeric sign. It does not change the value and tells you what sign the value has. It does not round or fix the number.
Use it when you test a number’s direction. It works well if you need to sort or solve math problems.
Take this example:
Math.sign(-5)
This returns -1
. The number is negative, so the method returns -1
. The input stays the same. You use this to handle different outcomes in your code. The function tells you the type without extra math steps.
Examples of Math.sign in JavaScript
Check a Positive Number
Math.sign(42)
This gives 1
because the number is positive. The method checks the value and shows the sign. It does not change the number or do extra work. It just reads the input.
Check a Negative Number
Math.sign(-123)
This returns -1
because the input is below zero. The method shows the sign without any math. You use this to trigger steps for negative values only.
Check Zero
Math.sign(0)
This gives 0
. The input equals zero, so the method tells you that. You may use this to skip or stop code for zero cases.
Check Negative Zero
Math.sign(-0)
This returns -0
. JavaScript treats -0
as a different value from 0
. This lets you catch edge cases that may break your logic later.
Check a String With a Number
Math.sign("10")
This gives 1
. JavaScript converts the string to a number before it checks the sign. The method works if the string holds a valid number.
Check a Non-Numeric String
Math.sign("abc")
This returns NaN
because the input is not a valid number. The method cannot find a sign if it cannot turn the input into a number.
Browser and JavaScript Version Support
Compatibility Across Browsers
- Works in Chrome 38 and above
- Works in Firefox 25 and above
- Works in Safari 8 and above
- Works in Edge 12 and above
- Works in Opera 25 and above
All major browsers support this method. You can use it without extra checks.
Support in Older JavaScript Versions
- Math.sign came in ECMAScript 6 (ES2015)
- It does not work in very old browsers
- You can add a simple fallback with a custom function
Modern environments support Math.sign. You can rely on it in current apps.
Wrapping Up
In this article, you learned how Math.sign in JavaScript checks the sign of any number. You saw how it works and when to use it.
Here’s a quick recap:
- Math.sign tells you whether a number is positive. It also shows if it’s negative or zero.
- It uses one input and gives an output
- You can pass numbers, strings, or zero
- It returns
1
,-1
,0
,-0
, orNaN
- It sorts and controls logic
- You do not need if-else or math checks
- It came in ES6 and runs in modern browsers