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
It gives a more descriptive way to compare a value with multiple variants.
5
+
Ia memberi cara lebih deskriptif untuk membandingkan nilai dengan varian ganda.
6
6
7
-
## The syntax
7
+
## Syntax
8
8
9
-
The `switch`has one or more `case`blocks and an optional default.
9
+
`switch`punya satu atau lebih blok `case`dan satu default opsional.
10
10
11
-
It looks like this:
11
+
Rupanya seperti ini:
12
12
13
13
```js no-beautify
14
14
switch(x) {
@@ -26,13 +26,13 @@ switch(x) {
26
26
}
27
27
```
28
28
29
-
-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.
30
-
-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`).
31
-
-If no case is matched then the`default`code is executed (if it exists).
29
+
-Nilai `x`dicek ekaulitasnya secara ketat dengan nilaid dari `case`pertama (yaitu, `value1`) lalu ke kedua (`value2`) dan seterusnya.
30
+
-Jika ekualitas ditemukan, `switch`mulai mengexekusi kode mulai dari `case` yang berkorespondensi, hingga`break`terdekat (atau hingga akhir`switch`).
31
+
-Jika tak ada case yang cocok maka kode`default`diexekusi (jika ada).
32
32
33
-
## An example
33
+
## Contoh
34
34
35
-
An example of `switch` (the executed code is highlighted):
35
+
Contoh `switch` (kode yang diexekusi dihighlight):
36
36
37
37
```js run
38
38
let a =2+2;
@@ -54,13 +54,13 @@ switch (a) {
54
54
}
55
55
```
56
56
57
-
Here the`switch`starts to compare `a`from the first `case`variant that is`3`. The match fails.
57
+
Di sini`switch`mulai membandingkan `a`dari varian `case`pertama yang bernilai`3`. Kecocokan gagal.
58
58
59
-
Then`4`. That's a match, so the execution starts from `case 4`until the nearest `break`.
59
+
Lalu`4`. Ada kecocokan, jadi exekusi mulai dari `case 4`hingga `break` terdekat.
60
60
61
-
**If there is no `break`then the execution continues with the next `case`without any checks.**
61
+
**Jika tak ada `break`maka exekusi lanjut dengan `case`berikutnya tanpa pengecekan.**
62
62
63
-
An example without`break`:
63
+
Contoh tanpa`break`:
64
64
65
65
```js run
66
66
let a =2+2;
@@ -79,18 +79,18 @@ switch (a) {
79
79
}
80
80
```
81
81
82
-
In the example above we'll see sequential execution of three `alert`s:
82
+
Di contoh di atas kita akan lihat exekusi sekuensial dari tiga `alert`:
83
83
84
84
```js
85
85
alert( 'Exactly!' );
86
86
alert( 'Too big' );
87
87
alert( "I don't know such values" );
88
88
```
89
89
90
-
````smart header="Any expression can be a`switch/case` argument"
91
-
Both`switch`and`case`allow arbitrary expressions.
90
+
````smart header="Expresi apapun bisa berupa argumen`switch/case`"
Here `+a`gives`1`, that's compared with`b + 1`in`case`, and the corresponding code is executed.
110
+
Di sini `+a`memberikan`1`, yang dibandingkan dengan`b + 1`ndalam`case`, and the corresponding code is executed.
111
111
````
112
112
113
-
## Grouping of "case"
113
+
## Pengelompokan "case"
114
114
115
-
Several variants of `case` which share the same code can be grouped.
115
+
Beberapa varian `case` yang berbagi kode yang sama bisa dikelompokkan.
116
116
117
-
For example, if we want the same code to run for `case 3` and `case 5`:
117
+
Misalnya, jika kita ingin kode yang sama berjalan untuk `case 3` dan `case 5`:
118
118
119
119
```js run no-beautify
120
120
let a = 2 + 2;
@@ -137,15 +137,15 @@ switch (a) {
137
137
}
138
138
```
139
139
140
-
Now both `3` and `5` show the same message.
140
+
Sekarang baik `3` maupun `5` menampilkan pesan yang sama.
141
141
142
-
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`.
142
+
Kemampuan "mengelompokkan" case adalah efek samping dari bagaimana `switch/case` bekerja tanpa `break`. Di sini exekusi dari `case 3` mulai dari baris `(*)` dan tembus ke `case 5`, karena tidak ada `break`.
143
143
144
-
## Type matters
144
+
## Tipe berpengaruh
145
145
146
-
Let's emphasize that the equality check is always strict. The values must be of the same type to match.
146
+
Mari kita tekankan bahwa pengecekan ekualitas selalu ketat. Nilainya harus bertipe sama supaya cocok.
147
147
148
-
For example, let's consider the code:
148
+
Misalnya, mari kita pertimbangkan kode ini:
149
149
150
150
```js run
151
151
let arg = prompt("Enter a value?");
@@ -167,6 +167,6 @@ switch (arg) {
167
167
}
168
168
```
169
169
170
-
1. For `0`, `1`, the first `alert` runs.
171
-
2. For `2` the second `alert` runs.
172
-
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 execute.
170
+
1. Untuk `0`, `1`, `alert` pertama berjalan.
171
+
2. Untuk `2` `alert` kedua berjalan.
172
+
3. Tapi untuk `3`, hasil dari `prompt` ialah string `"3"`, yang berbeda secara ketat `===` dengan angka `3`. Jadi kita punya kode mati di `case 3`! Varian `default` akan diexekusi.
0 commit comments