Skip to content

Commit 21cfb29

Browse files
author
Basarat Ali Syed
committed
moar
1 parent a99b4a2 commit 21cfb29

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* [Getting Started](docs/getting-started.md)
44
* [Why TypeScript](docs/why-typescript.md)
55
* [JavaScript](docs/javascript/recap.md)
6+
* [Equality](docs/javascript/equality.md)
67
* [Null vs. Undefined](docs/javascript/null-undefined.md)
8+
* [this](docs/javascript/this.md)
79
* [Closure](docs/javascript/closure.md)
810
* [Future JavaScript Now](docs/future-javascript.md)
911
* [Classes](docs/classes.md)

docs/javascript/equality.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Equality
2+
3+
One thing to be careful about in JavaScript is the difference between `==` and `===`. As JavaScript tries to
4+
be resilient against programming errors `==` tries to do type coercion between two variables e.g. converts a
5+
string to a number so that you can compare with a number as shown below:
6+
7+
```js
8+
console.log(5 == "5"); // true , TS Error
9+
console.log(5 === "5"); // false , TS Error
10+
```
11+
12+
However the choices JavaScript makes are not always ideal. For example in the below example the first statement is false
13+
because `""` and `"0"` are both strings and are clearly not equal. However in the second case both `0` and the
14+
empty string (`""`) are falsy (i.e. behave like `false`) and are therefore equal with respect to `==`. Both statements
15+
are false when you use `===`.
16+
17+
```js
18+
console.log("" == "0"); // false
19+
console.log(0 == ""); // true
20+
21+
console.log("" === "0"); // false
22+
console.log(0 === ""); // false
23+
```
24+
25+
> Note that `string == number` and `string === number` are both compile time errors in TypeScript, so you don't normally need to worry about this.
26+
27+
Similar to `==` vs. `===`, there is `!=` vs. `!==`
28+
29+
So ProTip: Always use `===` and `!==` except for null checks, which we cover next.

0 commit comments

Comments
 (0)