This commit is contained in:
Ilya Kantor 2017-03-24 17:28:37 +03:00
parent c9401b3104
commit 0fcf9f84fa
58 changed files with 673 additions and 643 deletions

View file

@ -1,6 +1,6 @@
To be precise, the `if` must use a strict comparison `'==='`.
To precisely match the functionality of `switch`, the `if` must use a strict comparison `'==='`.
In reality though, probably a simple `'=='` would do.
For given strings though, a simple `'=='` works too.
```js no-beautify
if(browser == 'Edge') {
@ -17,4 +17,4 @@ if(browser == 'Edge') {
Please note: the construct `browser == 'Chrome' || browser == 'Firefox' …` is split into multiple lines for better readability.
But the `switch` is still neater and more descriptive.
But the `switch` construct is still cleaner and more descriptive.

View file

@ -1,4 +1,4 @@
The first two checks are a usual `case`. The third one is split into two cases:
The first two checks turn into two `case`. The third check is split into two cases:
```js run
let a = +prompt('a?', '');
@ -24,4 +24,3 @@ switch (a) {
Please note: the `break` at the bottom is not required. But we put it to make the code future-proof.
In the future, there is a chance that we'd want to add one more `case`, for example `case 4`. And if we forget to add a break before it, at the end of `case 3`, there will be an error. So that's a kind of self-insurance.

View file

@ -8,7 +8,7 @@ It gives a more descriptive way to compare a value with multiple variants.
## The syntax
The `switch` has one or more `case` blocks and an optional default.
The `switch` has one or more `case` blocks and an optional default.
It looks like this:
@ -142,7 +142,7 @@ Now both `3` and `5` show the same message.
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`.
## The type matters
## Type matters
Let's emphase that the equality check is always strict. The values must be of the same type to match.
@ -170,4 +170,3 @@ switch (arg) {
1. For `0`, `1`, the first `alert` runs.
2. For `2` the second `alert` runs.
3. But for `3`, the result of the `prompt` is a string `"3"`, which is not strictly equal `===` to the number `3`. So we've got a dead code in `case 3`! The `default` variant will execite.