null VS undefined
비슷하면서도 비슷하지 않은(?) null
과 undefined
의 차이점에 대해 알아보자.
null
null은 변수 선언과 동시에 값(value)을 할당할 수가 있다. 즉, null은 값인데 존재하지 않는, 비어있는, 알 수 없는 값이다.
예를 들어, hello
라는 변수에 아직 인사말을 못정해서 어떤 값을 할당해야 할지 모르겠다면 null
을 넣어주면 된다. null
의 타입은 object
다. 타입은 object
이지만 string, number, bigint, boolean, symbol, null, undefined는 모두 원시형 값이다.
let hello = null; //변수 선언
console.log(hello); //null
console.log(typeof hello); //object
hello = '안녕하세요~';
console.log(hello); //안녕하세요~
undefined
undefined는 변수 선언은 했지만, 값(value)을 할당하지 않았을 때 undefined
가 된다. 타입도 undefined
가 된다.
let hello; //변수 선언
console.log(hello); //undefined
console.log(typeof hello); //undefined
null과 undefined의 차이점 정리
null
은 변수 선언과 값을 할당하는 것을 말하고, undefined
는 변수 선언과 값을 할당하지 않는 것을 말한다.null
과 undefined
를 비교했을 때 값과 타입까지 비교하는 ===
에서는 false
를 반환하지만 타입을 제외하고 단순히 값만 비교했을 때 ==
에서는 true
를 반환한다.
console.log(null === undefined); //false
console.log(null == undefined); //true
참고 자료
'공부 기록' 카테고리의 다른 글
[HTML] HTML 이해하기 (0) | 2022.06.18 |
---|---|
var VS let VS const (0) | 2022.06.14 |
양식 다시 제출 확인 팝업창 (0) | 2022.01.14 |
[스프링 부트로 게시판 만들기] 11. 댓글 기능, Remember Me (0) | 2021.07.24 |
[스프링 부트로 게시판 만들기] 10. 조회수, 페이징과 검색 (2) | 2021.07.23 |
댓글