June 2018
Beginner
288 pages
6h 31m
English
Many JavaScript programmers, including your humble author, often repeat the mistake of comparing using ==, which is the type-coercion non-strict equality operator. Let’s look at an example that shows why using == may be a bad idea.
| | //BROKEN CODE |
| | const a = '1'; |
| | const b = 1; |
| | const c = '1.0'; |
| | |
| | console.log(a == b); |
| | console.log(b == c); |
| | console.log(a == c); |
In the short piece of code, the constants a, b, and c have values ’1’, 1, and ’1.0’, respectively. One value is of number type and the other two are of string type. The last three lines compare each combination of the constants. Suppose a is equal to b and b is equal to c; then logically JavaScript should tell us that a is equal ...
Read now
Unlock full access