{"id":10970,"date":"2023-11-21T20:25:00","date_gmt":"2023-11-21T19:25:00","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=10970"},"modified":"2023-11-21T11:32:06","modified_gmt":"2023-11-21T10:32:06","slug":"javascript-can-a-1-a-2-a3-ever-evaluate-to-true-stack-overflow","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2023\/11\/21\/javascript-can-a-1-a-2-a3-ever-evaluate-to-true-stack-overflow\/","title":{"rendered":"javascript &#8211; Can (a== 1 &#038;&#038; a ==2 &#038;&#038; a==3) ever evaluate to true? &#8211; Stack Overflow"},"content":{"rendered":"\n<p><em><a href=\"https:\/\/stackoverflow.com\/questions\/48270127\/can-a-1-a-2-a-3-ever-evaluate-to-true\">javascript &#8211; Can (a== 1 &amp;&amp; a ==2 &amp;&amp; a==3) ever evaluate to true? &#8211; Stack Overflow<\/a><\/em><\/p>\n\n\n\n<p>Yes, it can. IMHO it is one of the several undesirable consequences of loosely typed languages. In fact, according to an almost <a href=\"https:\/\/stackoverflow.com\/users\/4020527\/user4020527\">anonymous user<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>If you take advantage of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Equality_comparisons_and_sameness#Loose_equality_using\">how <code class=\"\" data-line=\"\">==<\/code> works<\/a>, you could simply create an object with a custom <code class=\"\" data-line=\"\">toString<\/code> (or <code class=\"\" data-line=\"\">valueOf<\/code>) function that changes what it returns each time it is used such that it satisfies all three conditions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">const a = {\n  i: 1,\n  toString: function () {\n    return a.i++;\n  }\n}\n\nif(a == 1 &amp;&amp; a == 2 &amp;&amp; a == 3) {\n  console.log(&#039;Hello World!&#039;);\n}<\/code><\/pre>\n\n\n\n<p>The reason this works is due to the use of the loose equality operator. When using loose equality, if one of the operands is of a different type than the other, the engine will attempt to convert one to the other. In the case of an object on the left and a number on the right, it will attempt to convert the object to a number by first calling <code class=\"\" data-line=\"\">valueOf<\/code> if it is callable, and failing that, it will call <code class=\"\" data-line=\"\">toString<\/code>. I used <code class=\"\" data-line=\"\">toString<\/code> in this case simply because it&#8217;s what came to mind, <code class=\"\" data-line=\"\">valueOf<\/code> would make more sense. If I instead returned a string from <code class=\"\" data-line=\"\">toString<\/code>, the engine would have then attempted to convert the string to a number giving us the same end result, though with a slightly longer path.<\/p>\n<\/blockquote>\n\n\n\n<p>Worst, it is also possible using the === operator!<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">var i = 0;\n\nwith({\n  get a() {\n    return ++i;\n  }\n}) {\n  if (a == 1 &amp;&amp; a == 2 &amp;&amp; a == 3)\n    console.log(&quot;wohoo&quot;);\n}<\/code><\/pre>\n\n\n\n<p>This uses a getter inside of a <code class=\"\" data-line=\"\">with<\/code> statement to let <code class=\"\" data-line=\"\">a<\/code> evaluate to three different values.<\/p>\n\n\n\n<p>&#8230; this still does not mean this should be used in real code&#8230;<\/p>\n\n\n\n<p>Even worse, this trick will also work with the use of <code class=\"\" data-line=\"\">===<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">  var i = 0;\n\n  with({\n    get a() {\n      return ++i;\n    }\n  }) {\n    if (a !== a)\n      console.log(&quot;yep, this is printed.&quot;);\n  }<\/code><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">javascript &#8211; Can (a== 1 &amp;&amp; a ==2 &amp;&amp; a==3) ever evaluate to true? &#8211; Stack Overflow Yes, it can. IMHO it is one of the several undesirable consequences of loosely typed languages. In fact, according to an almost anonymous user: Worst, it is also possible using the === operator!<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2023\/11\/21\/javascript-can-a-1-a-2-a3-ever-evaluate-to-true-stack-overflow\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"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,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[72,50,278],"tags":[],"class_list":["post-10970","post","type-post","status-publish","format-standard","hentry","category-documentations","category-javascript","category-tricks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-2QW","jetpack-related-posts":[{"id":9317,"url":"https:\/\/monodes.com\/predaelli\/2022\/04\/28\/7-more-killer-one-liners-in-javascript-by-tapajyoti-bose-apr-2022-medium\/","url_meta":{"origin":10970,"position":0},"title":"7 More Killer One-Liners in JavaScript | by Tapajyoti Bose | Apr, 2022 | Medium","author":"Paolo Redaelli","date":"2022-04-28","format":false,"excerpt":"7 More Killer One-Liners in JavaScript | by Tapajyoti Bose | Apr, 2022 | Medium This is a continuation of the previous list of JavaScript one-liners. If you haven\u2019t checked out the article, you are highly encouraged to accomplish so. Let\u2019s crack on with the new list! 1. Sleep Function\u2026","rel":"","context":"In &quot;Javascript&quot;","block_context":{"text":"Javascript","link":"https:\/\/monodes.com\/predaelli\/category\/javascript\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":924,"url":"https:\/\/monodes.com\/predaelli\/2016\/01\/26\/json-references\/","url_meta":{"origin":10970,"position":1},"title":"JSON references","author":"Paolo Redaelli","date":"2016-01-26","format":false,"excerpt":"Is there a standard way of referencing objects by identity in JSON? For example, so that graphs and other data structures with lots of (possibly circular) references can be sanely serialized\/loaded? From: JSON: Standard way of referencing an object by identity (for, eg, circular references)? - Stack Overflow Then there's\u2026","rel":"","context":"In &quot;Javascript&quot;","block_context":{"text":"Javascript","link":"https:\/\/monodes.com\/predaelli\/category\/javascript\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3971,"url":"https:\/\/monodes.com\/predaelli\/2018\/04\/01\/sheer-pure-evil\/","url_meta":{"origin":10970,"position":2},"title":"Sheer, pure evil","author":"Paolo Redaelli","date":"2018-04-01","format":false,"excerpt":"JavaScript: Can (a==1 && a==2 && a==3) ever evaluate to true?\u00a0Yes, it can. Understand how in this article! That's pure sheer evil, in my humble opinion as it tricks most people. Eiffel forbids this evilness, but I shall explain it better. Recently, an interesting code snippet has been making the\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2529,"url":"https:\/\/monodes.com\/predaelli\/2017\/05\/27\/stack-overflow-helping-one-million-developers-exit-vim-stack-overflow-blog\/","url_meta":{"origin":10970,"position":3},"title":"Stack Overflow: Helping One Million Developers Exit Vim &#8211; Stack Overflow Blog","author":"Paolo Redaelli","date":"2017-05-27","format":false,"excerpt":"This morning, a popular Stack Overflow question hit a major milestone: Sorgente: Stack Overflow: Helping One Million Developers Exit Vim - Stack Overflow Blog","rel":"","context":"In &quot;Fun&quot;","block_context":{"text":"Fun","link":"https:\/\/monodes.com\/predaelli\/category\/fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2017\/05\/exitvim-1024x455.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2017\/05\/exitvim-1024x455.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2017\/05\/exitvim-1024x455.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2017\/05\/exitvim-1024x455.png?resize=700%2C400 2x"},"classes":[]},{"id":5130,"url":"https:\/\/monodes.com\/predaelli\/2019\/01\/15\/intuitive-asynchronous-javascript-edwin-yung-medium\/","url_meta":{"origin":10970,"position":4},"title":"Intuitive Asynchronous Javascript \u2013 Edwin Yung \u2013 Medium","author":"Paolo Redaelli","date":"2019-01-15","format":false,"excerpt":"Escape callback hell! Source: Intuitive Asynchronous Javascript \u2013 Edwin Yung \u2013 Medium \u00a0 Asynchronous Javascript Explained Simply and Intuitively Edwin Yung Dec 29, 2017 Escape callback hell with this\u00a0article! Asynchronous Javascript is difficult to wrangle. The following is my quick attempt to elucidate their inner workings. Before we get started,\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1614,"url":"https:\/\/monodes.com\/predaelli\/2016\/06\/01\/javascript-factory-functions-vs-constructor-functions-vs-classes-javascript-scene-medium\/","url_meta":{"origin":10970,"position":5},"title":"JavaScript Factory Functions vs Constructor Functions vs Classes \u2014 JavaScript Scene \u2014 Medium","author":"Paolo Redaelli","date":"2016-06-01","format":"link","excerpt":"https:\/\/medium.com\/javascript-scene\/javascript-factory-functions-vs-constructor-functions-vs-classes-2f22ceddf33e#.13avjf4lk","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/10970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=10970"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/10970\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=10970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=10970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=10970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}