Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript에서 변수 선언과 초기화의 차이점은 무엇입니까?


다음은 ECMAScript 사양의 변수 선언 및 초기화에 대해 설명되어 있습니다. -

A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. [...] A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.

위의 차이점은 다음과 같습니다.

  • 모든 변수는 정의되지 않은 값으로 초기화됩니다.
  • 변수 선언은 어휘 환경 초기화 시 undefined로 초기화됩니다.
  • 이 초기화는 할당으로 작동하지 않습니다.