[JS] Difference between == , ===

daniel·2022년 6월 2일
0

[JS]

목록 보기
7/16
post-thumbnail

from BBC https://www.bbc.com/news/health-56365422

The equality operator in javascript is used to compare if two values are equal. The comparison is made by == and === operators in javascript. The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

자바스크립트에서 두 값이 같은지 비교할때 등호를 두개쓰거나 세개를 쓸 수 있다. 통산 ==을 느슨한 비교===을 엄격한 비교라고 한다.

==

느슨한 비교는 두 값이 같은지만 비교하며

===

엄격한 비교는 값과 Data type까지 같은지를 비교한다 모두 같으면 true, 하나라도 다르면 false를 반환한다.

위와같이 같은 1을 쓰더라도 number인지 string 인지 구별해주는것은 엄격한 비교이다.

What about Object


객체타입의 변수는 메모리의 주소를 참조한다 따라서 위 두 객체의 데이터 타입과 값이 같지만 서로의 메모리주소가 다르기 때문에 두 객체는 같지않다.


만약 새로운 C를 선언하여 B를 얕은 복사한다면? 이때는 같은 메모리 주소를 참조하므로 둘이 서로 같음을 알 수 있다.

profile
FE 개발 velog

0개의 댓글