[JavaScript] undefined 이해하기

JavaScript에는 3가지 undefined가 존재한다.

타입으로서의 undefined

우선 undefined는 타입이다. ECMA Script 5까지는 다음과 같은 6가지1) Built-in type이 있었다.

  1. number
  2. boolean
  3. string
  4. object
  5. null
  6. undefined

undefined는 그중 하나다.

값으로서의 undefined

undefinedundefined 타입의 유일한 값이다. 우선 값이 할당되지 않은 변수 혹은 값이 할당되지 않은 프로퍼티는 undefined가 된다.

또한, 리턴문이 없는 함수나 리턴하는 값이 없는 리턴문으로 끝나는 함수의 실행 결과도 undefined가 된다.

혹은 전역 프로퍼티인 undefined를 통해서 undefined 값을 얻을 수 있다.

전역 프로퍼티 undefined

global context에는 undefined라는 이름의 프로퍼티가 설정되어 있다. 이 undefinedundefined인 값을 가진다. ECMAScript 5 이후로 이 프로퍼티는 non-configurable이고, non-writable로 설정되어 있다. 따라서 변경할 수 없다.


1) ECMAScript 6에는 Symbol type이 추가되었다.

댓글

이 블로그의 인기 게시물

[C++] enum class - 안전하고 쓰기 쉬운 enum

Log Aggregator 비교 - Scribe, Flume, Fluentd, logstash

RAII는 무엇인가

[Python] cache 데코레이터로 최적화하기

[Web] SpeechSynthesis - TTS API