Skip to content

Commit 8f2ae7e

Browse files
committed
fixes #1931
1 parent 479c7f9 commit 8f2ae7e

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

1-js/02-first-steps/04-variables/article.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ let user = 'John'
8080

8181
Technically, all these variants do the same thing. So, it's a matter of personal taste and aesthetics.
8282

83+
````warn header="Declaring twice triggers an error"
84+
A variable should be declared only once.
85+
86+
A repeated declaration of the same variable is an error:
87+
88+
```js run
89+
let message = "This";
90+
91+
// repeated 'let' leads to an error
92+
let message = "That"; // SyntaxError: 'message' has already been declared
93+
```
94+
So, we declare a variable once, and then should refer to it without `let`.
95+
````
8396

8497
````smart header="`var` instead of `let`"
8598
In older scripts, you may also find another keyword: `var` instead of `let`:
@@ -190,7 +203,7 @@ let имя = '...';
190203
let 我 = '...';
191204
```
192205
193-
Technically, there is no error here, such names are allowed, but there is an international tradition to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it some time.
206+
Technically, there is no error here. Such names are allowed, but there is an international convention to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it some time.
194207
````
195208

196209
````warn header="Reserved names"

1-js/06-advanced-functions/04-var/article.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,18 @@ In the very first chapter about [variables](info:variables), we mentioned three
1313
2. `const`
1414
3. `var`
1515

16-
`let` and `const` behave exactly the same way in terms of Lexical Environments.
17-
18-
But `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
19-
20-
If you don't plan on meeting such scripts you may even skip this chapter or postpone it, but then there's a chance that it bites you later.
21-
22-
From the first sight, `var` behaves similar to `let`. That is, declares a variable:
16+
The `var` declaration is similar to `let`. Most of the time we can replace `let` by `var` or vice-versa and expect things to work:
2317

2418
```js run
25-
function sayHi() {
26-
var phrase = "Hello"; // local variable, "var" instead of "let"
27-
28-
alert(phrase); // Hello
29-
}
19+
var message = "Hi";
20+
alert(message); // Hi
21+
```
3022

31-
sayHi();
23+
But internally `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
3224

33-
alert(phrase); // Error, phrase is not defined
34-
```
25+
If you don't plan on meeting such scripts you may even skip this chapter or postpone it.
3526

36-
...But here are the differences.
27+
On the other hand, it's important to understand differences when migrating old scripts from `var` to `let`, to avoid odd errors.
3728

3829
## "var" has no block scope
3930

@@ -94,7 +85,27 @@ alert(phrase); // Error: phrase is not defined (Check the Developer Console)
9485

9586
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript blocks had no Lexical Environments. And `var` is a remnant of that.
9687

97-
## "var" declarations are processed at the function start
88+
## "var" tolerates redeclarations
89+
90+
If we declare the same variable with `let` twice in the same scope, that's an error:
91+
92+
```js run
93+
let user;
94+
let user; // SyntaxError: 'user' has already been declared
95+
```
96+
97+
With `var`, we can redeclare a variable any number of times. If we use `var` with an already-declared variable, it's just ignored:
98+
99+
```js run
100+
var user = "Pete";
101+
102+
var user = "John"; // this "var" does nothing (already declared)
103+
// ...it doesn't trigger an error
104+
105+
alert(user); // John
106+
```
107+
108+
## "var" variables can be declared below their use
98109

99110
`var` declarations are processed when the function starts (or script starts for globals).
100111

0 commit comments

Comments
 (0)