Skip to content

Commit 1dfc9ef

Browse files
committed
feat(i18n): 1.02.11.article - つづく
1 parent 1aabb5d commit 1dfc9ef

File tree

1 file changed

+49
-49
lines changed
  • 1-js/02-first-steps/11-logical-operators

1 file changed

+49
-49
lines changed

Diff for: 1-js/02-first-steps/11-logical-operators/article.md

+49-49
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
# Logical operators
1+
# Operator logika
22

3-
There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
3+
Ada tiga operator logika di JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
44

5-
Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type.
5+
Meski mereka dipanggil "logika", mereka bisa diaplikasikan ke nilai tipe apapun, bukan cuma boolean. Hasil mereka bisa juga tipe apapun.
66

7-
Let's see the details.
7+
Mari kita lihat detilnya.
88

99
## || (OR)
1010

11-
The "OR" operator is represented with two vertical line symbols:
11+
Operator "OR" diwakili dengan dua simbol garis vertical:
1212

1313
```js
1414
result = a || b;
1515
```
1616

17-
In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are `true`, it returns `true`, otherwise it returns `false`.
17+
Di pemrograman klasik, logika OR gunanya cuma untuk memanipulasi nilai boolean. Jika argumennya ada yang `true`, ia mengembalikan `true`, tapi jika tidak, maka ia mengembalikan `false`.
1818

19-
In JavaScript, the operator is a little bit trickier and more powerful. But first, let's see what happens with boolean values.
19+
Di JavaScript, operator ini agak tricky dan lebih kuat. Tapi pertama-tama, ayo kita lihat apa yang terjadi pada nilai boolean.
2020

21-
There are four possible logical combinations:
21+
Ada empat kemungkinan kombinasi logika:
2222

2323
```js run
2424
alert( true || true ); // true
@@ -27,21 +27,21 @@ alert( true || false ); // true
2727
alert( false || false ); // false
2828
```
2929

30-
As we can see, the result is always `true` except for the case when both operands are `false`.
30+
Seperti yang kita lihat, hasilnya selalu `true` kecuali jika kedua operand sama-sama `false`.
3131

32-
If an operand is not a boolean, it's converted to a boolean for the evaluation.
32+
Jika operand bukan boolean, ia dikonversi ke boolean untuk evaluasi.
3333

34-
For instance, the number `1` is treated as `true`, the number `0` as `false`:
34+
Misalnya, angka `1` diperlakukan sebagai `true`, angka `0` sebagai `false`:
3535

3636
```js run
37-
if (1 || 0) { // works just like if( true || false )
37+
if (1 || 0) { // bekerja seperti if( true || false )
3838
alert( 'truthy!' );
3939
}
4040
```
4141

42-
Most of the time, OR `||` is used in an `if` statement to test if *any* of the given conditions is `true`.
42+
Seringkali, OR `||` digunakan di pernyataan `if` untuk menguji apakah ada satu kondisi *manapun* yang `true`.
4343

44-
For example:
44+
Misalnya:
4545

4646
```js run
4747
let hour = 9;
@@ -53,57 +53,57 @@ if (hour < 10 || hour > 18) {
5353
}
5454
```
5555

56-
We can pass more conditions:
56+
Kita bisa menyampaikan kondisi lebih:
5757

5858
```js run
5959
let hour = 12;
6060
let isWeekend = true;
6161

6262
if (hour < 10 || hour > 18 || isWeekend) {
63-
alert( 'The office is closed.' ); // it is the weekend
63+
alert( 'The office is closed.' ); // akhir minggu
6464
}
6565
```
6666

67-
## OR finds the first truthy value
67+
## OR mencari nilai truthy pertama
6868

69-
The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript.
69+
Logika di atas memang klasik. Sekarang, mari bawa fitur "extra" JavaScript.
7070

71-
The extended algorithm works as follows.
71+
Algoritma luas bekerja seperti berikut.
7272

73-
Given multiple OR'ed values:
73+
Untuk nilai yang diORkan:
7474

7575
```js
7676
result = value1 || value2 || value3;
7777
```
7878

79-
The OR `||` operator does the following:
79+
Operator OR `||` melakukan hal berikut:
8080

81-
- Evaluates operands from left to right.
82-
- For each operand, converts it to boolean. If the result is `true`, stops and returns the original value of that operand.
83-
- If all operands have been evaluated (i.e. all were `false`), returns the last operand.
81+
- Mengevaluasi operand dari kiri ke kanan.
82+
- Untuk tiap operand, konversikan ia ke boolean. Jika hasilnya `true`, stop dan mengembalikan nilai original dari operand.
83+
- Jika semua operand telah dievaluasi (misal semuanya `false`), mengembalikan operand terakhir.
8484

85-
A value is returned in its original form, without the conversion.
85+
Nilai dikembalikan di bentuk originalnya, tanpa konversi.
8686

87-
In other words, a chain of OR `"||"` returns the first truthy value or the last one if no truthy value is found.
87+
Dengan kata lain, rantai OR `"||"` mengembalikan nilai truthy pertama atau yang terakhir jika tak ada nilai truthy.
8888

