<?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: Khanh Luu</title>
    <description>The latest articles on DEV Community by Khanh Luu (@kkhanhluu).</description>
    <link>https://dev.to/kkhanhluu</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%2F277579%2Fda13defb-e7f7-41c6-89dc-73cafb5f830b.png</url>
      <title>DEV Community: Khanh Luu</title>
      <link>https://dev.to/kkhanhluu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kkhanhluu"/>
    <language>en</language>
    <item>
      <title>I recreated the classic tetris game with react and zustand</title>
      <dc:creator>Khanh Luu</dc:creator>
      <pubDate>Thu, 23 Jun 2022 00:06:47 +0000</pubDate>
      <link>https://dev.to/kkhanhluu/i-recreated-the-classic-tetris-game-with-react-and-zustand-9bp</link>
      <guid>https://dev.to/kkhanhluu/i-recreated-the-classic-tetris-game-with-react-and-zustand-9bp</guid>
      <description>&lt;p&gt;Hey guys, I want to show my latest project: The classic tetris game. Checkout the &lt;a href="https://kkhanhluu.github.io/react-tetris/"&gt;demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/pteELGFZlrbLosmbiR/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/pteELGFZlrbLosmbiR/giphy.gif" width="480" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The techstack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt;: Javascript library for building user interfaces&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/pmndrs/zustand"&gt;Zustand&lt;/a&gt;: State management library for react app&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API"&gt;Web audio api&lt;/a&gt;: This API provides a system to control audio on the web&lt;/li&gt;
&lt;li&gt;&lt;a href="https://styled-components.com/"&gt;Styled component&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://vitejs.dev/"&gt;Vite&lt;/a&gt;: A frontend build tool&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Acknowledgement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This repository is highly inspired by &lt;a href="https://github.com/trungk18/angular-tetris"&gt;Angular tetris&lt;/a&gt; and &lt;a href="https://github.com/chvin/react-tetris/blob/master/README-EN.md"&gt;this react tetris&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;The animation was built with the help of &lt;a href="https://overreacted.io/making-setinterval-declarative-with-react-hooks/"&gt;the useInterval hook from Dan Abramov&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  That's it
&lt;/h2&gt;

&lt;p&gt;That's the first time that I work with web audio api and zustand and I had so much fun working with those new stuffs.&lt;/p&gt;

&lt;p&gt;You can checkout &lt;a href="https://github.com/kkhanhluu/react-tetris"&gt;my repository&lt;/a&gt; to get more information&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>react</category>
      <category>webdev</category>
      <category>typescript</category>
    </item>
    <item>
      <title>The first javascript’s pillar: Types</title>
      <dc:creator>Khanh Luu</dc:creator>
      <pubDate>Mon, 14 Mar 2022 17:40:04 +0000</pubDate>
      <link>https://dev.to/kkhanhluu/the-first-javascripts-pillar-types-2m17</link>
      <guid>https://dev.to/kkhanhluu/the-first-javascripts-pillar-types-2m17</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This post was orginally posted on &lt;a href="https://kkhanhluu.github.io/types-in-javascript/" rel="noopener noreferrer"&gt;https://kkhanhluu.github.io/types-in-javascript/&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d6j2dqbhe82mi5rzuag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5d6j2dqbhe82mi5rzuag.png" alt="Javascript meme"&gt;&lt;/a&gt;If you search for javascript memes in google, there will be 296.000.000 results and many of them are about corner cases in this language like the meme above. Those corner cases are weird, unpredictable and should be avoided, only when we do not know how javascript works and what’s going under the hood. When we encounter such confusions, it’s easier to make meme of them and blame the language than blaming ourself for our lack of understanding of the tool, which we use everyday. I used to be that type of developer until i saw the book series &lt;em&gt;You don’t know js&lt;/em&gt; of Kyle Simpson on github a few years ago. It completely changed my mind. After spending years poring over the series and watching Kyle’s courses, it turned out I didn’t know Javascript as much as i thought. On my journey to understand javascript better, i really want to note down the knowledge and the experience i gained and this series is the beginning of that adventure.&lt;/p&gt;

&lt;p&gt;In order to get a better grip of javascript, let’s take a look into its core, which according to Kyle, can be divided into 3 pillars:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Types&lt;/li&gt;
&lt;li&gt;Scope&lt;/li&gt;
&lt;li&gt;Object and classes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this blog, we’ll go into the first pillar: &lt;strong&gt;Types&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Built-in types
&lt;/h2&gt;

