ed && <span class="hljs-literal">false</span>) |<span class="hljs-params"></span>| <span class="hljs-literal">true</span>) <span class="hljs-string">??</span> <span class="hljs-string">"default value"</span>; <span class="hljs-regexp">//</span> <span class="hljs-literal">true</span></pre></div><h1 id="5d21">結論</h1><p id="19ae">過往在使用 Logical OR 和 Logical AND 實作 Short Circuit 機制時,其實已經相當方便,但在一些場景下,非常容易出錯,或是讓程式碼在判斷情境時更為麻煩。空值合併運算符(Nullish Coalescing Operator)將 Logical OR 在判定運算元(Operand )的條件限縮,讓我們在實作 Short Circuit 機制上更為方便。</p><p id="fb42">然而,使用上搭配 Logical OR 或 Logical AND 時,必須考量到運算子優先序的問題,因此必須透過括號明確去表明哪個運算子的位階較高,指名要先被執行——這其實也能提升程式碼的可讀性喔!</p><p id="1f28" type="7">最後,如果你喜歡這篇的分享,請不吝嗇的多點幾個 👏 給我超多鼓勵,感謝 :)</p><h1 id="51c4">參考資源</h1><div id="24a0" class="link-block">
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing">
<div>
<div>
<h2>Nullish coalescing operator (??) - JavaScript | MDN</h2>
<div><h3>The nullish coalescing operator can be seen as a special case of the logical OR (||) operator. The latter returns the…</h3></div>
<div><p>developer.mozilla.org</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*7fH0v8Vin0rWLA50)"></div>
</div>
</div>
</a>
</div><div id="11f0" class="link-block">
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence">
<div>
<div>
<h2>Operator precedence - JavaScript | MDN</h2>
<div><h3>Consider an expression describable by the representation below, where both OP1 and OP2 are fill-in-the-blanks for…</h3></div>
<div><p>developer.mozilla.org</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*D3QPCl8ZQbu05KPs)"></div>
</div>
</div>
</a>
</div><div id="66e6" class="link-block">
<a href="https://javascript.info/nullish-coalescing-operator">
<div>
<div>
<h2>Nullish coalescing operator '??'</h2>
<div><h3>The nullish coalescing operator is written as two question marks ??. As it treats null and undefined similarly, we'll…</h3></div>
<div><p>javascript.info</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*S_2SHmetpYP2nqmz)"></div>
</div>
</div>
</a>
</div><div id="fcbc" class="link-block">
<a href="https://www.educative.io/answers/what-is-the-nullish-coalescing-operator-in-javascript">
<div>
<div>
<h2>What is the nullish coalescing operator (??) in JavaScript?</h2>
<div><h3>The nullish coalescing operator (??) returns the right-side value when its left-side value is null or undefined…</h3></div>
<div><p>www.educative.io</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*NDB3B2TCIXQizRPB)"></div>
</div>
</div>
</a>
</div><div id="5542" class="link-block">
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR">
<div>
<div>
<h2>Logical OR (||) - JavaScript | MDN</h2>
<div><h3>The logical OR (||) (logical disjunction) operator for a set of operands is true if and only if one or more of its…</h3></div>
<div><p>developer.mozilla.org</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*FQAaEyqmh0X49Tss)"></div>
</div>
</div>
</a>
</div></article></body>
邏輯運算子中 Logical AND (&&)的位階則高於 Logical OR(||),因此當一個運算式中(如下範例)同時運用了 Logical AND 和 LogicalOR,前者將優先被執行。然而,如果希望範例中的 Ligical OR 先被執行,則可以使用位階更高的 GroupingOperator() 來達成效果。
過往在使用 Logical OR 和 Logical AND 實作 Short Circuit 機制時,其實已經相當方便,但在一些場景下,非常容易出錯,或是讓程式碼在判斷情境時更為麻煩。空值合併運算符(Nullish Coalescing Operator)將 Logical OR 在判定運算元(Operand )的條件限縮,讓我們在實作 Short Circuit 機制上更為方便。
然而,使用上搭配 Logical OR 或 Logical AND 時,必須考量到運算子優先序的問題,因此必須透過括號明確去表明哪個運算子的位階較高,指名要先被執行——這其實也能提升程式碼的可讀性喔!