Skip to content

Commit 89183a0

Browse files
committed
minor fixes
1 parent 80956ed commit 89183a0

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

1-js/02-first-steps/02-structure/article.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,40 +56,36 @@ Errors which occur in such cases are quite hard to find and fix.
5656
If you're curious to see a concrete example of such an error, check this code out:
5757
5858
```js run
59-
[1, 2].forEach(alert)
59+
alert("Hello);
60+
61+
[1, 2].forEach(alert);
6062
```
6163
62-
No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of the code: it shows `1` then `2`.
64+
No need to think about the meaning of the brackets `[]` and `forEach` yet. We'll study them later. For now, just remember the result of running the code: it shows `Hello`, then `1`, then `2`.
6365
64-
Let's add an `alert` before the code and *not* finish it with a semicolon:
66+
Now let's remove the semicolon after the `alert`:
6567
6668
```js run no-beautify
67-
alert("There will be an error")
69+
alert("Hello")
6870
69-
[1, 2].forEach(alert)
71+
[1, 2].forEach(alert);
7072
```
7173
72-
Now if we run the code, only the first `alert` shows, and then we have an error!
73-
74-
But everything is fine again if we add a semicolon after `alert`:
75-
```js run
76-
alert("All fine now");
74+
The difference compared to the code above is only one character: the semicolon at the end of the first line is gone.
7775
78-
[1, 2].forEach(alert)
79-
```
76+
If we run this code, only the first `Hello` shows. There are no numbers any more.
8077
81-
Now we have the "All fine now" message followed by `1` and `2`.
78+
The difference is because JavaScript does not assume a semicolon before square brackets `[...]`. And, as there's no semicolon, the code in the first example is treated as a single statement.
8279
83-
84-
The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets `[...]`.
85-
86-
So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here's how the engine sees it:
80+
Here's how the engine sees it:
8781
8882
```js run no-beautify
89-
alert("There will be an error")[1, 2].forEach(alert)
83+
alert("Hello")[1, 2].forEach(alert)
9084
```
9185
92-
But it should be two separate statements, not one. Such a merging in this case is just wrong, hence the error. This can happen in other situations.
86+
Looks weird, right? Such merging in this case is just wrong. We need to put a semicolon after `alert` for the code to work correctly.
87+
88+
This can happen in other situations also.
9389
````
9490

9591
We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of the time. But it's safer -- especially for a beginner -- to use them.

0 commit comments

Comments
 (0)