89-
For instance:
89+
Misalnya:
9090

9191
```js run
92-
alert( 1 || 0 ); // 1 (1 is truthy)
93-
alert( true || 'no matter what' ); // (true is truthy)
92+
alert( 1 || 0 ); // 1 (1 truthy)
93+
alert( true || 'no matter what' ); // (true ialah truthy)
9494

95-
alert( null || 1 ); // 1 (1 is the first truthy value)
96-
alert( null || 0 || 1 ); // 1 (the first truthy value)
97-
alert( undefined || null || 0 ); // 0 (all falsy, returns the last value)
95+
alert( null || 1 ); // 1 (1 ialah nilai truthy pertama)
96+
alert( null || 0 || 1 ); // 1 (nilai truthy pertama)
97+
alert( undefined || null || 0 ); // 0 (semua falsy, mengembalikan nilai terakhir)
9898
```
9999

100-
This leads to some interesting usage compared to a "pure, classical, boolean-only OR".
100+
Hal ini menjadikan penggunaan yang menarik dibanding "OR booleanpure, classical, boolean-only OR".
101101

102-
1. **Getting the first truthy value from a list of variables or expressions.**
102+
1. **Dapatkan nilai truthy dari daftar variabel atau expresi.**
103103

104-
Imagine we have a list of variables which can either contain data or be `null/undefined`. How can we find the first one with data?
104+
Bayangkan kita punya daftar variabel yang bisa berisi data atau `null/undefined`. Bagaimana cara kita mencari data pertama?
105105

106-
We can use OR `||`:
106+
Kita bisa gunakan OR `||`:
107107

108108
```js run
109109
let currentUser = null;
@@ -113,27 +113,27 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
113113
let name = currentUser || defaultUser || "unnamed";
114114
*/!*
115115

116-
alert( name ); // selects "John" – the first truthy value
116+
alert( name ); // memilih "John" – nilai truthy pertama
117117
```
118118

119-
If both `currentUser` and `defaultUser` were falsy, `"unnamed"` would be the result.
120-
2. **Short-circuit evaluation.**
119+
Jika kedua `currentUser` dan `defaultUser` sama-sama falsy, `"unnamed"` akan menjadi hasilnya.
120+
2. **Evaluasi short-circuit.**
121121

122-
Operands can be not only values, but arbitrary expressions. OR evaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. This process is called "a short-circuit evaluation" because it goes as short as possible from left to right.
122+
Operand bukan hanya nilai, tapi juga expresi sembarang. OR mengevaluasi dan menguji mereka dari kiri ke kanan. Evaluasinya berhenti ketika nilai truthy tercapai, dan nilainya dikembalikan. Proses ini disebut "evaluasi short-circuit" karena ia berjalan sependek mungkin dari kiri ke kanan.
123123

124-
This is clearly seen when the expression given as the second argument has a side effect like a variable assignment.
124+
Ini jelas-jelas terlihat ketika expresi yang diberikan sebagai argumen kedua punya efek samping seperti penetapan variabel.
125125

126-
In the example below, `x` does not get assigned:
126+
Di contoh di bawah, `x` tidak ditetapkan:
127127

128128
```js run no-beautify
129129
let x;
130130
131131
*!*true*/!* || (x = 1);
132132
133-
alert(x); // undefined, because (x = 1) not evaluated
133+
alert(x); // undefined, karena (x = 1) tak dievaluasi
134134
```
135135

136-
If, instead, the first argument is `false`, `||` evaluates the second one, thus running the assignment:
136+
Jika, argumen pertama `false`, `||` mengevaluasi nilai kedua, maka penetapan ini:
137137

138138
```js run no-beautify
139139
let x;
@@ -143,21 +143,21 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
143143
alert(x); // 1
144144
```
145145

146-
An assignment is a simple case. There may be side effects, that won't show up if the evaluation doesn't reach them.
146+
Penetapan merupakan hal simpel. Bisa jadi ada efek samping, yang tak akan muncul jika evaluasinya tidak mencapainya.
147147

148-
As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated.
148+
Seperti yang bisa kita lihat, use case macam ini ialah "cara terpendek melakukan `if`". Operand pertama dikonversi ke boolean. Jika ia false, yang kedua dievaluasi.
149149

150-
Most of time, it's better to use a "regular" `if` to keep the code easy to understand, but sometimes this can be handy.
150+
Seringkali, lebih baik menggunakan `if` "reguler" supaya kodenya mudah dipahami, tapi kadang bisa jadi berguna.
151151

152152
## && (AND)
153153

154-
The AND operator is represented with two ampersands `&&`:
154+
Operator AND diwakili dua ampersand `&&`:
155155

156156
```js
157157
result = a && b;
158158
```
159159

160-
In classical programming, AND returns `true` if both operands are truthy and `false` otherwise:
160+
Dalam pemrograman klasik, AND mengembalikan `true` jika kedua operand sama-sama truthy dan `false` jika sebaliknya:
161161

162162
```js run
163163
alert( true && true ); // true

0 commit comments

Comments
 (0)