This commit is contained in:
Ilya Kantor 2020-06-01 09:43:55 +03:00
parent 479c7f908a
commit 8f2ae7eb20
2 changed files with 43 additions and 19 deletions

View file

@ -80,6 +80,19 @@ let user = 'John'
Technically, all these variants do the same thing. So, it's a matter of personal taste and aesthetics. Technically, all these variants do the same thing. So, it's a matter of personal taste and aesthetics.
````warn header="Declaring twice triggers an error"
A variable should be declared only once.
A repeated declaration of the same variable is an error:
```js run
let message = "This";
// repeated 'let' leads to an error
let message = "That"; // SyntaxError: 'message' has already been declared
```
So, we declare a variable once, and then should refer to it without `let`.
````
````smart header="`var` instead of `let`" ````smart header="`var` instead of `let`"
In older scripts, you may also find another keyword: `var` instead of `let`: In older scripts, you may also find another keyword: `var` instead of `let`:
@ -190,7 +203,7 @@ let имя = '...';
let 我 = '...'; let 我 = '...';
``` ```
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. 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.
```` ````
````warn header="Reserved names" ````warn header="Reserved names"

View file

@ -13,27 +13,18 @@ In the very first chapter about [variables](info:variables), we mentioned three
2. `const` 2. `const`
3. `var` 3. `var`
`let` and `const` behave exactly the same way in terms of Lexical Environments. 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:
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.
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.
From the first sight, `var` behaves similar to `let`. That is, declares a variable:
```js run ```js run
function sayHi() { var message = "Hi";
var phrase = "Hello"; // local variable, "var" instead of "let" alert(message); // Hi
alert(phrase); // Hello
}
sayHi();
alert(phrase); // Error, phrase is not defined
``` ```
...But here are the differences. 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.
If you don't plan on meeting such scripts you may even skip this chapter or postpone it.
On the other hand, it's important to understand differences when migrating old scripts from `var` to `let`, to avoid odd errors.
## "var" has no block scope ## "var" has no block scope
@ -94,7 +85,27 @@ alert(phrase); // Error: phrase is not defined (Check the Developer Console)
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. 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.
## "var" declarations are processed at the function start ## "var" tolerates redeclarations
If we declare the same variable with `let` twice in the same scope, that's an error:
```js run
let user;
let user; // SyntaxError: 'user' has already been declared
```
With `var`, we can redeclare a variable any number of times. If we use `var` with an already-declared variable, it's just ignored:
```js run
var user = "Pete";
var user = "John"; // this "var" does nothing (already declared)
// ...it doesn't trigger an error
alert(user); // John
```
## "var" variables can be declared below their use
`var` declarations are processed when the function starts (or script starts for globals). `var` declarations are processed when the function starts (or script starts for globals).