1 답변
-
자바스크립트에서 undefined는 변수가 정의되었지만 아무 값도 할당받지 않은 상태를 뜻합니다.
var TestVar; alert(TestVar); //shows undefined alert(typeof TestVar); //shows undefined
그에비해 null은 null값을 할당받은것을 뜻합니다.
var TestVar = null; alert(TestVar); //shows null alert(typeof TestVar); //shows object
null값은 객체로 취급됩니다. 몇가지 예제로 둘의 차이를 더 분명하게 보여드리자면 undefined는 딱히 타입이 없고 null은 객체타입입니다.
null === undefined // false null == undefined // true null === null // true
undefined는 아무값도 할당되지않은상태라 값을 할당할숭있지만 null은 아닙니다.
null = 'value' // ReferenceError undefined = 'value' // 'value'
댓글 입력