diff options
Diffstat (limited to 'files/fr/web/javascript/reference/operators/logical_and_assignment')
-rw-r--r-- | files/fr/web/javascript/reference/operators/logical_and_assignment/index.html | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/files/fr/web/javascript/reference/operators/logical_and_assignment/index.html b/files/fr/web/javascript/reference/operators/logical_and_assignment/index.html new file mode 100644 index 0000000000..e788d794d6 --- /dev/null +++ b/files/fr/web/javascript/reference/operators/logical_and_assignment/index.html @@ -0,0 +1,78 @@ +--- +title: Affectation après ET logique (&&=) +slug: Web/JavaScript/Reference/Operators/Logical_AND_assignment +tags: + - JavaScript + - Language feature + - Logical assignment + - Operator + - Reference +browser-compat: javascript.operators.logical_and_assignment +translation-of: Web/JavaScript/Reference/Operators/Logical_AND_assignment +--- +<div>{{jsSidebar("Operators")}}</div> + +<p>L'opérateur d'affectation après ET logique (<code>x &&= y</code>) n'affecte la valeur de l'opérande droit uniquement si l'opérande gauche est <a href="/fr/docs/Glossary/Truthy">équivalent à vrai (<i>truthy</i>)</a>.</p> + +<div>{{EmbedInteractiveExample("pages/js/expressions-logical-and-assignment.html")}}</div> + +<h2 id="syntax">Syntaxe</h2> + +<pre class="brush: js"> +<var>expr1</var> &&= <var>expr2</var> +</pre> + +<h2 id="description">Description</h2> + +<h3 id="short-circuit_evaluation">Évaluation en court-circuit</h3> + +<p>L'opérateur <a href="/fr/docs/Web/JavaScript/Reference/Operators/Logical_AND">ET logique</a> est évalué de gauche à droite et le moteur vérifie s'il peut utiliser un court-circuit avec la régle suivante :</p> + +<p><code>(une expression équivalente à faux) && expr</code> sera court-circuitée pour fournir directement l'expression équivalente à faux.</p> + +<p>Ce « court-circuit » indique que <code><var>expr</var></code> <strong>n'est pas évaluée</strong>. Tout effet de bord lié à cette évaluation n'aura pas lieu (par exemple si <code><var>expr</var></code> est un appel de fonction, la fonction n'est pas exécutée).</p> + +<p>L'opérateur d'affectation après ET logique utilise également ce court-circuit et <code>x &&= y</code> est donc équivalent à :</p> + +<pre class="brush: js"> +x && (x = y); +</pre> + +<p>En revanche, <strong>il n'est pas équivalent</strong> à ce qui suit, et qui effectue quoi qu'il arrive une affectation :</p> + +<pre class="brush: js example-bad"> +x = x && y; +</pre> + +<h2 id="examples">Exemples</h2> + +<h3 id="using_logical_and_assignment">Utiliser l'affectation après ET logique</h3> + +<pre class="brush: js"> +let x = 0; +let y = 1; + +x &&= 0; // 0 +x &&= 1; // 0 +y &&= 1; // 1 +y &&= 0; // 0 +</pre> + +<h2 id="specifications">Spécifications</h2> + +<p>{{Specifications}}</p> + +<h2 id="browser_compatibility">Compatibilité des navigateurs</h2> + +<p>{{Compat}}</p> + +<h2 id="see_also">Voir aussi</h2> + +<ul> + <li><a href="/fr/docs/Web/JavaScript/Reference/Operators/Logical_AND">L'opérateur ET logique (&&)</a></li> + <li><a href="/fr/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator">L'opérateur de coalescence des nuls (<code>??</code>)</a></li> + <li><a href="/fr/docs/Web/JavaScript/Reference/Operators/Bitwise_AND_assignment">L'opérateur d'affectation après ET binaire (<code>&=</code>)</a></li> + <li><a href="/fr/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Le type <code>Boolean</code></a> + <li><a href="/fr/docs/Glossary/Truthy"><i>Truthy</i></a></li> + <li><a href="/fr/docs/Glossary/Falsy"><i>Falsy</i></a></li> +</ul> |