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
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/04-variables/article.md
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -88,16 +88,16 @@ In older scripts, you may also find another keyword: `var` instead of `let`:
88
88
*!*var*/!* message ='Hello';
89
89
```
90
90
91
-
The `var` keyword is *almost* the same as `let`. It also declares a variable, but in a slightly different, "old-school" way.
91
+
The `var` keyword is *almost* the same as `let`. It also declares a variable but in a slightly different, "old-school" way.
92
92
93
-
There are subtle differences between `let` and `var`, but they do not matter for us yet. We'll cover them in detail in the chapter <info:var>.
93
+
There are subtle differences between `let` and `var`, but they do not matter to us yet. We'll cover them in detail in the chapter <info:var>.
94
94
````
95
95
96
96
## A real-life analogy
97
97
98
98
We can easily grasp the concept of a "variable" if we imagine it as a "box" for data, with a uniquely-named sticker on it.
99
99
100
-
For instance, the variable `message` can be imagined as a box labeled `"message"` with the value `"Hello!"` in it:
100
+
For instance, the variable `message` can be imagined as a box labelled `"message"` with the value `"Hello!"` in it:
101
101
102
102

103
103
@@ -198,14 +198,14 @@ Variables named `apple` and `APPLE` are two different variables.
198
198
```
199
199
200
200
````smart header="Non-Latin letters are allowed, but not recommended"
201
-
It is possible to use any language, including cyrillic letters, Chinese logograms and so on, like this:
201
+
It is possible to use any language, including Cyrillic letters, Chinese logograms and so on, like this:
202
202
203
203
```js
204
204
let имя = '...';
205
205
let 我 = '...';
206
206
```
207
207
208
-
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.
208
+
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 sometime.
myBirthday = '01.01.2001'; // error, can't reassign the constant!
261
261
```
262
262
263
-
When a programmer is sure that a variable will never change, they can declare it with `const` to guarantee and clearly communicate that fact to everyone.
263
+
When a programmer is sure that a variable will never change, they can declare it with `const` to guarantee and communicate that fact to everyone.
264
264
265
265
### Uppercase constants
266
266
267
-
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution.
267
+
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known before execution.
268
268
269
269
Such constants are named using capital letters and underscores.
270
270
@@ -289,15 +289,15 @@ Benefits:
289
289
290
290
When should we use capitals for a constant and when should we name it normally? Let's make that clear.
291
291
292
-
Being a "constant" just means that a variable's value never changes. But there are constants that are known prior to execution (like a hexadecimal value for red) and there are constants that are *calculated* in run-time, during the execution, but do not change after their initial assignment.
292
+
Being a "constant" just means that a variable's value never changes. But some constants are known before execution (like a hexadecimal value for red) and some constants are *calculated* in run-time, during the execution, but do not change after their initial assignment.
293
293
294
294
For instance:
295
295
296
296
```js
297
297
const pageLoadTime = /* time taken by a webpage to load */;
298
298
```
299
299
300
-
The value of `pageLoadTime` is not known prior to the page load, so it's named normally. But it's still a constant because it doesn't change after assignment.
300
+
The value of `pageLoadTime` is not known before the page load, so it's named normally. But it's still a constant because it doesn't change after the assignment.
301
301
302
302
In other words, capital-named constants are only used as aliases for "hard-coded" values.
303
303
@@ -307,18 +307,18 @@ Talking about variables, there's one more extremely important thing.
307
307
308
308
A variable name should have a clean, obvious meaning, describing the data that it stores.
309
309
310
-
Variable naming is one of the most important and complex skills in programming. A quick glance at variable names can reveal which code was written by a beginner versus an experienced developer.
310
+
Variable naming is one of the most important and complex skills in programming. A glance at variable names can reveal which code was written by a beginner versus an experienced developer.
311
311
312
-
In a real project, most of the time is spent modifying and extending an existing code base rather than writing something completely separate from scratch. When we return to some code after doing something else for a while, it's much easier to find information that is well-labeled. Or, in other words, when the variables have good names.
312
+
In a real project, most of the time is spent modifying and extending an existing code base rather than writing something completely separate from scratch. When we return to some code after doing something else for a while, it's much easier to find information that is well-labelled. Or, in other words, when the variables have good names.
313
313
314
314
Please spend time thinking about the right name for a variable before declaring it. Doing so will repay you handsomely.
315
315
316
316
Some good-to-follow rules are:
317
317
318
318
- Use human-readable names like `userName` or `shoppingCart`.
319
-
- Stay away from abbreviations or short names like `a`, `b`, `c`, unless you really know what you're doing.
319
+
- Stay away from abbreviations or short names like `a`, `b`, and `c`, unless you know what you're doing.
320
320
- Make names maximally descriptive and concise. Examples of bad names are `data` and `value`. Such names say nothing. It's only okay to use them if the context of the code makes it exceptionally obvious which data or value the variable is referencing.
321
-
- Agree on terms within your team and in your own mind. If a site visitor is called a "user" then we should name related variables `currentUser` or `newUser` instead of `currentVisitor` or `newManInTown`.
321
+
- Agree on terms within your team and in your mind. If a site visitor is called a "user" then we should name related variables `currentUser` or `newUser` instead of `currentVisitor` or `newManInTown`.
322
322
323
323
Sounds simple? Indeed it is, but creating descriptive and concise variable names in practice is not. Go for it.
0 commit comments