File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -10,13 +10,15 @@ JavaScript (and by extension TypeScript) has two bottom types : `null` and `unde
1010
1111### Checking for either
1212
13- Fact is you will need to deal with both. Just check for either with ` == ` check.
13+ Fact is you will need to deal with both. Interestingly in JavaScript with ` == ` , ` null ` and ` undefined ` are only equal to each other:
1414
1515``` ts
16- // / Imagine you are doing `foo.bar == undefined` where bar can be one of:
17- console .log (undefined == undefined ); // true
16+ // Both null and undefined are only `==` to themselves and each other:
17+ console .log (null == null ); // true (of course)
18+ console .log (undefined == undefined ); // true (of course)
1819console .log (null == undefined ); // true
1920
21+
2022// You don't have to worry about falsy values making through this check
2123console .log (0 == undefined ); // false
2224console .log (' ' == undefined ); // false
@@ -32,7 +34,9 @@ function foo(arg: string | null | undefined) {
3234}
3335```
3436
35- One exception, root level undefined values which we discuss next.
37+ > You could also do ` == undefined ` , but ` == null ` is more conventional/shorter.
38+
39+ One exception, root level ` undefined ` values which we discuss next.
3640
3741### Checking for root level undefined
3842
You can’t perform that action at this time.
0 commit comments