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/09-alert-prompt-confirm/article.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
This part of the tutorial aims to cover JavaScript "as is", without environment-specific tweaks.
4
4
5
-
But still we use a browser as the demo environment. So we should know at least few user-interface functions. In this chapter we'll get familiar with the browser functions `alert`, `prompt` and `confirm`.
5
+
But still we use a browser as the demo environment. So we should know at least a few user-interface functions. In this chapter we'll get familiar with the browser functions `alert`, `prompt` and `confirm`.
6
6
7
7
[cut]
8
8
@@ -40,7 +40,7 @@ It shows a modal window with a text message, an input field for the visitor and
40
40
`default`
41
41
: An optional second parameter, the initial value for the input field.
42
42
43
-
The visitor may type something in the prompt input field and press OK. Or they can cancel the input by pressing a CANCEL button or hitting the `key:Esc` key.
43
+
The visitor may type something in the prompt input field and press OK. Or they can cancel the input by pressing the CANCEL button or hitting the `key:Esc` key.
44
44
45
45
The call to `prompt` returns the text from the field or `null` if the input was canceled.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/10-ifelse/article.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
Sometimes we need to perform different actions based on a condition.
4
4
5
-
There is `if` statement for that and also the conditional (ternary) operator for conditional evaluation which we will be referring as “question mark” operator: `"?"` for simplicity.
5
+
There is the `if` statement for that and also the conditional (ternary) operator for conditional evaluation which we will be referring as the “question mark” operator: `"?"` for simplicity.
6
6
7
7
[cut]
8
8
9
9
## The "if" statement
10
10
11
-
The "if" statement gets a condition, evaluates it and -- if the result is `true` -- executes the code.
11
+
The "if" statement gets a condition, evaluates it and, if the result is `true`, executes the code.
12
12
13
13
For example:
14
14
@@ -22,7 +22,7 @@ if (year == 2015) alert( 'You are right!' );
22
22
23
23
In the example above, the condition is a simple equality check: `year == 2015`, but it can be much more complex.
24
24
25
-
If there is more than one command to execute -- we can use a code block in figure brackets:
25
+
If there is more than one command to execute, we can use a code block in figure brackets:
26
26
27
27
```js
28
28
if (year ==2015) {
@@ -101,7 +101,7 @@ if (year < 2015) {
101
101
}
102
102
```
103
103
104
-
In the code above JavaScript first checks `year < 2015`, if it is falsy then goes to the next condition `year > 2015`, and otherwise shows the last `alert`.
104
+
In the code above JavaScript first checks `year < 2015`. If it is falsy it then goes to the next condition `year > 2015`, and otherwise shows the last `alert`.
105
105
106
106
There can be more `else if` blocks. The ending `else` is optional.
107
107
@@ -128,7 +128,7 @@ alert(accessAllowed);
128
128
129
129
The so-called "ternary" or "question mark" operator lets us do that shorter and simpler.
130
130
131
-
The operator is represented by a question mark `"?"`. The formal term "ternary" means that the operator has 3 operands. It is actually the one and only operator in JavaScript which has that many.
131
+
The operator is represented by a question mark `"?"`. The formal term "ternary" means that the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
132
132
133
133
The syntax is:
134
134
```js
@@ -151,7 +151,7 @@ Technically, we can omit parentheses around `age > 18`. The question mark operat
151
151
let accessAllowed = age >18?true:false;
152
152
```
153
153
154
-
...But parentheses make the code more readable. So it's recommended to put them.
154
+
...But parentheses make the code more readable. So it's recommended to use them.
155
155
156
156
````smart
157
157
In the example above it's possible to evade the question mark operator, because the comparison by itself returns `true/false`:
@@ -214,13 +214,13 @@ let company = prompt('Which company created JavaScript?', '');
214
214
215
215
Depending on the condition `company == 'Netscape'`, either the first or the second part after `"?"` gets executed and shows the alert.
216
216
217
-
We don't assign a result to a variable here, the idea is to execute different code depending on the condition.
217
+
We don't assign a result to a variable here. The idea is to execute different code depending on the condition.
218
218
219
219
**It is not recommended to use the question mark operator in this way.**
220
220
221
-
The notation seem to be shorter than `if`, that appeals to some programmers. But it is less readable.
221
+
The notation seems to be shorter than `if`, which appeals to some programmers. But it is less readable.
222
222
223
-
Here is the same with `if` for comparison:
223
+
Here is the same code with `if` for comparison:
224
224
225
225
```js run no-beautify
226
226
let company =prompt('Which company created JavaScript?', '');
- For each operand, convert it to boolean. If the result is `true`, then stop and return that the original value of that operand.
83
+
-Evaluate operands from left to right.
84
+
- For each operand, convert it to boolean. If the result is `true`, then stop and return the original value of that operand.
85
85
- If all other operands have been assessed (i.e. all were `falsy`), return the last operand.
86
86
87
87
A value is returned in its original form, without the conversion.
88
88
89
-
In other words, a chain of OR `"||"` returns the first truthy value or the last one if no such value found.
89
+
In other words, a chain of OR `"||"` returns the first truthy value or the last one if no such value is found.
90
90
91
91
For instance:
92
92
@@ -147,7 +147,7 @@ That leads to some interesting usages compared to a "pure, classical, boolean-on
147
147
148
148
An assignment is a simple case, other side effects can be involved.
149
149
150
-
As we can see, such use case is a "shorter way to do `if`". The first operand is converted to boolean and if it's false then the second one is evaluated.
150
+
As we can see, such a use case is a "shorter way to do `if`". The first operand is converted to boolean and if it's false then the second one is evaluated.
151
151
152
152
Most of time it's better to use a "regular"`if` to keep the code easy to understand, but sometimes that can be handy.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/12-while-for/article.md
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
We often have a need to perform similar actions many times in a row.
4
4
5
-
For example, when we need to output goods from the list one after another. Or just run the same code for each number from 1 to 10.
5
+
For example, when we need to output goods from a list one after another. Or just run the same code for each number from 1 to 10.
6
6
7
7
*Loops* are a way to repeat the same part of code multiple times.
8
8
@@ -19,7 +19,7 @@ while (condition) {
19
19
}
20
20
```
21
21
22
-
While the `condition` is `true` -- the `code` from the loop body is executed.
22
+
While the `condition` is `true`, the `code` from the loop body is executed.
23
23
24
24
For instance, the loop below outputs `i` while `i<3`:
25
25
@@ -31,7 +31,7 @@ while (i < 3) { // shows 0, then 1, then 2
31
31
}
32
32
```
33
33
34
-
A single execution of the loop body is called *an iteration*. The loop in the example above makes 3 iterations.
34
+
A single execution of the loop body is called *an iteration*. The loop in the example above makes three iterations.
35
35
36
36
If there were no `i++` in the example above, the loop would repeat (in theory) forever. In practice, the browser provides ways to stop such loops, and for server-side JavaScript we can kill the process.
37
37
@@ -70,7 +70,7 @@ do {
70
70
} while (condition);
71
71
```
72
72
73
-
The loop will first execute the body, then check the condition, and while it's truthy -- execute it again and again.
73
+
The loop will first execute the body, then check the condition and, while it's truthy, execute it again and again.
74
74
75
75
For example:
76
76
@@ -82,7 +82,7 @@ do {
82
82
} while (i <3);
83
83
```
84
84
85
-
This form of syntax is rarely used except when you want the body of the loop to execute **at least once** regardless of the condition being truthy. Usually, the other form is preferred: `while(…) {…}`.
85
+
This form of syntax is rarely used except when you want the body of the loop to execute **at least once** regardless of the condition being truthy. Usually, the other form is preferred: `while(…) {…}`.
86
86
87
87
## The "for" loop
88
88
@@ -231,7 +231,7 @@ alert( 'Sum: ' + sum );
231
231
232
232
The `break` directive is activated in the line `(*)` if the user enters an empty line or cancels the input. It stops the loop immediately, passing the control to the first line after the loop. Namely, `alert`.
233
233
234
-
The combination: "infinite loop + `break` as needed" is great for situations when the condition must be checked not in beginning/end of the loop, but in the middle. Or even in several places of the body.
234
+
The combination "infinite loop + `break` as needed" is great for situations when the condition must be checked not in the beginning/end of the loop, but in the middle, or even in several places of the body.
235
235
236
236
## Continue to the next iteration [#continue]
237
237
@@ -266,7 +266,7 @@ for (let i = 0; i < 10; i++) {
266
266
}
267
267
```
268
268
269
-
From the technical point of view it's identical to the example above. Surely, we can just wrap the code in the `if` block instead of `continue`.
269
+
From a technical point of view it's identical to the example above. Surely, we can just wrap the code in the `if` block instead of `continue`.
270
270
271
271
But as a side-effect we got one more figure brackets nesting level. If the code inside `if` is longer than a few lines, that may decrease the overall readability.
272
272
````
@@ -360,12 +360,12 @@ outer:
360
360
for (let i =0; i <3; i++) { ... }
361
361
```
362
362
363
-
The `continue` directive can also be used with a label. In this case the execution jumps to the next iteration of the labelled loop.
363
+
The `continue` directive can also be used with a label. In this case the execution jumps to the next iteration of the labeled loop.
364
364
365
365
````warn header="Labels are not a \"goto\""
366
-
Labels do not allow to jump into an arbitrary place of code.
366
+
Labels do not allow us to jump into an arbitrary place of code.
367
367
368
-
For example, it is impossible to do like this:
368
+
For example, it is impossible to do this:
369
369
```js
370
370
break label; // jumps to label? No.
371
371
@@ -379,12 +379,12 @@ The call to a `break/continue` is only possible from inside the loop, and the la
379
379
380
380
We covered 3 types of loops:
381
381
382
-
-`while` -- the condition is checked before each iteration.
383
-
-`do..while` -- the condition is checked after each iteration.
384
-
-`for(;;)` -- the condition is checked before each iteration, additional settings available.
382
+
-`while` -- The condition is checked before each iteration.
383
+
-`do..while` -- The condition is checked after each iteration.
384
+
-`for(;;)` -- The condition is checked before each iteration, additional settings available.
385
385
386
386
To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` directive.
387
387
388
-
If we don't want to do anything on the current iteration and would like to forward to the next one -- the `continue` directive does it.
388
+
If we don't want to do anything on the current iteration and would like to forward to the next one, the `continue` directive does it.
389
389
390
390
`Break/continue` support labels before the loop. A label is the only way for `break/continue` to escape the nesting and go to the outer loop.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/13-switch/article.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -28,9 +28,9 @@ switch(x) {
28
28
}
29
29
```
30
30
31
-
- The value of `x` is checked for a strict equality to the value from the first `case`, that is:`value1`, then to the second `value2` and so on.
32
-
- If the equality is found -- `switch` starts to execute the code starting from the corresponding `case`, and to the nearest `break` (or to the end of `switch`).
33
-
- If no case matched then the `default` code is executed (if exists).
31
+
- The value of `x` is checked for a strict equality to the value from the first `case` (that is,`value1`) then to the second (`value2`) and so on.
32
+
- If the equality is found, `switch` starts to execute the code starting from the corresponding `case`, until the nearest `break` (or until the end of `switch`).
33
+
- If no case is matched then the `default` code is executed (if it exists).
34
34
35
35
## An example
36
36
@@ -58,7 +58,7 @@ switch (a) {
58
58
59
59
Here the `switch` starts to compare `a` from the first `case` variant that is `3`. The match fails.
60
60
61
-
Then `4`. That's the match, so the execution starts from `case 4`and till the nearest `break`.
61
+
Then `4`. That's a match, so the execution starts from `case 4`until the nearest `break`.
62
62
63
63
**If there is no `break` then the execution continues with the next `case` without any checks.**
64
64
@@ -89,8 +89,8 @@ alert( 'Too big' );
89
89
alert( "I don't know such values" );
90
90
```
91
91
92
-
````smart header="Any expresion can be a `switch/case` argument"
93
-
Both `switch` and case allow arbitrary expressions.
92
+
````smart header="Any expression can be a `switch/case` argument"
93
+
Both `switch` and `case` allow arbitrary expressions.
94
94
95
95
For example:
96
96
@@ -141,7 +141,7 @@ switch (a) {
141
141
142
142
Now both `3` and `5` show the same message.
143
143
144
-
The ability to "group" cases a side-effect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
144
+
The ability to "group" cases is a side-effect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
0 commit comments