web123456

How to check if a number is positive or negative in JavaScript

ES6 method returns the sign of a number, indicating whether the number is positive, negative, or zero.

It's easy to use:

  1. Math.sign(6) // 1
  2. Math.sign(-6) // -1
  3. Math.sign(0) // 0

return value

() There are 5 possible return values:

  1. 1 // Positive
  2. -1 // Negative numbers
  3. 0 // positive zero
  4. -0 // Negative zero
  5. NaN // Not a number
  6. Math.sign(8) // 1
  7. Math.sign(-8) // -1
  8. Math.sign(0) // 0
  9. Math.sign(-0) // -0
  10. Math.sign(NaN) // NaN
  11. Math.sign('hello') // NaN
  12. Math.sign() //NaN

take note of: The parameters passed to this method willimplicit conversionbecause ofnumber 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

  1. const number = 1
  2. // Comparison operators
  3. if (number > 0) // Positive
  4. else // Negative
  5. // Ternary operators
  6. return (number < 0) ? -1 : (number > 0) ? 1 : 0
  7. //
  8. if (Math.sign(number) > 0) // Positive
  9. 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.

  1. const number = 5
  2. number > 0 // true
  3. 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

  1. const reverseInteger = num => {
  2. const numArray = Math.abs(num)
  3. .toString()
  4. .split('')
  5. .reverse()
  6. .join('')
  7. const sign = Math.sign(num) // -1
  8. return numArray * sign
  9. }
  10. 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:

  1. Math.sign(0) // 0
  2. 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:

Browser Support

If you need to use it in IE or older browsers, you can use the MDN-suppliedPolyfill

  1. if (!Math.sign) {
  2. Math.sign = function(x) {
  3. return (x > 0) - (x < 0) || +x
  4. }
  5. }

Other solutions are also available:

  1. const positive = 5
  2. const negative = -5
  3. const zero = 0
  4. positive === 0 ? positive : positive > 0 ? 1 : -1 // 1
  5. negative === 0 ? negative : negative > 0 ? 1 : -1 // -1
  6. zero === 0 ? zero : zero > 0 ? 1 : -1 // 0