ES6 method returns the sign of a number, indicating whether the number is positive, negative, or zero.
It's easy to use:
-
Math.sign(6) // 1
-
Math.sign(-6) // -1
-
Math.sign(0) // 0
return value
()
There are 5 possible return values:
-
1 // Positive
-
-1 // Negative numbers
-
0 // positive zero
-
-0 // Negative zero
-
NaN // Not a number
-
-
Math.sign(8) // 1
-
Math.sign(-8) // -1
-
-
Math.sign(0) // 0
-
Math.sign(-0) // -0
-
-
Math.sign(NaN) // NaN
-
Math.sign('hello') // NaN
-
Math.sign() //NaN
take note of: The parameters passed to this method willimplicit conversionbecause of
number
Type.
Common mistakes are Returns the converted parameter value
Returns only the sign of the number. It does not return values.
A common mistake is to assume that Returns the converted parameter values. And the fact that the
Returns only the sign of the number. It does not return values.
Math.sign(-8) // -1
As you can see, it returns the-1
Instead of returning-8
。
with the comparison operator
One might say: why use the comparison operator when I can use the
?
-
const number = 1
-
// Comparison operators
-
if (number > 0) // Positive
-
else // Negative
-
-
// Ternary operators
-
return (number < 0) ? -1 : (number > 0) ? 1 : 0
-
-
//
-
if (Math.sign(number) > 0) // Positive
-
else // Negative
In fact, if you're just checking for Boolean state, then we can use the comparison operator instead of the。
But we need to know. The effect of this is to return a numeric value. This means you can do calculations.
-
const number = 5
-
-
number > 0 // true
-
Math.sign(number) // 1
both (... and...)
()
Comparison/ternary operator (computing)For example, it is more readable and takes less time to write, read, and understand.
Solving algorithmic problems
We can use the settleinteger inversionAlgorithm:
123 ——> 321
-123 ——> -321
-
const reverseInteger = num => {
-
const numArray = Math.abs(num)
-
.toString()
-
.split('')
-
.reverse()
-
.join('')
-
-
const sign = Math.sign(num) // -1
-
return numArray * sign
-
}
-
-
reverseInteger(-321) // -123
You can find more information on theLeetcode(used form a nominal expression)integer inversionPerform the exercise.
negative zero
It also returns negative zero:
-
Math.sign(0) // 0
-
Math.sign(-0) // -0
Why return negative zero, do we need it 🤨? It exists for a reason.《You Don't Know JS》Kyle Simpson in the middle explains it very well:
Now, why do we need a negative zero, besides academic trivia?
There are certain applications where developers use the magnitude of a value to represent one piece of information (like speed of movement per animation frame) and the sign of that number to represent another piece of information (like the direction of that movement).
In those applications, as one example, if a variable arrives at zero and it loses its sign, then you would lose the information of what direction it was moving in before it arrived at zero. Preserving the sign of the zero prevents potentially unwanted information loss.
The translation is as follows:
Now, aside from academic trivia, why do we need negative zeros?
In some applications, developers use the magnitude of a value to represent one piece of information (e.g., the speed of movement for each animation frame) and the sign of that number to represent another piece of information (e.g., the direction of that movement).
In these applications, for example, if a variable reaches zero and it loses its sign, you will lose information about which direction it is moving until it reaches zero. Retaining the sign of zero prevents potentially unnecessary loss of information.
Browser Support
Except for IE. All major browsers are supported as shown below:
If you need to use it in IE or older browsers, you can use the MDN-suppliedPolyfill:
-
if (!Math.sign) {
-
Math.sign = function(x) {
-
return (x > 0) - (x < 0) || +x
-
}
-
}
Other solutions are also available:
-
const positive = 5
-
const negative = -5
-
const zero = 0
-
-
positive === 0 ? positive : positive > 0 ? 1 : -1 // 1
-
negative === 0 ? negative : negative > 0 ? 1 : -1 // -1
-
zero === 0 ? zero : zero > 0 ? 1 : -1 // 0