This repository was archived by the owner on Jan 18, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010
1111For a steady stream of TILs, [ sign up for my newsletter] ( https://crafty-builder-6996.ck.page/e169c61186 ) .
1212
13- _ 1169 TILs and counting..._
13+ _ 1170 TILs and counting..._
1414
1515---
1616
@@ -364,6 +364,7 @@ _1169 TILs and counting..._
364364- [ Basic Date Formatting Without A Library] ( javascript/basic-date-formatting-without-a-library.md )
365365- [ Character Codes from Keyboard Listeners] ( javascript/character-codes-from-keyboard-listeners.md )
366366- [ Check Classes On A DOM Element] ( javascript/check-classes-on-a-dom-element.md )
367+ - [ Check If A Number Is Positive Or Negative] ( javascript/check-if-a-number-is-positive-or-negative.md )
367368- [ Check If Something Is An Array] ( javascript/check-if-something-is-an-array.md )
368369- [ Check The Password Confirmation With Yup] ( javascript/check-the-password-confirmation-with-yup.md )
369370- [ Compare The Equality Of Two Date Objects] ( javascript/compare-the-equality-of-two-date-objects.md )
Original file line number Diff line number Diff line change 1+ # Check If A Number Is Positive Or Negative
2+
3+ The
4+ [ ` Math ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math )
5+ module has a handy function for checking if a number is _ positive_ or
6+ _ negative_ . Or _ zero_ , for that matter.
7+
8+ It is
9+ [ ` Math.sign ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign ) .
10+
11+ ``` javascript
12+ > Math .sign (5 )
13+ 1
14+
15+ > Math .sign (- 5 )
16+ - 1
17+
18+ > Math .sign (0 )
19+ 0
20+ ```
21+
22+ Any positive number will result in ` 1 ` . Any negative number will result in
23+ ` -1 ` . If the number happens to be ` 0 ` , then ` 0 ` will be returned.
24+
25+ This function goes real well with a switch statement.
26+
27+ Note also that anything that isn't a number will result in ` NaN ` .
28+
29+ ``` javascript
30+ > Math .sign (" one" )
31+ NaN
32+ ```
You can’t perform that action at this time.
0 commit comments