var = 함수단위스코프
function test () {
var a = 1
if(true){
console.log(a) // 1
}
}
let, const = 블록단위 스코프
function test () {
var a = 1
if(true){
console.log(a) // undefined
let b = 1
console.log(b) //1
}
}
var
let, const
var
let, const
const 는 상수를 뜻하지만 객체에 할당시 객체값은 참조된 값이므로 변경이 가능하게된다.
이를 방지하기위해 Object.freeze() 기능을 사용하면 상수화 시킬수있다.
const test = {
x:1,
y:2
}
Object.freeze(test)
test.x = 11;
console.log(test) // {x:1,y:2}