개발 및 관련 자료/WEB
[es6] const, let
kid1412
2019. 10. 20. 23:04
728x90
const, let
javscript ES6 버전에서는 변수 선언시 이전 버전의 var 말고도 const, let 키워드가 추가되었습니다.
let
블록 스코프 변수로써 자신을 정의한 블록에서만 접근 가능하며 블록 밖에서는 볼 수 없습니다.
function letTest() {
let x = 10;
console.log(x); //10
if(true) {
let y= 20;
console.log(y); //20
}
console.log(y); //Reference Error Exception
}
const
읽기 전용 변수, 즉 값을 다시 할당할 수 없는 상수를 선언
const도 블록 스코프 변수라 let으로 선언한 변수와 규칙은 동일
다만, 객체 자신이 아닌 참조값이 저장하므로 객체 내부는 변경 가능
function constTest() {
const b = 10;
console.log(b); // 10
b = 20; // Assignment to constant variable.
}
참고문헌
- ECMAScript 6 길들이기 (http://www.yes24.com/Product/Goods/23904865?Acode=101)