<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniel Rochetti</title>
    <description>The latest articles on DEV Community by Daniel Rochetti (@drochetti).</description>
    <link>https://dev.to/drochetti</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F244992%2F00e2167c-1d03-46c1-a04c-2ef821869587.jpg</url>
      <title>DEV Community: Daniel Rochetti</title>
      <link>https://dev.to/drochetti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/drochetti"/>
    <language>en</language>
    <item>
      <title>JavaScript is bananas</title>
      <dc:creator>Daniel Rochetti</dc:creator>
      <pubDate>Sun, 28 Feb 2021 12:51:49 +0000</pubDate>
      <link>https://dev.to/drochetti/javascript-is-bananas-5hn</link>
      <guid>https://dev.to/drochetti/javascript-is-bananas-5hn</guid>
      <description>&lt;p&gt;During one of these dev Twitter &lt;em&gt;"Offend a JavaScript developer in one line of code."&lt;/em&gt; jokes, &lt;a href="https://twitter.com/drochetti/status/1365418508599795713"&gt;I replied with&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a good old joke about how JavaScript dynamic and &lt;em&gt;forgiving&lt;/em&gt; type system handles some operations.&lt;/p&gt;

&lt;p&gt;It's worth it calling out that I didn't come up with it. I don't even remember the first time I saw it, but it was interesting to notice the curiosity and confusion it created.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's happening there?
&lt;/h3&gt;

&lt;p&gt;Let's take a deeper look at what happens in each step of that line:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;'b'&lt;/code&gt; is concatenated with &lt;code&gt;'a'&lt;/code&gt;, resulting in &lt;code&gt;'ba' + + 'a' + 'a'&lt;/code&gt;. So far so good.&lt;/li&gt;
&lt;li&gt;Now things get interesting. The next expression is &lt;code&gt;+ + 'a'&lt;/code&gt;. The first &lt;code&gt;+&lt;/code&gt; is a concatenation operation just like the previous one, but then it finds another &lt;code&gt;+&lt;/code&gt; sign, followed by &lt;code&gt;'a'&lt;/code&gt;, a &lt;code&gt;string&lt;/code&gt;. The &lt;code&gt;+ 'a'&lt;/code&gt; is then evaluated as a unary operation, but since 'a' is not a number, JavaScript tries to convert it which results in a &lt;code&gt;NaN&lt;/code&gt;, the JavaScript global property that indicates a failed numeric expression. Then it gets converted to a string, &lt;code&gt;'NaN'&lt;/code&gt;, to complete the concatenation operation. Phew!&lt;/li&gt;
&lt;li&gt;Now we have &lt;code&gt;'baNaN' + 'a'&lt;/code&gt;, which is the last step of the string concatenation, nothing unusual there.&lt;/li&gt;
&lt;li&gt;Last but not least, &lt;code&gt;baNaNa.toLowerCase()&lt;/code&gt; gets rid of the &lt;code&gt;NaN&lt;/code&gt; to make the joke less obvious.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The key thing happens in step #2. The expression &lt;code&gt;+ + 'a'&lt;/code&gt; is something that would be considered an invalid syntax in most languages, but JavaScript tries to &lt;em&gt;guess&lt;/em&gt; what you're trying to accomplish and then things get weird.&lt;/p&gt;

&lt;p&gt;JavaScript is very permissive when it comes to interaction between its different types. Whenever a &lt;code&gt;string&lt;/code&gt; is present or an operation is happening between two incompatible types, JavaScript will try to transform everything to a &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Of course this is a generalization and there are exceptions, but &lt;code&gt;1 + '1' === '11'&lt;/code&gt; (number + string) and &lt;code&gt;[1, 2, 3] + 4 === '1,2,34'&lt;/code&gt; (array + number) are two good examples of such behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript is bad
&lt;/h3&gt;

&lt;p&gt;For years JavaScript wasn't taken seriously, often considered a bad scripting language that couldn't be avoided due how Browsers work.&lt;/p&gt;

&lt;p&gt;Reality is the language has evolved quite a bit, it has an engaged community, a diligent development process through TC39, and it enables a variety of different solutions in all sorts of platforms and devices. However, that wasn't always the case and some of its (weird) early days behaviors remain the same.&lt;/p&gt;

&lt;p&gt;I appreciate its power and all the products that were made possible due to its resilience and extensibility. These peculiarities will remain a good source of jokes for years to come nonetheless, and that is just another thing I love about the language.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>jokes</category>
      <category>twitter</category>
    </item>
  </channel>
</rss>
