{"id":3189,"date":"2024-04-16T11:27:07","date_gmt":"2024-04-16T09:27:07","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3189"},"modified":"2024-04-16T11:27:07","modified_gmt":"2024-04-16T09:27:07","slug":"exploring-the-difference-between-and-in-javascript","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2024\/04\/16\/exploring-the-difference-between-and-in-javascript\/","title":{"rendered":"Exploring the Difference Between &#8216;||&#8217; and &#8216;??&#8217; in JavaScript"},"content":{"rendered":"\n<p>In JavaScript, the<strong><em> logical OR operator<\/em><\/strong> (||) and the <strong><em>nullish coalescing operato<\/em><\/strong>r (??) are two distinct operators that serve different purposes when it comes to handling values. Understanding the differences between these operators is crucial for writing robust and error-free JavaScript code. In this article, we will delve into the nuances of<strong> ||<\/strong> and <strong>??<\/strong> <strong><em>operators<\/em><\/strong>, providing detailed examples to illustrate their behaviors and use cases.<\/p>\n\n\n\n<h4>The Logical OR Operator (||)<\/h4>\n\n\n\n<p>The logical OR operator (||) in JavaScript is commonly used for providing a default value when dealing with falsy values such as null, undefined, 0, false, and empty strings.<\/p>\n\n\n\n<p><strong>Example:<\/strong> (<em>*Javascript<\/em>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Using the logical OR operator to provide a default value\r\nconst defaultValue = someValue || 'default';<\/code><\/pre>\n\n\n\n<p>In the above example, if <em>someValue<\/em> is falsy, the expression will evaluate to &#8216;default&#8217;. However, if <em>someValue<\/em> is truthy, the expression will evaluate to <em>someValue<\/em>.<\/p>\n\n\n\n<p>JavaScript considers a value that is not among the following truthy:<\/p>\n\n\n\n<ul><li><code>false<\/code><\/li><li><code>0<\/code><\/li><li><code>''<\/code>&nbsp;(empty string)<\/li><li><code>null<\/code><\/li><li><code>undefined<\/code><\/li><li><code>NaN<\/code><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>let name = \"\";\r\nlet defaultName = \"Chloe\";\r\nlet displayName = name || defaultName; \/\/ Chloe<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>The Nullish Coalescing Operator (??)<\/h4>\n\n\n\n<p>The nullish coalescing operator (<strong>??<\/strong>) was introduced in ECMAScript 2020 as a more specific alternative to the logical OR operator. <em><strong>It specifically checks for <span class=\"has-inline-color has-vivid-red-color\">null<\/span> or <span class=\"has-inline-color has-vivid-red-color\">undefined<\/span> values and provides a default value only if the operand on the left-hand side is <span class=\"has-inline-color has-vivid-red-color\">null<\/span> or <span class=\"has-inline-color has-vivid-red-color\">undefined<\/span>.<\/strong><\/em><\/p>\n\n\n\n<p><strong>Example:<\/strong> (<em>*Javascript<\/em>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Using the nullish coalescing operator to provide a default value\r\nconst defaultValue = someValue ?? 'default';<\/code><\/pre>\n\n\n\n<p>In this example, if <em>someValue<\/em> is null or undefined, the expression will evaluate to &#8216;default&#8217;. If<em> someValue<\/em> is any other falsy value, the expression will evaluate to <em>someValue<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let age = 0;\r\nlet defaultAge = 18;\r\nlet userAge = age ?? defaultAge; \/\/ 0<\/code><\/pre>\n\n\n\n<p>In this example, even though age is\u00a0<code>0<\/code>\u00a0(which is considered falsy), the\u00a0<code>??<\/code>\u00a0operator does not consider it as a trigger for the fallback because\u00a0<code>0<\/code>\u00a0is not\u00a0<code>null<\/code>\u00a0or\u00a0<code>undefined<\/code>. Therefore,\u00a0<code>userAge<\/code>\u00a0is assigned the value of age, which is\u00a0<code>0<\/code>.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Key Differences and Use Cases<\/h4>\n\n\n\n<ol><li><strong>Treatment of Falsy Values:<\/strong><\/li><\/ol>\n\n\n\n<ul><li>The logical OR operator ( || ) considers all falsy values (such as 0, false, and empty strings) as candidates for providing a default value.<\/li><li>The nullish coalescing operator ( ?? ) only considers null and undefined as candidates for providing a default value.<\/li><\/ul>\n\n\n\n<p><br><br>2. <strong>Use Cases:<\/strong><\/p>\n\n\n\n<ul><li>The logical OR operator ( || ) is commonly used for providing a default value in a broader sense, including cases where 0, false, or empty strings should trigger the default value.<\/li><li>The nullish coalescing operator ( ?? ) is specifically designed for scenarios where null or undefined should trigger the default value, while other falsy values should not.<\/li><\/ul>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, the logical OR operator (||) and the nullish coalescing operator (??) are two distinct operators that serve different purposes when it comes to handling values. Understanding the differences between these operators is crucial for writing robust and error-free JavaScript code. In this article, we will delve into the nuances of || and ?? [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":258,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[831,832,834,830,463,833],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3189"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3189"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3189\/revisions"}],"predecessor-version":[{"id":3190,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3189\/revisions\/3190"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/258"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}