1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
---
title: Operadors Lògics
slug: >-
conflicting/Web/JavaScript/Reference/Operators_af596841c6a3650ee514088f0e310901
translation_of: Web/JavaScript/Reference/Operators
translation_of_original: Web/JavaScript/Reference/Operators/Logical_Operators
original_slug: Web/JavaScript/Referencia/Operadors/Logical_Operators
---
<div>
<div>{{jsSidebar("Operators")}}</div>
</div>
<h2 id="Summary" name="Summary">Resum</h2>
<p>Els operadors lògics s'utilitzen normalment amb valors <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code> (lògics). En cas de serh-hi presents, retornen un valor booleà. Tot i així, els operadors <code>&&</code> i <code>||</code> retornen el valor d'un dels operands especificats, així que si aquests operadors es fan servir amb valors no booleans, poden retornar un valor no booleà.</p>
<h2 id="Descripció">Descripció</h2>
<p>Els operadors lògics es descriuren en la taula següent:</p>
<table class="fullwidth-table">
<tbody>
<tr>
<th>Operador</th>
<th>Ús</th>
<th>Descripció</th>
</tr>
<tr>
<td>AND lògic (<code>&&</code>)</td>
<td><code><em>expr1</em> && <em>expr2</em></code></td>
<td>Retorna <code>expr1 </code>si es pot convertir en false; sinó, retorna expr2. Així, quan es fa servir amb valors booleans, <code>&&</code> retorna true si ambdós operands són true; de ser el contrari, retorna false.</td>
</tr>
<tr>
<td>OR lògic(<code>||</code>)</td>
<td><code><em>expr1</em> || <em>expr2</em></code></td>
<td>Retorna <code>expr1</code> si es pot converir en true; de ser al contrari, retorna <code>expr2</code>. Així, quan s'usa amb valors Booleans, <code>||</code> retorna true si l'operand és true; si amdós són false, retorna false.</td>
</tr>
<tr>
<td>NOT lògic (<code>!</code>)</td>
<td><code>!<em>expr</em></code></td>
<td>Retorna false si el seu únic operand pot convertir-se a true; sinó, retorna true.</td>
</tr>
</tbody>
</table>
<p>Exemples d'expressions que es poden converir a false son aquelles que avaluen <code>null</code>, <code>0</code>, la cadena buida (""), o <code>undefined</code>.</p>
<p>Tot i que els operadors <code>&&</code> i <code>||</code> es poden fer servir amb operands que no siguin valors Booleans, poden ser considerats operadors Booleans ja que els valors que retornen sempre es poden convertir en valors Booleans.</p>
<h3 id="Short-Circuit_Evaluation" name="Short-Circuit_Evaluation">Avaluació de tipus curtcircuit</h3>
<p>Com a expressions lògiques que són, s'avaluen d'esquerra a dreta, they are tested for possible "short-circuit" evaluation fent servir les regles següents:</p>
<ul>
<li><code>false && (<em>quelcom)</em></code> is short-circuit evaluated to false.</li>
<li><code>true || (<em>quelcom)</em></code> is short-circuit evaluated to true.</li>
</ul>
<p>Les regles de la lògica garanteixen que aquestes avaluacions són sempre correctes. Fixeu-vos que la part <code><em>quelcom</em></code> d'adalt no s'avalua, així que els efectes secundaris d'això no tindràn efecte. Fixeu-vos també que la part de l'expressió de dalt anomenada <code><em>quelcom </em></code>és una expressió lògica simple (com s'indiquen amb els parèntesis).</p>
<p>Per exemple, les dues funcions següents són equivalents.</p>
<pre class="brush: js">function shortCircuitEvaluation() {
doSomething() || doSomethingElse()
}
function equivalentEvaluation() {
var flag = doSomething();
if (!flag) {
doSomethingElse();
}
}
</pre>
<p>Tot i així, les següents expressions no són equivalents degut a la <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence">precendència dels operadors</a>, i remarca la importància de requerir que l'operador de la dreta sigui una sola expressió (agrupada, si s'escau, per parèntesi).</p>
<pre class="brush: js">false && true || true // returns true
false && (true || true) // returns false</pre>
<h3 id="Logical_AND_.28&&.29" name="Logical_AND_.28&&.29"><a name="Logical_AND">AND lògic (<code>&&</code>)</a></h3>
<p>El codi següent mostra exemples de l'operador <code>&&</code> (AND lògic).</p>
<pre class="brush: js">a1 = true && true // t && t returns true
a2 = true && false // t && f returns false
a3 = false && true // f && t returns false
a4 = false && (3 == 4) // f && f returns false
a5 = "Cat" && "Dog" // t && t returns "Dog"
a6 = false && "Cat" // f && t returns false
a7 = "Cat" && false // t && f returns false
</pre>
<h3 id="OR_lògic_()"><a name="Logical_OR">OR lògic (<code>||</code>)</a></h3>
<p>El codi següent mostra exemples de l'operador <code>||</code> (OR lògic).</p>
<pre class="brush: js">o1 = true || true // t || t returns true
o2 = false || true // f || t returns true
o3 = true || false // t || f returns true
o4 = false || (3 == 4) // f || f returns false
o5 = "Cat" || "Dog" // t || t returns "Cat"
o6 = false || "Cat" // f || t returns "Cat"
o7 = "Cat" || false // t || f returns "Cat"
</pre>
<h3 id="Logical_NOT_.28.21.29" name="Logical_NOT_.28.21.29"><a name="Logical_NOT">NOT lògic (<code>!</code>)</a></h3>
<p>El codi següent mostra exemples de l'operador <code>!</code> (NOT lògic).</p>
<pre class="brush: js">n1 = !true // !t returns false
n2 = !false // !f returns true
n3 = !"Cat" // !t returns false
</pre>
<h3 id="Regles_de_conversió">Regles de conversió</h3>
<h4 id="Convertir_AND_a_OR">Convertir AND a OR</h4>
<p>L'operació següent que inclou Booleans:</p>
<pre class="brush: js">bCondition1 && bCondition2</pre>
<p>sempre és igual a:</p>
<pre class="brush: js">!(!bCondition1 || !bCondition2)</pre>
<h4 id="Convertir_OR_a_AND">Convertir OR a AND</h4>
<p>L'operació següent que inclou Booleans:</p>
<pre class="brush: js">bCondition1 || bCondition2</pre>
<p>sempre és igual a:</p>
<pre class="brush: js">!(!bCondition1 && !bCondition2)</pre>
<h3 id="Desfer-se_de_parèntesis_aniuats">Desfer-se de parèntesis aniuats</h3>
<p>Com que les expressions lògiques s'avaluen d'esquerra a dreta, sempre és posible esborrar els parèntesi d'expressions complexes mitjançant les regles següents.</p>
<h4 id="Desfer-se_d'un_AND_aniuat">Desfer-se d'un AND aniuat</h4>
<p>L'operació següent que inclou Booleans:</p>
<pre class="brush: js">bCondition1 || (bCondition2 && bCondition3)</pre>
<p>sempre és igual a:</p>
<pre class="brush: js">bCondition1 || bCondition2 && bCondition3</pre>
<h4 id="Desfer-se_d'un_OR_aniuat">Desfer-se d'un OR aniuat</h4>
<p>L'operació següent que inclou Booleans:</p>
<pre class="brush: js">bCondition1 && (bCondition2 || bCondition3)</pre>
<p>sempre és igual a:</p>
<pre class="brush: js">!(!bCondition1 || !bCondition2 && !bCondition3)</pre>
<h2 id="Especificacions">Especificacions</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificació</th>
<th scope="col">Estat</th>
<th scope="col">Comentari</th>
</tr>
<tr>
<td>1a edició de ECMAScript.</td>
<td>Estàndard</td>
<td>Definició inicial.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-11.4.9', 'Logical NOT Operator')}}<br>
{{SpecName('ES5.1', '#sec-11.11', 'Binary Logical Operators')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-logical-not-operator', 'Logical NOT operator')}}<br>
{{SpecName('ES6', '#sec-binary-logical-operators', 'Binary Logical Operators')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td><a href="#Logical_AND">AND lògic (<code>&&</code>)</a></td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
</tr>
<tr>
<td><a href="#Logical_OR">OR </a><a href="#Logical_AND">lògic </a><a href="#Logical_OR">(<code>||</code>)</a></td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
</tr>
<tr>
<td><a href="#Logical_NOT">NOT </a><a href="#Logical_AND">lògic</a><a href="#Logical_NOT"> (<code>!</code>)</a></td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Android</th>
<th>Chrome per Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td><a href="#Logical_AND">AND </a><a href="#Logical_AND">lògic</a><a href="#Logical_AND"> (<code>&&</code>)</a></td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
</tr>
<tr>
<td><a href="#Logical_OR">OR </a><a href="#Logical_AND">lògic </a><a href="#Logical_OR">(<code>||</code>)</a></td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
</tr>
<tr>
<td><a href="#Logical_NOT">NOT </a><a href="#Logical_AND">lògic</a><a href="#Logical_NOT"> (<code>!</code>)</a></td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
<td>{{ CompatVersionUnknown() }}</td>
</tr>
</tbody>
</table>
</div>
<h3 id="JavaScript_1.0_and_1.1" name="JavaScript_1.0_and_1.1">Compatibilitat amb versions anteriors: Comportament a JavaScript 1.0 i 1.1</h3>
<p>Els operadors && i <code>||</code> es comporten de la forma següent:</p>
<table class="fullwidth-table">
<tbody>
<tr>
<th>Operador</th>
<th>Ús</th>
<th>Comportament</th>
</tr>
<tr>
<td><code>&&</code></td>
<td><code><em>expr1</em> && <em>expr2</em></code></td>
<td>Si el primer operand (<code>expr1</code>) es pot converitr a false, l'operador <code>&&</code> retorna false en comptes del valor de <code>expr1</code>.</td>
</tr>
<tr>
<td><code>||</code></td>
<td><code><em>expr1</em> || <em>expr2</em></code></td>
<td>Si el primer operand (<code>expr1</code>) es pot converitr a true, l'operador <code>||</code> retorna true en comptes del valor de <code>expr1</code>.</td>
</tr>
</tbody>
</table>
<h2 id="See_also" name="See_also">Vegeu també</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators">Bitwise operators</a></li>
</ul>
|