Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 749 Bytes

check-if-a-number-is-positive-or-negative.md

File metadata and controls

32 lines (23 loc) · 749 Bytes

Check If A Number Is Positive Or Negative

The Math module has a handy function for checking if a number is positive or negative. Or zero, for that matter.

It is Math.sign.

> Math.sign(5)
1

> Math.sign(-5)
-1

> Math.sign(0)
0

Any positive number will result in 1. Any negative number will result in -1. If the number happens to be 0, then 0 will be returned.

This function goes real well with a switch statement.

Note also that anything that isn't a number will result in NaN.

> Math.sign("one")
NaN