본문 바로가기
컴퓨터 기본

[JavaScript] undefined와 null의 차이

by Hindsight.. 2020. 8. 16.
console.log(null === undefined); //false
console.log(null == undefined); //true
console.log(undefined == undefined); //true
console.log(null == null); //true

 

단도직입적으로

undefined와 null은 다르다. 마치 정수 1과 문자열 '1'을 출력하면 같이 1이 나오지만 type이 다른것처럼.

 

undefined는 선언 O, 할당되지 않은 상태

null은 말 그대로 null이라는 값이 할당된 것이다. (할당되지 않았다는 값을 할당한 것)

 

둘다 값이 없다는점은 같다 그래서 '==' 연산자에서는 true가 출력된다.

하지만 type까지 체크하는 '===' 연산자 에서는 false가 출력되는 것이다.

 

둘을 혼동 및 혼용하지 말자!