&lt;p&gt;One thing we should make clear before diving into types: &lt;strong&gt;Variables don’t have types, but the values held by them do.&lt;/strong&gt; There are 7 &lt;a href="https://tc39.es/ecma262/#sec-ecmascript-language-types" rel="noopener noreferrer"&gt;built-in primitive types&lt;/a&gt; in javascript: &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;object&lt;/code&gt;, &lt;code&gt;symbol&lt;/code&gt;. Operator &lt;code&gt;typeof&lt;/code&gt; can be used to identify them&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "object"; 😩&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "undefined";&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "boolean";&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "number";&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Khanh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "string";&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Khanh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// "object";&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// "symbol";&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the &lt;code&gt;typeof&lt;/code&gt; operator will return a string representing the value’s type, surprisingly except for &lt;code&gt;null&lt;/code&gt; type. &lt;del&gt;This bug&lt;/del&gt; this feature stands from the very &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#typeof_null" rel="noopener noreferrer"&gt;first implementation of javascript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;💡 All of these types except &lt;code&gt;object&lt;/code&gt; are called "primitives".&lt;/p&gt;

&lt;h3&gt;
  
  
  undefined vs undeclared
&lt;/h3&gt;

&lt;p&gt;It’s tempting to think &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;undeclared&lt;/code&gt; are synonyms and those terms can be used interchangeably but in fact, they are 2 different concepts. An &lt;code&gt;undefined&lt;/code&gt; variable is one that is already declared, is accessible in scope, but has &lt;em&gt;currently&lt;/em&gt; no value. By contrast, &lt;code&gt;undeclared&lt;/code&gt; is one that is not declared, not accessible in the scope. When we try to use &lt;code&gt;undeclared&lt;/code&gt; variable, the &lt;code&gt;ReferenceError&lt;/code&gt; will be thrown&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;undefinedVar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;undefinedVar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nx"&gt;undeclaredVar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// ReferenceError: undeclaredVar is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Type coercion
&lt;/h2&gt;

&lt;p&gt;Coercion aka 'type conversion' is a mechanism of converting one type to another. There are two types of coercion: "implicit" and "explicit". Here is an example of coercion taken from &lt;em&gt;You don’t know js&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// implicit coercion&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// explicit coercion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does coercion work internally and what’s going under the hood? In order to know the internal procedures, we need to understand &lt;strong&gt;abstract operations.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstract operations
&lt;/h3&gt;

&lt;p&gt;Every time a coercion happens, it’s handled by one or more &lt;a href="https://tc39.es/ecma262/#sec-abstract-operations" rel="noopener noreferrer"&gt;abstract operation&lt;/a&gt;. They are &lt;strong&gt;internal-only operations&lt;/strong&gt; and not like a function that could somehow be called. Here we will look into 3 abstract operations: &lt;strong&gt;ToPrimitive, ToString&lt;/strong&gt; and &lt;strong&gt;ToNumber.&lt;/strong&gt; There are more operations to refer and use, you can check the &lt;a href="https://tc39.es/ecma262/#sec-abstract-operations" rel="noopener noreferrer"&gt;spec&lt;/a&gt; for more information&lt;/p&gt;

&lt;h4&gt;
  
  
  ToPrimitive
&lt;/h4&gt;

&lt;p&gt;If we have something non-primitive (array, object,...) and want to make it into primitive, &lt;code&gt;ToPrimitive&lt;/code&gt; is the first abstract operation involving in. The operation takes 2 argument: input and the optional &lt;strong&gt;preferredType (hint),&lt;/strong&gt; which can be either &lt;em&gt;string&lt;/em&gt; or &lt;em&gt;number&lt;/em&gt;. All built-in types except for &lt;code&gt;object&lt;/code&gt; are primitives, so every non-primitive has 2 available methods derived from &lt;code&gt;Object.prototype&lt;/code&gt;: &lt;strong&gt;toString()&lt;/strong&gt; and &lt;strong&gt;valueOf()&lt;/strong&gt;. If the hint is &lt;em&gt;string,&lt;/em&gt; &lt;code&gt;toString()&lt;/code&gt; is invoked first. If the result is primitive value, &lt;code&gt;valueOf&lt;/code&gt; will come into play and vice versa if the hint is &lt;em&gt;number&lt;/em&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;hint: “string”&lt;/th&gt;
&lt;th&gt;hint: “number”&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;toString()&lt;/td&gt;
&lt;td&gt;valueOf()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;valueOf()&lt;/td&gt;
&lt;td&gt;toString()&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;ToPrimitive&lt;/code&gt; is inherently recursive, that means if the result of the operation is not primitive, the operation will be invoked again until the result is primitive.&lt;/p&gt;

