You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: types-grammar/ch1.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -855,6 +855,10 @@ You may wonder why we'd ever need such a thing as `-0`. It can be useful when us
855
855
856
856
Without having a signed zero value, you couldn't tell which direction such an item was pointing at the moment it came to rest.
857
857
858
+
| NOTE: |
859
+
| :--- |
860
+
| While JS defines a signed zero in the `number` type, there is no corresponding signed zero in the `bigint` number type. As such, `-0n` is just interpreted as `0n`, and the two are indistinguishable. |
861
+
858
862
### Invalid Number
859
863
860
864
Mathematical operations can sometimes produce an invalid result. For example:
Copy file name to clipboardExpand all lines: types-grammar/ch2.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -941,6 +941,12 @@ myAge = 41. + 1.;
941
941
myAge; // 42
942
942
```
943
943
944
+
Values of `bigint` type cannot have decimals, so the parsing is unambiguous that a `.` after a literal (with the trailing `n`) is always a property access:
945
+
946
+
```js
947
+
42n.toString(); // 42
948
+
```
949
+
944
950
### Static `Number` Properties
945
951
946
952
*`Number.EPSILON`: The smallest value possible between `1` and the next highest number
@@ -988,6 +994,47 @@ Unlike `Number`, which is also the `Number(..)` function (for number coercion),
988
994
| :--- |
989
995
| One peculiar member of the `Math` namespace is `Math.random()`, for producing a random floating point value between `0` and `1.0`. It's unusual to consider random number generation -- a task that's inherently stateful/side-effect'ing -- as a mathematical operation. It's also long been a footgun security-wise, as the pseudo-random number generator (PRNG) that JS uses is *not* secure (can be predicted) from a cryptography perspective. The web platform stepped in several years ago with the safer `crypto.getRandomValues(..)` API (based on a better PRNG), which fills a typed-array with random bits that can be interpreted as one or more integers (of type-specified maximum magnitude). Using `Math.random()` is universally discouraged now. |
990
996
997
+
### BigInts and Numbers Don't Mix
998
+
999
+
As we covered in Chapter 1, values of `number` type and `bigint` type cannot mix in the same operations. That can trip you up even if you're doing a simple increment of the value (like in a loop):
1000
+
1001
+
```js
1002
+
myAge =42n;
1003
+
1004
+
myAge +1; // TypeError thrown!
1005
+
myAge +=1; // TypeError thrown!
1006
+
1007
+
myAge +1n; // 43n
1008
+
myAge +=1n; // 43n
1009
+
1010
+
myAge++;
1011
+
myAge; // 44n
1012
+
```
1013
+
1014
+
As such, if you're using both `number` and `bigint` values in your programs, you'll need to manually coerce one value-type to the other somewhat regularly. The `BigInt(..)` function (no `new` keyword) can coerce a `number` value to `bigint`. Vice versa, to go the other direction from `bigint` to `number`, use the `Number(..)` function (again, no `new` keyword):
1015
+
1016
+
```js
1017
+
BigInt(42); // 42n
1018
+
1019
+
Number(42n); // 42
1020
+
```
1021
+
1022
+
Keep in mind though: coercing between these types has some risk:
1023
+
1024
+
```js
1025
+
BigInt(4.2); // RangeError thrown!
1026
+
BigInt(NaN); // RangeError thrown!
1027
+
BigInt(Infinity); // RangeError thrown!
1028
+
1029
+
Number(2n**1024n); // Infinity
1030
+
```
1031
+
1032
+
## Primitives Are Foundational
1033
+
1034
+
Over the last two chapters, we've dug deep into how primitive values behave in JS. I bet more than a few readers were, like me, ready to skip over these topics. But now, hopefully, you see the importance of understanding these concepts.
1035
+
1036
+
The story doesn't end here, though. Far from it! In the next chapter, we'll turn our attention to understanding JS's object types (objects, arrays, etc).
1037
+
991
1038
[^TwitterUnicode]: "New update to the Twitter-Text library: Emoji character count"; Andy Piper; Oct 2018; https://twittercommunity.com/t/new-update-to-the-twitter-text-library-emoji-character-count/114607 ; Accessed July 2022
992
1039
993
1040
[^INTLAPI]: ECMAScript 2022 Internationalization API Specification; https://402.ecma-international.org/9.0/ ; Accessed August 2022
0 commit comments