Variables: let and const
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A variable is a name for a value, like a labelled box in memory. Follow a six-line program that keeps a game score: let makes a box whose value can change, const makes a locked box, and = works out the right side first and then stores the answer on the left. The last line tries to change a const, and JavaScript stops it with a TypeError.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01After these lines run, what is in x? let x = 4; x = x + 3;
The right side runs first: it reads 4 and works out 4 + 3 = 7. Then = stores 7 in x. = means "store", not "is equal to".
02What happens when this runs? const speed = 10; speed = 20;
A const is a locked box. Trying to reassign it throws a TypeError ("Assignment to constant variable." in Chrome and Node.js), and the program stops there.
03Which line is NOT allowed in JavaScript?
A name cannot start with a digit. Letters, digits, _ and $ are allowed, as long as the first character is not a digit.
04Why does const lives; (with no value) give an error?
A const can never be given a value later, so JavaScript requires the value right away: const lives; is a SyntaxError.
05You keep a running total that grows inside a loop. Which keyword fits best?
Use const by default, and let when the value really changes, like a total or a score. var is the older keyword; modern code uses let and const.
المسرد
- Variable
- A name for a value, like a labelled box in memory. Code can read the value by using the name.
- Declaration
- The line that makes a variable, such as let score = 0 or const maxLives = 3.
- let
- Makes a variable whose value can be replaced later.
- const
- Makes a variable that must get its value at once and can never be given a new one.
- Assignment (=)
- Stores the value on the right into the variable on the left. It does not mean "is equal to".
- TypeError
- An error JavaScript throws when an operation is not allowed, such as giving a const a new value.
- Identifier
- The name of a variable. It may use letters, digits, _ and $, but cannot start with a digit or be a reserved word.
- camelCase
- The usual way to write JavaScript names with several words: first word lower case, then a capital for each new word, as in maxLives.
الملاحظات والمصادر
- The box is a simplification. The ECMAScript specification calls a variable a binding: a name that refers to a value, kept in an environment record for that block of code. Strictly, the binding exists from the start of its block but cannot be used until its line runs (the temporal dead zone).
- Chrome and Node.js (the V8 engine) print "TypeError: Assignment to constant variable."; Firefox says "invalid assignment to const 'maxLives'". The error happens when line 6 runs, so lines 1 to 5 have already run and 15 3 is printed first.
- const locks the box, not what is inside an object or array it holds: after const scores = [], scores.push(5) works, but scores = [] throws a TypeError.
- Two more rules: using a let or const variable before the line that makes it throws a ReferenceError, and making the same name twice with let or const in one block is a SyntaxError.
- var is the older keyword. A var variable belongs to the whole function, not just the block it is in, and can be used before its line (it is undefined there), so modern code uses let and const.
- The program is real. Save it as score.js and run node score.js, or paste it into your browser's console: it prints 15 3, then the TypeError.
اختبار يتاح بعد الخطوة الأخيرة.