diff --git a/README.md b/README.md index bc5ce1b..97166c9 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1169 TILs and counting..._ +_1170 TILs and counting..._ --- @@ -364,6 +364,7 @@ _1169 TILs and counting..._ - [Basic Date Formatting Without A Library](javascript/basic-date-formatting-without-a-library.md) - [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md) - [Check Classes On A DOM Element](javascript/check-classes-on-a-dom-element.md) +- [Check If A Number Is Positive Or Negative](javascript/check-if-a-number-is-positive-or-negative.md) - [Check If Something Is An Array](javascript/check-if-something-is-an-array.md) - [Check The Password Confirmation With Yup](javascript/check-the-password-confirmation-with-yup.md) - [Compare The Equality Of Two Date Objects](javascript/compare-the-equality-of-two-date-objects.md) diff --git a/javascript/check-if-a-number-is-positive-or-negative.md b/javascript/check-if-a-number-is-positive-or-negative.md new file mode 100644 index 0000000..ab97e24 --- /dev/null +++ b/javascript/check-if-a-number-is-positive-or-negative.md @@ -0,0 +1,32 @@ +# Check If A Number Is Positive Or Negative + +The +[`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) +module has a handy function for checking if a number is _positive_ or +_negative_. Or _zero_, for that matter. + +It is +[`Math.sign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign). + +```javascript +> 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`. + +```javascript +> Math.sign("one") +NaN +```