Language/Javascript

[Javascript] var, let, const 비교 분석

TechNote.kr 2022. 7. 3. 23:43
728x90

Javascript 에는 3가지 변수 선언 방식이 있다. 

재선언, 재할당 가능 여부에 따라 구별되어 사용된다.

  재선언 재할당
var O O
let X O
const X X

 

Javascript Code 를 실행하는 방법 참조 링크 : [Javascript] 코드 동작 확인 방법 (technote.kr)


var

재선언과 재할당이 가능하다.

[Code #1]

var a = 1;
a = 2;
var a = 3;

console.log(a);
PS D:\workspace\node_test> node main.js
3

let

재할당은 가능하지만 재선언은 불가능하다.

[Code #1]

let a = 1;
a = 2;

console.log(a);
PS D:\workspace\node_test> node main.js
2

[Code #2]

let a = 1;
var a = 2;

console.log(a);
PS D:\workspace\node_test> node main.js
D:\workspace\node_test\main.js:2
var a = 2;
    ^

SyntaxError: Identifier 'a' has already been declared
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

const

재선언, 재할당 모두 불가능하다.

[Code #1]

const a = 1;
a = 2;

console.log(a);
PS D:\workspace\node_test> node main.js
D:\workspace\node_test\main.js:2
a = 2;
  ^

TypeError: Assignment to constant variable.
    at Object.<anonymous> (D:\workspace\node_test\main.js:2:3)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

[Code #2]

const a = 1;
var a = 2;

console.log(a);
PS D:\workspace\node_test> node main.js
D:\workspace\node_test\main.js:2
var a = 2;
    ^

SyntaxError: Identifier 'a' has already been declared
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

[변수가 var인지, let인지, const인지 판단할 수 있는 코드]

const x = 5;

var code = `var x = 3`
const original = x;
let isConst = false;
let isNotVar = false;

try {
  eval(code);  
} catch (err) {
  isNotVar = true;
}

if (isNotVar == false) {
  console.log('var x');
  return ;
}

try {
  x = 'anything';
  x = original;
} catch (err) {
  isConst = true;
}

console.log(isConst ? 'const x' : 'let x');

 

728x90

'Language > Javascript' 카테고리의 다른 글

[Javascript] 코드 동작 확인 방법  (0) 2022.06.20
javascript is eating the world.  (0) 2015.12.23
ajax POST debugging 방법  (0) 2015.12.12
ajax를 이용한 async post data 저장  (0) 2015.11.16
Slickgrid grid options  (0) 2015.11.16
Slickgrid example1-simple.html 분석  (0) 2015.11.16
ajax  (0) 2015.11.02
Closure - 클로저  (0) 2015.10.25