{"id":28714,"date":"2022-04-25T17:17:08","date_gmt":"2022-04-25T11:47:08","guid":{"rendered":"https:\/\/tutorial.eyehunts.com\/?p=28714"},"modified":"2023-07-13T17:51:47","modified_gmt":"2023-07-13T12:21:47","slug":"javascript-in-operator-array-example-code","status":"publish","type":"post","link":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/","title":{"rendered":"JavaScript in operator array | Example code"},"content":{"rendered":"\n<p>JavaScript<strong> in operator<\/strong> used in an array to check specified index or its prototype chain. The <code>in<\/code> operator will return <code>false<\/code> for empty array slots. Even if accessing it directly returns <code>undefined<\/code>.<\/p>\n\n\n\n<p>For example, you can use the <code>in<\/code> operator to check if a property exists in an object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const person = {\r\n  name: 'John',\r\n  age: 30,\r\n};\r\n\r\nconsole.log('name' in person); \/\/ true\r\nconsole.log('address' in person); \/\/ false\r<\/code><\/pre>\n\n\n\n<p>However, if you want to check if an element exists in an array, you can use the <code>Array.prototype.includes()<\/code> method introduced in ECMAScript 2016 (ES7) or the <code>Array.prototype.indexOf()<\/code> method.<\/p>\n\n\n\n<p>Here&#8217;s an example using the <code>includes()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const fruits = &#91;'apple', 'banana', 'orange'];\r\n\r\nconsole.log(fruits.includes('banana')); \/\/ true\r\nconsole.log(fruits.includes('grape')); \/\/ false\r<\/code><\/pre>\n\n\n\n<p>And here&#8217;s an example using the <code>indexOf()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const fruits = &#91;'apple', 'banana', 'orange'];\r\n\r\nconsole.log(fruits.indexOf('banana') !== -1); \/\/ true\r\nconsole.log(fruits.indexOf('grape') !== -1); \/\/ false\r<\/code><\/pre>\n\n\n\n<p>These methods return a boolean value indicating whether the element is found in the array or not.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JavaScript in operator array<\/strong><\/h2>\n\n\n\n<p>A simple example code shows some uses of the <code>in<\/code> operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;body&gt;\n\n  &lt;script&gt;\n   let trees = &#91;'redwood', 'bay', 'cedar', 'oak', 'maple']\n   \n   console.log(0 in trees)        \n   console.log(3 in trees)       \n   console.log(6 in trees)     \n   console.log('bay' in trees) \/\/returns false (you must specify the index number, not the value at that index)\n\n &lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt; \n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"272\" height=\"115\" src=\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg?resize=272%2C115&#038;ssl=1\" alt=\"JavaScript in operator array\" class=\"wp-image-28739\"\/><\/figure><\/div>\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why does javascript&#8217;s &#8220;in&#8221; operator return true when testing if 0 exists in an array that doesn&#8217;t contain 0?<\/strong><\/h3>\n\n\n\n<p><strong>Answer<\/strong>: Javascript&#8217;s <code>in<\/code> operator does not check if a value is contained in an array.<\/p>\n\n\n\n<p>The <code>in<\/code> operator doesn&#8217;t do what you think it does. The <code>in<\/code> operator returns <code>true<\/code> if the specified operand is a <a href=\"https:\/\/tutorial.eyehunts.com\/js\/javascript-check-if-object-has-property-example-code\/\">property of the object<\/a>. For arrays, it returns <code>true<\/code> if the operand is a valid <em>index<\/em> (which makes sense if think of arrays as a special-case object where the properties are simply named 0, 1, 2, &#8230;)<\/p>\n\n\n\n<p>For example, try this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x=&#91;1,4,6];\nalert(2 in x);\n<\/code><\/pre>\n\n\n\n<p>It&#8217;ll also return <code>true<\/code>, because &#8220;2&#8221; is a valid index in the array. In the same way, &#8220;0&#8221; is an index in the array, so also returns <code>true<\/code>.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Do comment if you have any doubts or suggestions on this JS &#8220;in&#8221; operator topic.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note:<\/strong>&nbsp;The<strong>&nbsp;All JS Examples codes&nbsp;<\/strong>are&nbsp;tested on the Firefox browser and the Chrome browser.<\/p><p>OS:&nbsp;<strong>Windows 10<\/strong><\/p><p>Code: HTML 5 Version<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript in operator used in an array to check specified index or its prototype chain. The in operator will return false for empty array slots. Even if accessing it directly returns undefined. For example, you can use the in operator to check if a property exists in an object: However, if you want to check&hellip;&nbsp;<a href=\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">JavaScript in operator array | Example code<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[69],"tags":[106,120],"post_series":[],"class_list":["post-28714","post","type-post","status-publish","format-standard","hentry","category-js","tag-js-array","tag-js-operator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript in operator array | Example code<\/title>\n<meta name=\"description\" content=\"JavaScript in operator used in an array to check specified index or its prototype chain. The in operator will return false for empty array...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript in operator array | Example code\" \/>\n<meta property=\"og:description\" content=\"JavaScript in operator used in an array to check specified index or its prototype chain. The in operator will return false for empty array...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-25T11:47:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-13T12:21:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg\" \/>\n<meta name=\"author\" content=\"Rohit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/\",\"url\":\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/\",\"name\":\"JavaScript in operator array | Example code\",\"isPartOf\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg\",\"datePublished\":\"2022-04-25T11:47:08+00:00\",\"dateModified\":\"2023-07-13T12:21:47+00:00\",\"author\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1\"},\"description\":\"JavaScript in operator used in an array to check specified index or its prototype chain. The in operator will return false for empty array...\",\"breadcrumb\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg?fit=272%2C115&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg?fit=272%2C115&ssl=1\",\"width\":272,\"height\":115},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tutorial.eyehunts.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript in operator array | Example code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/#website\",\"url\":\"https:\/\/tutorial.eyehunts.com\/\",\"name\":\"Tutorial\",\"description\":\"By EyeHunts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tutorial.eyehunts.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1\",\"name\":\"Rohit\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971\",\"contentUrl\":\"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971\",\"caption\":\"Rohit\"},\"description\":\"Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology &amp; like learning technical.\",\"url\":\"https:\/\/tutorial.eyehunts.com\/author\/rohit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript in operator array | Example code","description":"JavaScript in operator used in an array to check specified index or its prototype chain. The in operator will return false for empty array...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript in operator array | Example code","og_description":"JavaScript in operator used in an array to check specified index or its prototype chain. The in operator will return false for empty array...","og_url":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/","og_site_name":"Tutorial","article_published_time":"2022-04-25T11:47:08+00:00","article_modified_time":"2023-07-13T12:21:47+00:00","og_image":[{"url":"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg","type":"","width":"","height":""}],"author":"Rohit","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rohit","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/","url":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/","name":"JavaScript in operator array | Example code","isPartOf":{"@id":"https:\/\/tutorial.eyehunts.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#primaryimage"},"image":{"@id":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#primaryimage"},"thumbnailUrl":"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg","datePublished":"2022-04-25T11:47:08+00:00","dateModified":"2023-07-13T12:21:47+00:00","author":{"@id":"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1"},"description":"JavaScript in operator used in an array to check specified index or its prototype chain. The in operator will return false for empty array...","breadcrumb":{"@id":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#primaryimage","url":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg?fit=272%2C115&ssl=1","contentUrl":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator-array.jpg?fit=272%2C115&ssl=1","width":272,"height":115},{"@type":"BreadcrumbList","@id":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-array-example-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tutorial.eyehunts.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript in operator array | Example code"}]},{"@type":"WebSite","@id":"https:\/\/tutorial.eyehunts.com\/#website","url":"https:\/\/tutorial.eyehunts.com\/","name":"Tutorial","description":"By EyeHunts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tutorial.eyehunts.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1","name":"Rohit","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/image\/","url":"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971","contentUrl":"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971","caption":"Rohit"},"description":"Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology &amp; like learning technical.","url":"https:\/\/tutorial.eyehunts.com\/author\/rohit\/"}]}},"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":28705,"url":"https:\/\/tutorial.eyehunts.com\/js\/javascript-hasownproperty-vs-in\/","url_meta":{"origin":28714,"position":0},"title":"JavaScript hasOwnProperty vs in","author":"Rohit","date":"June 19, 2023","format":false,"excerpt":"In JavaScript, hasOwnProperty and the in operator are both used to check if an object has a specific property. However, they have some differences in behavior and usage. hasOwnProperty method: The hasOwnProperty method is a built-in method of the Object prototype. It checks if an object has a property with\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/tutorial.eyehunts.com\/category\/js\/"},"img":{"alt_text":"JavaScript hasOwnProperty vs in","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2023\/06\/JavaScript-hasOwnProperty-vs-in.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28603,"url":"https:\/\/tutorial.eyehunts.com\/js\/javascript-checks-if-the-object-has-key-example-code\/","url_meta":{"origin":28714,"position":1},"title":"JavaScript checks if the object has key | Example code","author":"Rohit","date":"May 5, 2022","format":false,"excerpt":"Use JavaScript in the operator to check if the object has the key. Use myObj.hasOwnProperty('key') to check an object's own keys and will only return true if key is available on myObj directly: if ('key' in myObj) And the inverse. if (!('key' in myObj)) Note: The in operator matches all\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/tutorial.eyehunts.com\/category\/js\/"},"img":{"alt_text":"JavaScript checks if the object has key","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/05\/JavaScript-checks-if-the-object-has-key.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28698,"url":"https:\/\/tutorial.eyehunts.com\/js\/javascript-hasownproperty-method\/","url_meta":{"origin":28714,"position":2},"title":"JavaScript hasOwnProperty | Method","author":"Rohit","date":"April 25, 2022","format":false,"excerpt":"Use JavaScript Object hasOwnProperty() method to check if the object has the given property as its own property. This returns true if the specified property is a direct property of the object \u2014 even if the value is null or undefined. And it returns false if the property is inherited\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/tutorial.eyehunts.com\/category\/js\/"},"img":{"alt_text":"JavaScript hasOwnProperty Method","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-hasOwnProperty-Method.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28848,"url":"https:\/\/tutorial.eyehunts.com\/js\/dot-operator-in-javascript-example-code\/","url_meta":{"origin":28714,"position":3},"title":"Dot operator in JavaScript | Example code","author":"Rohit","date":"April 28, 2022","format":false,"excerpt":"The JavaScript dot (.) operator is simply an operator that sits between its operands, just like + and -. The variables stored in an object we access via the dot operator are called properties. objectName.propertyName objectName.methodName() Here, objectName represents the name of the object, propertyName is the name of the\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/tutorial.eyehunts.com\/category\/js\/"},"img":{"alt_text":"Dot operator in JavaScript","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/Dot-operator-in-JavaScript.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28197,"url":"https:\/\/tutorial.eyehunts.com\/js\/javascript-in-operator-example-code\/","url_meta":{"origin":28714,"position":4},"title":"JavaScript in operator | Example code","author":"Rohit","date":"April 25, 2022","format":false,"excerpt":"Use JavaScript in operator to check if the specified (given) property is in the specified object or its prototype chain. The in operator returns true if the specified property exists. property_name in object Here, property_name is the name of the property you want to check, and object is the object\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/tutorial.eyehunts.com\/category\/js\/"},"img":{"alt_text":"JavaScript in operator","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-in-operator.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":28733,"url":"https:\/\/tutorial.eyehunts.com\/js\/javascript-double-question-mark-vs-double-pipe-code\/","url_meta":{"origin":28714,"position":5},"title":"JavaScript double question mark vs double pipe | Code","author":"Rohit","date":"April 27, 2022","format":false,"excerpt":"The OR operator (double pipe) || uses the right value if the left is falsy, while the nullish coalescing operator ?? (double question mark) uses the right value if the left is null or undefined. These operators are often used to provide a default value if the first one is\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/tutorial.eyehunts.com\/category\/js\/"},"img":{"alt_text":"JavaScript double question mark vs double pipe","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2022\/04\/JavaScript-double-question-mark-vs-double-pipe.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/28714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/comments?post=28714"}],"version-history":[{"count":1,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/28714\/revisions"}],"predecessor-version":[{"id":40993,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/28714\/revisions\/40993"}],"wp:attachment":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/media?parent=28714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/categories?post=28714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/tags?post=28714"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/post_series?post=28714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}