[ 프론트엔드 ]/JavaScript
[ JavaScript ] 동등 연산자 (==) vs. 일치 연산자 (===)
불주먹고양이
2022. 1. 12. 11:23

동등연산자 (==)는 피연산자들의 값만 비교한다.
일치연산자 (===)는 피연산자들의 값과 타입을 모두 비교한다.
const numA = 1;
const strA = '1';
console.log(numA == strA); // true
console.log(numA === strA); // false
numA는 숫자형, strA는 문자형이다.
동등연산자로 비교했을 때 둘의 값이 같다고 판단하여 true를 출력했다.
반면에 일치연산자로 비교했을 때 둘의 값은 같지만 타입이 달라 false를 출력했다.
console.log(null == undefined) // true
console.log(null === undefined) // false
null과 undefined의 비교에서도 동등연산자는 true를, 비교연산자에서는 false를 반환한다.
null은 object 타입이고, undefined는 undefined 타입이기 때문이다.