undefined

1. 타입

값 타입은 typeof 연산자로 알 수 있다. 그럼 반환 값은 항상 7가지 내장 타입 중 하나 일까?

typeof undefined === 'undefined' // true
typeof true === 'boolean' // true
typeof 42 === 'number' // true
typeof '42' === 'string' // true
typeof { life: 42 } === 'object' // true
typeof Symbol() === 'symbol' // true

// ???
typeof null === 'object' // true

하위 호환성을 위해서 손을 대지 못하고 있다. null을 확인하려면 조건 하나가 더 필요하다.

var a = null;
(!a && typeof a === 'object'); // true

typeof가 반환하는 문자열은 하나 더 있다.

typeof function a() {} === 'function'; // true

그리고 typeof는 항상 문자열을 반환한다.

정리

"undefined" 와 "undeclared" 가 대충 같다고 보는 개발자들이 많은데, 전혀 다르다.

undefined는 선언된 변수에 할당 할 수 있는 값이지만, undeclared는 변수 자체가 선언된 적이 없음을 나타낸다. typeof 반환 값도 모두 undefined로 뭉뚱 그린다.