Skip to content

Commit 83807cb

Browse files
committed
abstract equality comparison vs strict equality comparison
1 parent c92b4e5 commit 83807cb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Primitive Types, Non-Primitive Types, `typeof` operatior, Special values like Na
77
## [Coercion](coercion/README.md)
88

99
Abstract Operations: ToPrimitive, ToString, ToNumber and ToBoolean
10+
11+
## [Abstract Equality Comparison vs Strict Equality Comparison](abstract-equality-vs-strict-equality/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Abstract Euality Comparison (==) vs Strict Equality Comparison (===)
2+
3+
`==` and `===` both checks for type if the value's type matches both comparison is same, but if types does not matches `==` allows coercion while `===` does not.
4+
5+
## Is coercion helpful in an equality comparison or not?
6+
7+
The ESLint rule eqeqeq prevents the use of == (Abstract Equality Comparison). Whether this rule is helpful its a decision of your team. However, it’s important to understand when and how to use == effectively, as it can help you avoid unnecessary bugs caused by unexpected type coercion.
8+
9+
## Abstract Equality Comparsion
10+
11+
Abstract Equality Comparsion is same when the values type matches. If Type(x) is same as Type(y) then it return the result of performing **Strict Equality Comparison x===y**.
12+
13+
- If x is null and y is undefined, return true.
14+
15+
- If x is undefined and y is null, return true.
16+
17+
If only if Type(x) and Type(y) does not matches, Abstract Equality Comparison allow coercion.
18+
19+
- If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
20+
21+
- If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
22+
23+
- If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
24+
25+
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
26+
27+
- If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
28+
29+
- If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
30+
31+
## Strict Equality Comparison

0 commit comments

Comments
 (0)