You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
جالب است بدانید زبانهای [functional](https://en.wikipedia.org/wiki/Functional_programming) مانند [Scala](http://www.scala-lang.org/) یا [Erlang](http://www.erlang.org/) تغییر مقدار متغیر را ممنوع کردهاند.
143
+
=======
144
+
````warn header="Declaring twice triggers an error"
145
+
A variable should be declared only once.
146
+
147
+
A repeated declaration of the same variable is an error:
148
+
149
+
```js run
150
+
let message = "This";
151
+
152
+
// repeated 'let' leads to an error
153
+
let message = "That"; // SyntaxError: 'message' has already been declared
154
+
```
155
+
So, we should declare a variable once and then refer to it without `let`.
156
+
````
157
+
158
+
```smart header="Functional languages"
159
+
It's interesting to note that there exist [functional](https://en.wikipedia.org/wiki/Functional_programming) programming languages, like [Scala](http://www.scala-lang.org/) or [Erlang](http://www.erlang.org/) that forbid changing variable values.
160
+
>>>>>>> d35baee32dcce127a69325c274799bb81db1afd8
137
161
138
162
در این نوع زبانها وقتی مقداری را در جعبه قرار میدهیم تا ابد آنجا میماند و اگر بخواهیم مقداری دیگر را ذخیره کنیم، ما را مجبور میکنند تا جعبهای جدید بسازیم.
139
163
@@ -188,7 +212,11 @@ let имя = '...';
188
212
let 我 = '...';
189
213
```
190
214
215
+
<<<<<<< HEAD
191
216
از نظر فنی اینها درست کار میکنند ولی بر اساس یک رسم بینالمللی برای نام متغیرها از زبان انگلیسی استفاده میشود. حتی اگر اسکریپتی کوچک مینویسیم، ممکن است تا مدت طولانیای مورد استفاده و توسعه قرار بگیرد و اشخاصی از سایر کشورها ممکن است نیاز باشد روزی آن اسکریپت را بخوانند.
217
+
=======
218
+
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.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/04-var/article.md
+34-9Lines changed: 34 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,27 +13,28 @@ In the very first chapter about [variables](info:variables), we mentioned three
13
13
2.`const`
14
14
3.`var`
15
15
16
+
<<<<<<< HEAD
16
17
`let` and `const` behave exactly the same way in terms of Lexical Environments.
17
18
18
19
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
20
21
If you don't plan 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
22
23
From the first sight, `var` behaves similar to `let`. That is, declares a variable:
24
+
=======
25
+
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:
26
+
>>>>>>> d35baee32dcce127a69325c274799bb81db1afd8
23
27
24
28
```js run
25
-
functionsayHi() {
26
-
var phrase ="Hello"; // local variable, "var" instead of "let"
29
+
var message ="Hi";
30
+
alert(message); // Hi
31
+
```
27
32
28
-
alert(phrase); // Hello
29
-
}
33
+
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.
30
34
31
-
sayHi();
35
+
If you don't plan on meeting such scripts you may even skip this chapter or postpone it.
32
36
33
-
alert(phrase); // Error, phrase is not defined
34
-
```
35
-
36
-
...But here are the differences.
37
+
On the other hand, it's important to understand differences when migrating old scripts from `var` to `let`, to avoid odd errors.
37
38
38
39
## "var" has no block scope
39
40
@@ -82,7 +83,31 @@ alert(phrase); // Error: phrase is not defined
82
83
83
84
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.
84
85
86
+
<<<<<<< HEAD
85
87
## "var" are processed at the function start
88
+
=======
89
+
## "var" tolerates redeclarations
90
+
91
+
If we declare the same variable with `let` twice in the same scope, that's an error:
92
+
93
+
```js run
94
+
let user;
95
+
let user; // SyntaxError: 'user' has already been declared
96
+
```
97
+
98
+
With `var`, we can redeclare a variable any number of times. If we use `var` with an already-declared variable, it's just ignored:
99
+
100
+
```js run
101
+
var user ="Pete";
102
+
103
+
var user ="John"; // this "var" does nothing (already declared)
104
+
// ...it doesn't trigger an error
105
+
106
+
alert(user); // John
107
+
```
108
+
109
+
## "var" variables can be declared below their use
110
+
>>>>>>> d35baee32dcce127a69325c274799bb81db1afd8
86
111
87
112
`var` declarations are processed when the function starts (or script starts for globals).
0 commit comments