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/11-logical-operators/article.md
+49-49
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
-
# Logical operators
1
+
# Operator logika
2
2
3
-
There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
3
+
Ada tiga operator logika di JavaScript: `||` (OR), `&&` (AND), `!` (NOT).
4
4
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.
6
6
7
-
Let's see the details.
7
+
Mari kita lihat detilnya.
8
8
9
9
## || (OR)
10
10
11
-
The "OR" operator is represented with two vertical line symbols:
11
+
Operator "OR" diwakili dengan dua simbol garis vertical:
12
12
13
13
```js
14
14
result = a || b;
15
15
```
16
16
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`.
18
18
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.
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`.
31
31
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.
33
33
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`:
35
35
36
36
```js run
37
-
if (1||0) { //works just like if( true || false )
37
+
if (1||0) { //bekerja seperti if( true || false )
38
38
alert( 'truthy!' );
39
39
}
40
40
```
41
41
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`.
43
43
44
-
For example:
44
+
Misalnya:
45
45
46
46
```js run
47
47
let hour =9;
@@ -53,57 +53,57 @@ if (hour < 10 || hour > 18) {
53
53
}
54
54
```
55
55
56
-
We can pass more conditions:
56
+
Kita bisa menyampaikan kondisi lebih:
57
57
58
58
```js run
59
59
let hour =12;
60
60
let isWeekend =true;
61
61
62
62
if (hour <10|| hour >18|| isWeekend) {
63
-
alert( 'The office is closed.' ); //it is the weekend
63
+
alert( 'The office is closed.' ); //akhir minggu
64
64
}
65
65
```
66
66
67
-
## OR finds the first truthy value
67
+
## OR mencari nilai truthy pertama
68
68
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.
70
70
71
-
The extended algorithm works as follows.
71
+
Algoritma luas bekerja seperti berikut.
72
72
73
-
Given multiple OR'ed values:
73
+
Untuk nilai yang diORkan:
74
74
75
75
```js
76
76
result = value1 || value2 || value3;
77
77
```
78
78
79
-
The OR `||`operator does the following:
79
+
Operator OR `||`melakukan hal berikut:
80
80
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.
84
84
85
-
A value is returned in its original form, without the conversion.
85
+
Nilai dikembalikan di bentuk originalnya, tanpa konversi.
86
86
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.
88
88
89
-
For instance:
89
+
Misalnya:
90
90
91
91
```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)
94
94
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)
98
98
```
99
99
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".
101
101
102
-
1.**Getting the first truthy value from a list of variables or expressions.**
102
+
1.**Dapatkan nilai truthy dari daftar variabel atau expresi.**
103
103
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?
105
105
106
-
We can use OR `||`:
106
+
Kita bisa gunakan OR `||`:
107
107
108
108
```js run
109
109
let currentUser =null;
@@ -113,27 +113,27 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
113
113
let name = currentUser || defaultUser ||"unnamed";
114
114
*/!*
115
115
116
-
alert( name ); //selects "John" – the first truthy value
116
+
alert( name ); //memilih "John" – nilai truthy pertama
117
117
```
118
118
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.**
121
121
122
-
Operands can be not only values, but arbitrary expressions. ORevaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. Thisprocess 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. ORmengevaluasi 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.
123
123
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.
125
125
126
-
In the example below, `x`does not get assigned:
126
+
Di contoh di bawah, `x`tidak ditetapkan:
127
127
128
128
```js run no-beautify
129
129
let x;
130
130
131
131
*!*true*/!* || (x = 1);
132
132
133
-
alert(x); // undefined, because (x = 1) not evaluated
133
+
alert(x); // undefined, karena (x = 1) tak dievaluasi
134
134
```
135
135
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:
137
137
138
138
```js run no-beautify
139
139
let x;
@@ -143,21 +143,21 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
143
143
alert(x); // 1
144
144
```
145
145
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.
147
147
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 iafalse, yang kedua dievaluasi.
149
149
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.
151
151
152
152
## && (AND)
153
153
154
-
TheANDoperator is represented with two ampersands`&&`:
154
+
OperatorANDdiwakili dua ampersand`&&`:
155
155
156
156
```js
157
157
result = a && b;
158
158
```
159
159
160
-
In classical programming, ANDreturns`true`if both operands are truthy and`false`otherwise:
160
+
Dalam pemrograman klasik, ANDmengembalikan`true`jika kedua operand sama-sama truthy dan`false`jika sebaliknya:
0 commit comments