목차
==, ===

- 자바스크립트는 엄격한 비교와 유형변환 비교를 모두 지원함
- 따라서, 어떤 연산자가 어떤 비교조건에 사용되는지가 중요함
- ===는 변수 유형을 고려함
└ 엄격한 비교를 함 ([값 & 자료형] -> true) - ==는 변수 값을 기반으로 유형을 수정함
└ 서로 다른 유형의 두 변수의 [값] 비교
예시
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 == false // true |
└ 0값은 false와 동일하므로 true 출력
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 === false // false | |
console.log(typeof 0); // "number" | |
console.log(typeof false); // "boolean" |
└ 두 피연산자의 유형이 다르기 때문에 false 출력
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 == "2" // true |
└ 자동 유형변화 비교하기 때문에 true 출력
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 === "2" // false | |
console.log(typeof 2); // "number" | |
console.log(typeof "2"); // "string" |
└ 두 피연산자의 유형이 다르기 때문에 false 출력
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
null == undefined // true |
└ 자동 유형변화 비교하기 때문에 true 출력
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
null == undefined // false | |
console.log(typeof null); // "object" | |
console.log(typeof undefined); // "undefined" |
└ 두 피연산자의 유형이 다르기 때문에 false 출력
!=와 !== 비교연산자의 차이는 ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 != "2" // false |
└ 값이 다르지 않으므로(자료형 비교 안하므로) false 출력
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 !== "2" // true | |
console.log(typeof 2); // "number" | |
console.log(typeof "2"); // "string" |
└ 두 피연산자의 유형이 다른 것이 맞기 때문에 true 출력
'Web Basic > JavaScript' 카테고리의 다른 글
let, const, var (0) | 2022.05.01 |
---|---|
ES6의 특징 (0) | 2022.04.18 |
스코프, 스코프 체인 (0) | 2022.03.29 |
실행 컨텍스트와 호이스팅 (0) | 2022.03.28 |
Array의 필수 메서드(02) (0) | 2022.03.21 |