Variables — let and const · 変数 — let と 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).
日本語
変数は名前のついた箱
- 変数は値を格納し、名前を付与することで、後に再度使用できるようにします。
letは変更可能なボックスを作ります。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変数を新しい値で更新する
- 再代入 —
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. · 変数は 名前付きのボックス である。let は変更可能だが、const は固定される。
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は再代入できません:const x = 1; x = 2;はエラーを投げてしまいます。- 良い習慣として、基本は
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を使います。 - Check answer を押して確認する。
Store the text Cairo in a const called city, then log city. · テキストCairoをconstである変数cityに格納し、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). · let w = 4 と let h = 3 は幅と高さである。面積(w * h)を計算し、ログ出力せよ(それは 12 である)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Make a let total = 10, add 5 to it, then log total (it is 15). · let total = 10 を作成し、5 を加算し、total をログ出力せよ(それは 15 である)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。