&lt;p&gt;💡 By default, the inherited method valueOf from Object.prototype returns the object itself. For example, [].valueOf() returns [] or {}.valueOf() returns {}&lt;/p&gt;

&lt;h4&gt;
  
  
  ToString
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't get confused with &lt;code&gt;ToString&lt;/code&gt; and &lt;code&gt;Object.prototype.toString()&lt;/code&gt;, they are 2 different things. &lt;code&gt;ToString&lt;/code&gt; is an abstract operation, an internal operation, meanwhile, &lt;code&gt;Object.prototype.toString()&lt;/code&gt; is a function derived from &lt;code&gt;Object.prototype&lt;/code&gt; and available to all objects in javascript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The coercion of non-string value to string is handled by &lt;code&gt;ToString&lt;/code&gt; operation. It converts the value according to this &lt;a href="https://tc39.es/ecma262/#sec-tostring" rel="noopener noreferrer"&gt;table&lt;/a&gt; and here are some examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;      &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;       &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;null&lt;/span&gt;           &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;null&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt;           &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;            &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="mi"&gt;15&lt;/span&gt;             &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;              &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;15&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For non-primitive values, &lt;code&gt;ToPrimitive&lt;/code&gt; will be called with hint &lt;em&gt;string,&lt;/em&gt; which in turn invoke the &lt;code&gt;Object.prototype.toString()&lt;/code&gt; and then &lt;code&gt;valueOf()&lt;/code&gt; (if necessary). The default implementation of &lt;code&gt;Object.prototype.toString()&lt;/code&gt; returns &lt;code&gt;[Object object]&lt;/code&gt;. Array itself has an overridden implementation for &lt;strong&gt;toString()&lt;/strong&gt;: It removes the square brackets and concatenate array element with &lt;code&gt;,&lt;/code&gt;. This can lead to some &lt;del&gt;weird&lt;/del&gt; interesting results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;[]&lt;/span&gt;                           &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;    &lt;span class="dl"&gt;""&lt;/span&gt;   &lt;span class="err"&gt;🤔&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;                    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1, 2, 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  &lt;span class="err"&gt;😳&lt;/span&gt;
&lt;span class="p"&gt;[,,,]&lt;/span&gt;                        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;,,,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ToNumber
&lt;/h3&gt;

&lt;p&gt;The operation converts a non-numeric value to a number according to this &lt;a href="https://tc39.es/ecma262/#sec-tonumber" rel="noopener noreferrer"&gt;table&lt;/a&gt;. For non-primitive values, &lt;code&gt;ToPrimitive&lt;/code&gt; will be called with hint &lt;em&gt;number,&lt;/em&gt; which in turn invoke the &lt;code&gt;valueOf()&lt;/code&gt; and then &lt;code&gt;Object.prototype.toString()&lt;/code&gt; (if necessary). Because the default &lt;code&gt;valueOf()&lt;/code&gt; returns the object itself. Let’s take an example to understand the operation better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Because &lt;code&gt;[""]&lt;/code&gt; is not a primitive value, the &lt;code&gt;ToPrimitive()&lt;/code&gt; will be invoked with hint &lt;em&gt;number&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;valueOf()&lt;/code&gt; will be invoked, which returns the object itself. The result from &lt;code&gt;valueOf()&lt;/code&gt; is not a primitive value so the &lt;code&gt;Object.prototype.toString()&lt;/code&gt; will come into play.&lt;/li&gt;
&lt;li&gt;The overridden implementation of array’s &lt;code&gt;toString()&lt;/code&gt; removes the square bracket and concatenate array’s element with &lt;code&gt;,&lt;/code&gt;, so &lt;code&gt;[""].toString()&lt;/code&gt; returns &lt;code&gt;""&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Look up the table i mentioned above, the empty string will be converted into 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cases of coercion
&lt;/h3&gt;

