Translator: I live by my breath
The original link
(Ads, please don’t stop.)
9. Variables and assignments
The main ways in which JavaScript declares variables:
-
Let declares mutable variables.
-
Const declares a constant (immutable variable).
Before ES6, there was VAR. But it has several quirks, so it’s best to avoid in modern JavaScript. You can read more about this in Speaking JavaScript.
9.1. let
Variables declared by let are mutable:
let i;
i = 0;
i = i + 1;
assert.equal(i, 1);
Copy the code
You can declare and copy both:
let i = 0;
Copy the code
9.2. const
Variables declared by const are immutable. You must initialize immediately:
const i = 0; // must initialize
assert.throws(
() => { i = i + 1 },
{
name: 'TypeError',
message: 'Assignment to constant variable.',
}
);
Copy the code
9.2.1. Const and unchanged
In JavaScript, const simply means that a pointer (the association between a variable name and its value) is immutable. The value itself can be mutable, as obj in the following example.
const obj = { prop: 0 };
obj.prop = obj.prop + 1;
assert.equal(obj.prop, 1);
Copy the code
However:
const obj = { prop: 0 };
assert.throws(
() => { obj = {} },
{
name: 'TypeError',
message: 'Assignment to constant variable.',
}
);
Copy the code
9.2.2. Const and loop
You can use const with for-of loops, creating a new binding with each iteration:
const arr = ['hello', 'world'];
for (const elem of arr) {
console.log(elem);
}
// Output:
// 'hello'
// 'world'
Copy the code
In the Plain for loop, let must be used, but:
const arr = ['hello', 'world'];
for (let i=0; i<arr.length; i++) {
const elem = arr[i];
console.log(elem);
}
Copy the code
9.3. Let and const choices
I recommend using the following rules to select let and const:
-
Const represents an immutable binding, and the variable never changes its value. Use him first.
-
Let indicates that the value of a variable has changed. Use const only if it cannot be used.
Exercise: const
exercises/variables-assignment/const_exrc.js
9.4. Scope of variables
The scope of a variable refers to the area in which it can be accessed.
As with most modern programming languages, variables declared through lets and const are block-level scoped: they can only be accessed in the declared block.
{
const x = 0;
}
assert.throws(
() => x + 1,
{
name: 'ReferenceError',
message: 'x is not defined',
}
);
Copy the code
Curly braces contain a code block. X exists only in the block and cannot be accessed externally.
9.4.1. Overlays and blocks
You cannot declare the same variable twice at the same level. However, you can nest blocks and use the same variable name x that is used outside the block:
const x = 1;
assert.equal(x, 1);
{
const x = 2;
assert.equal(x, 2);
}
assert.equal(x, 1);
Copy the code
Inside the block, the name of the inner X is unique. The X inside will cover the X outside. External access, you can access x external access.
More information on variable ranges and closures
For more information on variable scopes and closures, see the corresponding Corresponding Chapter, The Corresponding Corresponding Chapter, later in this book.
test
Check out the quiz app.
comments
Next: [10. Values](