--- title: Logical AND assignment (&&=) slug: Web/JavaScript/Reference/Operators/Logical_AND_assignment translation_of: Web/JavaScript/Reference/Operators/Logical_AND_assignment --- <div>{{jsSidebar("Operators")}}</div> <p>The logical AND assignment (<code>x &&= y</code>) operator only assigns if <code>x</code> is {{Glossary("truthy")}}.</p> <div>{{EmbedInteractiveExample("pages/js/expressions-logical-and-assignment.html")}}</div> <h2 id="语法">语法</h2> <pre class="syntaxbox"><em>expr1</em> &&= <em>expr2</em> </pre> <h2 id="描述">描述</h2> <h3 id="Short-circuit_evaluation">Short-circuit evaluation</h3> <p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND">logical AND</a> operator is evaluated left to right, it is tested for possible short-circuit evaluation using the following rule:</p> <p><code>(some falsy expression) && expr</code> is short-circuit evaluated to the falsy expression;</p> <p>Short circuit means that the <code><em>expr</em></code> part above is <strong>not evaluated</strong>, hence any side effects of doing so do not take effect (e.g., if <code><em>expr</em></code> is a function call, the calling never takes place).</p> <p>Logical AND assignment short-circuits as well meaning that <code>x &&= y</code> is equivalent to:</p> <pre class="brush: js">x && (x = y);</pre> <p>And not equivalent to the following which would always perform an assignment:</p> <pre class="brush: js example-bad">x = x && y; </pre> <h2 id="例子">例子</h2> <h3 id="Using_logical_AND_assignment">Using logical AND assignment</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="规范">规范</h2> <table class="standard-table"> <thead> <tr> <th scope="col">Specification</th> </tr> </thead> <tbody> <tr> </tr> <tr> <td>{{SpecName('Logical Assignment', '#sec-assignment-operators', 'Assignment operators')}}</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("javascript.operators.logical_and_assignment")}}</p> <h2 id="参见">参见</h2> <ul> <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND">Logical AND (&&)</a></li> <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator">The nullish coalescing operator (<code>??</code>)</a></li> <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND_assignment">Bitwise AND assignment (<code>&=</code>)</a></li> <li>{{jsxref("Boolean")}}</li> <li>{{Glossary("Truthy")}}</li> <li>{{Glossary("Falsy")}}</li> </ul>