aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/operators/conditional_operator
diff options
context:
space:
mode:
Diffstat (limited to 'files/ca/web/javascript/reference/operators/conditional_operator')
-rw-r--r--files/ca/web/javascript/reference/operators/conditional_operator/index.html171
1 files changed, 171 insertions, 0 deletions
diff --git a/files/ca/web/javascript/reference/operators/conditional_operator/index.html b/files/ca/web/javascript/reference/operators/conditional_operator/index.html
new file mode 100644
index 0000000000..0cfbd791a3
--- /dev/null
+++ b/files/ca/web/javascript/reference/operators/conditional_operator/index.html
@@ -0,0 +1,171 @@
+---
+title: Operador Condicional (ternari)
+slug: Web/JavaScript/Reference/Operators/Conditional_Operator
+translation_of: Web/JavaScript/Reference/Operators/Conditional_Operator
+original_slug: Web/JavaScript/Referencia/Operadors/Conditional_Operator
+---
+<div>
+<div>{{jsSidebar("Operators")}}</div>
+</div>
+
+<h2 id="Summary" name="Summary">Resum</h2>
+
+<p>L'operador <strong>condicional (ternari)</strong> és l'únic operador de JavaScript que opera amb tres operands. Aquest operador és freqüentment usat com una simplificació de la sentència <a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else"><code>if</code></a>.</p>
+
+<h2 id="Sintaxi">Sintaxi</h2>
+
+<pre class="syntaxbox"><em>condition</em> ? <em>expr1</em> : <em>expr2</em> </pre>
+
+<h2 id="Paràmetres">Paràmetres</h2>
+
+<dl>
+ <dt><code>condition</code></dt>
+ <dd>Una expressió que avalua <code>true</code> o <code>false</code>.</dd>
+</dl>
+
+<dl>
+ <dt><code>expr1</code>, <code>expr2</code></dt>
+ <dd>Expressions amb valors de qualsevol tipus.</dd>
+</dl>
+
+<h2 id="Descripció">Descripció</h2>
+
+<p>Si <code>condition</code> és <code>true</code>, l'operador retorna el valor de <code>expr1</code>; de ser el contrari, retorna el valor de <code>expr2</code>. Per exemple, per mostrar diferents missatges basats en el valor de la variable <code>isMember</code>, es podria fer servir aquesta sentència:</p>
+
+<pre class="brush: js">"The fee is " + (isMember ? "$2.00" : "$10.00")
+</pre>
+
+<p>També es pot assignar variables depenent del resultat ternari:</p>
+
+<pre class="brush: js">var elvisLives = Math.PI &gt; 4 ? "Yep" : "Nope";</pre>
+
+<p>És possible realitzar avaluacions ternàries múltiples (nota: L'operador condicional operator s'associa per la dreta):</p>
+
+<pre class="brush: js">var firstCheck = false,
+ secondCheck = false,
+ access = firstCheck ? "Access denied" : secondCheck ? "Access denied" : "Access granted";
+
+console.log( access ); // logs "Access granted"</pre>
+
+<p>També es pot usar avaluacions ternàries en espais lliures per tal de fer diferents operacions:</p>
+
+<pre class="brush: js">var stop = false, age = 16;
+
+age &gt; 18 ? location.assign("continue.html") : stop = true;
+</pre>
+
+<p>També es pot fer més d'una sola operació per cas, separant-les amb una coma:</p>
+
+<pre class="brush: js">var stop = false, age = 23;
+
+age &gt; 18 ? (
+ alert("OK, you can go."),
+ location.assign("continue.html")
+) : (
+ stop = true,
+ alert("Sorry, you are much too young!")
+);
+</pre>
+
+<p>I per últim, també es pot fer més d'una operació durant l'assignació d'un valor. En aquest cas, <strong><em>el últim valor del parèntesis serparat per una coma</em> serà el valor assignat</strong>.</p>
+
+<pre class="brush: js">var age = 16;
+
+var url = age &gt; 18 ? (
+ alert("OK, you can go."),
+ // alert returns "undefined", but it will be ignored because
+ // isn't the last comma-separated value of the parenthesis
+ "continue.html" // the value to be assigned if age &gt; 18
+) : (
+ alert("You are much too young!"),
+ alert("Sorry :-("),
+ // etc. etc.
+ "stop.html" // the value to be assigned if !(age &gt; 18)
+);
+
+location.assign(url); // "stop.html"</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">Comentaris</th>
+ </tr>
+ <tr>
+ <td>1a edició deECMAScript.</td>
+ <td>Estàndard</td>
+ <td>Definició inicial. Implementat en JavaScript 1.0</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-11.12', 'The conditional operator')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-conditional-operator', 'Conditional Operator')}}</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>Suport bàsic</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>Suport bàsic</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="See_also" name="See_also">Vegeu també</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if statement</a></li>
+</ul>