minor fixes

This commit is contained in:
Ilya Kantor 2021-05-16 18:08:21 +03:00
parent 70049c3fdf
commit 859fa9bedd

View file

@ -58,29 +58,29 @@ If you're curious to see a concrete example of such an error, check this code ou
```js run ```js run
alert("Hello"); alert("Hello");
["World"].forEach(alert); [1, 2].forEach(alert);
``` ```
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 `World`. 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`.
Now let's remove the semicolon after the `alert`: Now let's remove the semicolon after the `alert`:
```js run no-beautify ```js run no-beautify
alert("Hello") alert("Hello")
["World"].forEach(alert); [1, 2].forEach(alert);
``` ```
The difference compared to the code above is only one character: the semicolon at the end of the first line is gone. The difference compared to the code above is only one character: the semicolon at the end of the first line is gone.
If we run this code, only the first `Hello` shows. There are no numbers any more. If we run this code, only the first `Hello` shows (and then you may see the error, but it may also be hidden). There are no numbers any more.
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. 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.
Here's how the engine sees it: Here's how the engine sees it:
```js run no-beautify ```js run no-beautify
alert("Hello")["World"].forEach(alert) alert("Hello")[1, 2].forEach(alert)
``` ```
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. 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.