JavaScript:StrictEquality
Equal to(==) vs Strict equal(===)
- MDN: Equality comparisons and sameness
- Stackoverflow: Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?
- JavaScript == (동등, equal to), === (일치, strictly equal to) 차이
Equal to (==):
- 두 피연산자가 같은 type일 때, 값이 같이면 true
- 두 피연산자가 다른 type일 때, 미리 정의해 놓은 type conversion 규칙에 의해 두 피연산자가 같아지면 true
Strict equal (===):
- type conversion없이도 두 피연산자(operands)가 같은지 검사.
- 피연산자가 primitive type이고 값이 같다면 true
- 피연산자가 object type이고 참조하는 객체가 같다면 true
- 객체가 구조와 내용이 같더라도 주소가 다른 객체이면 false
비교 테이블:
x | y | == | === |
undefined | undefined | true | true |
null | null | true | true |
true | true | true | true |
false | false | true | true |
'foo' | 'foo' | true | true |
0 | 0 | true | true |
+0 | -0 | true | true |
+0 | 0 | true | true |
-0 | 0 | true | true |
0 | false | true | false |
"" | false | true | false |
"" | 0 | true | false |
'0' | 0 | true | false |
'17' | 17 | true | false |
[1,2] | '1,2' | true | false |
new String('foo') | 'foo' | true | false |
null | undefined | true | false |