Skip to content

Commit 5294de9

Browse files
committed
types-grammar, ch2: finishing ch2 draft
1 parent 7d446e8 commit 5294de9

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

types-grammar/ch1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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
855855
856856
Without having a signed zero value, you couldn't tell which direction such an item was pointing at the moment it came to rest.
857857
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+
858862
### Invalid Number
859863
860864
Mathematical operations can sometimes produce an invalid result. For example:

types-grammar/ch2.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,12 @@ myAge = 41. + 1.;
941941
myAge; // 42
942942
```
943943

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+
944950
### Static `Number` Properties
945951

946952
* `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),
988994
| :--- |
989995
| 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. |
990996

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+
9911038
[^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
9921039

9931040
[^INTLAPI]: ECMAScript 2022 Internationalization API Specification; https://402.ecma-international.org/9.0/ ; Accessed August 2022

types-grammar/toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
* Primitive Assignments
2323
* String Behaviors
2424
* Number Behaviors
25-
* TODO
25+
* Primitives Are Foundational

0 commit comments

Comments
 (0)