let
값이 변경될 가능성이 있는 경우, let을 통해 변수 선언을 한다. 예를 들어 'age'변수의 경우 고정값이 아닌 변동될 가능성이 있으므로 이 경우에는 let으로 선언하는 것이 타당하다.
"we use the let keyword to declare variables that can change later so basically during the execution of our program"
변경 가능하므로 아래와 같이 값을 재할당할 수 있다.
let age = 30;
...
age = 31;
이를 reassigning 혹은 mutate라고 한다. 즉 mutate할 필요가 있는 변수는 let으로 선언한다.
"when we need to mutate a variable, that's the perfect use case for using let and that also counts for the case that we want to declare empty variables"
const
const변수의 값은 변하지 않는다. let과 반대.
const birthYear = 2020;
birthYear = 2021; //uncaught TypeError
const children; // uncaught SyntaxError
즉 const로 선언한 변수는 immutable하다. 또한 let과 달리 empty값도 할당 불가능하다.
var -> var는 let과 비슷하나 뒤에서는 전혀 다르게 작동한다. 최대한 쓰지마라.
** 이 포스트는 Udemy의 "The Complete JavaScript Course 2021: From Zero to Expert!" 강좌를 토대로 작성한 글입니다.
'TIL > JAVASCRTIPT' 카테고리의 다른 글
Section2-33~37 Function (0) | 2021.01.02 |
---|---|
Section2-20 Type Conversion and Coercion (0) | 2021.01.02 |
Section2-12 Data Type (0) | 2021.01.02 |