Variables — let and const
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
A variable is a named box
- A variable stores a value and gives it a name, so you can use it again later.
letmakes a box you can change.constmakes a box that must stay the same (a constant).
한국어
변수는 이름이 붙은 상자입니다
- 변수(variable) 는 값을 저장하고 이름(name) 을 부여하므로 나중에 다시 사용할 수 있습니다.
- **
let**는 당신이 변경(change) 할 수 있는 상자를 만듭니다. - **
const**는 변경되지 않아야 하는 상자를 만듭니다(constant).
let age = 15; // a value that may change
const name = "Ada"; // a value that won't
console.log(name, age); // → Ada 15
English
Change a let with a new value
- Assign again —
count = count + 1— to update alet. count++is a handy shortcut for "add 1 tocount".
한국어
let을 새로운 값으로 변경하기
- 다시 할당(assign)하십시오 —
count = count + 1— **let**을 업데이트하기 위해. - **
count++는 "count**에 1을 더하다"의 편리한 단축문입니다.
let count = 0;
count = count + 1;
count++;
console.log(count); // → 2
Explore · 탐색하기
Watch the boxes change
A variable is a named box. let can be changed; const stays fixed.
English
Watch out
- A
constcannot be reassigned:const x = 1; x = 2;throws an error. - Good habit: use
constby default, and reach forletonly when the value truly changes.
한국어
주의사항
- **
const**는 재할당(reassign)할 수 없습니다: **const x = 1; x = 2;**은 오류(error) 를 발생시킵니다. - 좋은 습관: 기본적으로 **
const**를 사용하고, 값이 실제로 변할 때만 **let**을 사용하십시오.
English
Common mistakes
- Use
constfor values that never change,letfor those that do. - Declare a variable before you use it.
한국어
흔한 실수
- 변하지 않는 값에는 **
const**를, 변하는 값에는 **let**을 사용하십시오. - 변수를 사용하기 전에 먼저 선언해야 합니다.
English
Now you try
- Use
letorconstto store values. - Press Check answer to test it.
한국어
이제 직접 해보기
- 값을 저장하기 위해
let또는 **const**을 사용하십시오. - 답안 확인을 눌러 테스트해 보십시오.
Store the text Cairo in a const called city, then log city.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
let w = 4 and let h = 3 are the width and height. Work out the area (w * h) and log it (it is 12).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Make a let total = 10, add 5 to it, then log total (it is 15).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.