&lt;p&gt;With those abstraction operations as foundation, it’s time to tackle the topic of coercion. Is type coercion really an evil and a horrible part, that we should avoid? You can claim to avoid coercion because it’s corrupt but in some cases, coercion is really helpful or you may have used it without knowing about it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`My brother is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old`&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// "My brother 25 years old"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How on earth can javascript concatenate the string “My brother is” to &lt;code&gt;age&lt;/code&gt;, whose value is currently a number? Yeah, you’re right, it’s type coercion. Without type coercion, you need to convert the age explicitly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`My brother is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; years old`&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// "My brother 25 years old"&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`My brother is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt; years old`&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// "My brother 25 years old"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, the first version is always my preference because of its conciseness and readability.&lt;/p&gt;

&lt;p&gt;Another example of type coercion that you should have seen in many code bases as working with browsers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addNumber&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or there’s a if statement using type coercion, that every js developer should have written:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Oh, that's having a value&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Assemble our knowledge
&lt;/h3&gt;

&lt;p&gt;After knowing some abstract operations and how it works, now we should be able to explain some of corner cases in the above meme. Let’s go through some of it&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;[] + [] -&amp;gt; ""&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;The result of &lt;code&gt;ToString()&lt;/code&gt; with empty array is &lt;code&gt;“”&lt;/code&gt;, so &lt;code&gt;""&lt;/code&gt; concatenating with &lt;code&gt;""&lt;/code&gt; of course returns &lt;code&gt;“”&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;[] + {} -&amp;gt; "[Object object]"&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;It should be an easy one. &lt;code&gt;[]&lt;/code&gt; is converted to &lt;code&gt;""&lt;/code&gt; and the default &lt;code&gt;Object.prototype.toString()&lt;/code&gt; returns &lt;code&gt;"[Object object]"&lt;/code&gt;, so the result if of course string “[Object object]”&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;{} + [] -&amp;gt; 0&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Hm, that’s really a tricky one. Since curly braces at the beginning of a statement are interpreted as starting of code block, the first pair of curly brackets is interpreted as an empty code block. So this expression is equivalent to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;                             &lt;span class="c1"&gt;// The plus here is an unary operator, which converts [] to number&lt;/span&gt;
&lt;span class="nc"&gt;ToNumber&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;                    &lt;span class="c1"&gt;// calls toPrimitive with hint number&lt;/span&gt;
&lt;span class="nc"&gt;ToPrimitive&lt;/span&gt;&lt;span class="p"&gt;([],&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;// calls valueOf() first and then toString() if necessary&lt;/span&gt;
                                &lt;span class="c1"&gt;// [].valueOf() returns [], which is not primitive, so we have to use toString()&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;([].&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;code&gt;true + true + true = 3&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;The plus here is a binary operator, so &lt;code&gt;true&lt;/code&gt; will be converted into number 1, please refer to the table i mentioned in &lt;code&gt;ToNumber&lt;/code&gt;. So yes, true + true + true is really 3 in javascript.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;(! + [] + [] + ![]).length = 9&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;The first exclamation mark performs a boolean coercion, the first unary plus operator handles a numeric coercion. So the first three symbols &lt;code&gt;!+[]&lt;/code&gt; will perform firstly a numeric conversion of an empty array, and then convert that result to a boolean value. The second &lt;code&gt;[]&lt;/code&gt; will be converted into primitive like the way i explained in previous examples, and the last &lt;code&gt;[]&lt;/code&gt; is converted to boolean with &lt;code&gt;[ToBoolean&lt;/code&gt; abstract operation](&lt;a href="https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toboolean" rel="noopener noreferrer"&gt;https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toboolean&lt;/a&gt;), which i don’t mention in this blog. So this expression is equivalent to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;([].&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;false&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;false&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;truefalse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this post, we turn our attention to the types systems and how type conversions in javascript works. The implicit type coercion in javascript is handled by &lt;em&gt;abstract operations&lt;/em&gt;. Dynamic types is one of JS’s core features but on the other hand, it’s also controversial. In order to end this post, I’d like to take a quote of Kyle Simpson from his famous series &lt;em&gt;You don’t know JS&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Coercion gets a bad rap, but it's actually quite useful in many cases. An important task for the responsible JS developer is to take the time to learn all the ins and outs of coercion to decide which parts will help improve their code, and which parts they really should avoid.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
