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/02-structure/article.md
+15-19Lines changed: 15 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -56,40 +56,36 @@ Errors which occur in such cases are quite hard to find and fix.
56
56
If you're curious to see a concrete example of such an error, check this code out:
57
57
58
58
```js run
59
-
[1, 2].forEach(alert)
59
+
alert("Hello);
60
+
61
+
[1, 2].forEach(alert);
60
62
```
61
63
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`.
63
65
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`:
65
67
66
68
```js run no-beautify
67
-
alert("There will be an error")
69
+
alert("Hello")
68
70
69
-
[1, 2].forEach(alert)
71
+
[1, 2].forEach(alert);
70
72
```
71
73
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.
77
75
78
-
[1, 2].forEach(alert)
79
-
```
76
+
If we run this code, only the first `Hello` shows. There are no numbers any more.
80
77
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.
82
79
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:
87
81
88
82
```js run no-beautify
89
-
alert("There will be an error")[1, 2].forEach(alert)
83
+
alert("Hello")[1, 2].forEach(alert)
90
84
```
91
85
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.
93
89
````
94
90
95
91
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