<?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: Ambrose Liew</title>
    <description>The latest articles on DEV Community by Ambrose Liew (@morninglit).</description>
    <link>https://dev.to/morninglit</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%2F1026477%2F2de7924c-a0d4-4739-85fb-8e7f01658e4e.png</url>
      <title>DEV Community: Ambrose Liew</title>
      <link>https://dev.to/morninglit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/morninglit"/>
    <language>en</language>
    <item>
      <title>Most Developers Fail This Simple CSS Interview Question (CSS Specificity)</title>
      <dc:creator>Ambrose Liew</dc:creator>
      <pubDate>Mon, 24 Apr 2023 14:34:14 +0000</pubDate>
      <link>https://dev.to/morninglit/most-developers-fail-this-simple-css-interview-question-css-specificity-pmk</link>
      <guid>https://dev.to/morninglit/most-developers-fail-this-simple-css-interview-question-css-specificity-pmk</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Think CSS is easy? Think again!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kRAXzDW6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/v2/resize:fit:720/format:webp/1%2Auseg7xOof7Qr4PZMtvta5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kRAXzDW6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/v2/resize:fit:720/format:webp/1%2Auseg7xOof7Qr4PZMtvta5w.png" alt="Photo by Maranda Vandergriff on Unsplash" width="720" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Test
&lt;/h2&gt;

&lt;p&gt;Given the following HTML and CSS code, do you know what would be the &lt;strong&gt;color&lt;/strong&gt; of the text &lt;strong&gt;‘test’&lt;/strong&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"abc"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;test&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;    &lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="nc"&gt;.abc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;purple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;.hello&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;.abc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&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;blockquote&gt;
&lt;p&gt;Take a second to think through&lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;&lt;em&gt;Intention space left blank so that you don’t accidentally see the answer&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;&lt;em&gt;More intentional blank space&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please give it a shot before looking at the answer&lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;If you had guessed the color was &lt;strong&gt;red&lt;/strong&gt;, then you are &lt;strong&gt;correct!&lt;/strong&gt; You would also have a solid understanding of &lt;strong&gt;CSS Specificity.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can have a quick confirmation of the answer through this &lt;a href="https://codepen.io/ambroseliew/pen/jOpzXpj"&gt;&lt;strong&gt;codepen.&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/ambroseliew/embed/jOpzXpj?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  But why?
&lt;/h2&gt;

&lt;p&gt;If you did not manage to get the correct answer or understand why the text color was &lt;strong&gt;red&lt;/strong&gt;, you can &lt;a href="https://dev.to/morninglit/stop-writing-important-in-your-css-code-and-do-this-instead-2knd"&gt;check out this other article where I teach &lt;strong&gt;CSS Specificity&lt;/strong&gt; in detail here.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/morninglit" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HjOOUvoH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--p0xsGche--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/1026477/2de7924c-a0d4-4739-85fb-8e7f01658e4e.png" alt="morninglit"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/morninglit/stop-writing-important-in-your-css-code-and-do-this-instead-2knd" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Stop Writing !important in your CSS Code, and Do This Instead!&lt;/h2&gt;
      &lt;h3&gt;Ambrose Liew ・ Apr 24 ・ 3 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#css&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#html&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;Understanding this concept is important because concepts like this are always being used if you are a &lt;strong&gt;Web Developer,&lt;/strong&gt; and especially so if you are a &lt;strong&gt;Frontend Developer.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Walkthrough
&lt;/h2&gt;

&lt;p&gt;But to break it down further, the &lt;strong&gt;CSS Specificity&lt;/strong&gt; for the rule that applies the color &lt;strong&gt;purple,&lt;/strong&gt; and the other rule that applies the color &lt;strong&gt;red&lt;/strong&gt;, both have a specificity of &lt;strong&gt;0–0–0–1–1.&lt;/strong&gt; This is because they both have &lt;strong&gt;1 Class Selector&lt;/strong&gt; and &lt;strong&gt;1 Type Selector.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Meanwhile, for the rule that applies the color &lt;strong&gt;blue&lt;/strong&gt; has a &lt;strong&gt;CSS Specificity&lt;/strong&gt; of &lt;strong&gt;0–0–0–1–0,&lt;/strong&gt; because it only has &lt;strong&gt;1 Class Selector.&lt;/strong&gt; While, the rule that applies the color &lt;strong&gt;green&lt;/strong&gt; has a &lt;strong&gt;CSS Specificity&lt;/strong&gt; of &lt;strong&gt;0–0–0–0–1,&lt;/strong&gt; because it only has &lt;strong&gt;1 Type Selector.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thus, the rules that have the &lt;strong&gt;highest CSS Specificity value&lt;/strong&gt;, are the rule that apply the color &lt;strong&gt;purple&lt;/strong&gt; and the other rule that apply the color &lt;strong&gt;red&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But 2 CSS rules with the same &lt;strong&gt;CSS Specificity value!&lt;/strong&gt; How do we know which to apply?&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;‘Cascading’&lt;/strong&gt; in &lt;strong&gt;Cascading Style Sheets (CSS)&lt;/strong&gt; comes into play. What this means is that whichever rule is applied the &lt;strong&gt;latest&lt;/strong&gt; (the rule that is found at the &lt;strong&gt;bottommost&lt;/strong&gt;) will be the one that is applied. In this case, it would be the rule that applies the color &lt;strong&gt;red&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus
&lt;/h2&gt;

&lt;p&gt;We can even take it a step further by adding another CSS rule to the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;    &lt;span class="nc"&gt;.hello.hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;pink&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;What do you think would the outcome of the color be? &lt;strong&gt;Red&lt;/strong&gt; or &lt;strong&gt;pink?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And yes, you can stack &lt;strong&gt;Class Selectors&lt;/strong&gt; (and &lt;strong&gt;ID Selectors&lt;/strong&gt;) to increase the &lt;strong&gt;specificity&lt;/strong&gt;. Thus, the CSS Specificity for this rule would be 0–0–0–2–0. This value is definitely higher than all the other rules so far.&lt;/p&gt;

&lt;p&gt;However, if you add this CSS rule to the existing CSS code so far in the &lt;a href="https://codepen.io/ambroseliew/pen/jOpzXpj"&gt;&lt;strong&gt;codepen&lt;/strong&gt;&lt;/a&gt;, you would realize that the color of the text is still &lt;strong&gt;red!&lt;/strong&gt; Why is that?&lt;/p&gt;

&lt;p&gt;It is because rules that &lt;strong&gt;directly target elements&lt;/strong&gt; will always take priority over rules that &lt;strong&gt;target the parents&lt;/strong&gt; (aka &lt;strong&gt;inherited styles&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;Thus, in this case, since this CSS rule targets the &lt;strong&gt;parent&lt;/strong&gt; instead of the &lt;strong&gt;actual p tag itself,&lt;/strong&gt; it would be overwritten by the other more specific CSS rules. And yes, the CSS rule that applies the color &lt;strong&gt;green&lt;/strong&gt; would also overwrite this &lt;strong&gt;pink&lt;/strong&gt; color.&lt;/p&gt;




&lt;p&gt;If you felt that you have learned something new, do feel free to &lt;strong&gt;follow&lt;/strong&gt; 🥰, drop a &lt;strong&gt;react 💖&lt;/strong&gt; or write a &lt;strong&gt;comment&lt;/strong&gt; ✍️!&lt;br&gt;
It helps makes me create and share more valuable content for &lt;strong&gt;you&lt;/strong&gt; to become a better &lt;strong&gt;Web Developer!&lt;/strong&gt; 🤗&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Stop Writing !important in your CSS Code, and Do This Instead!</title>
      <dc:creator>Ambrose Liew</dc:creator>
      <pubDate>Mon, 24 Apr 2023 14:20:17 +0000</pubDate>
      <link>https://dev.to/morninglit/stop-writing-important-in-your-css-code-and-do-this-instead-2knd</link>
      <guid>https://dev.to/morninglit/stop-writing-important-in-your-css-code-and-do-this-instead-2knd</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Ever wrote !important in your CSS file to override a CSS property? &lt;br&gt;
Come learn CSS Specificity to remove the need to write !important ever again!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ppBFsyPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/12000/0%2Ap-AayuO3yv3OoSti" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ppBFsyPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/12000/0%2Ap-AayuO3yv3OoSti" alt="Photo by [Maik Jonietz](https://unsplash.com/@der_maik_?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why shouldn’t I use !important in my code?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First and foremost, I would like to address the elephant in the room. And that is why we should not write &lt;strong&gt;!important&lt;/strong&gt; in CSS.&lt;/p&gt;

&lt;p&gt;There are a number of reasons, but the main reason is that if you are working on a large project with other developers, writing &lt;strong&gt;!important&lt;/strong&gt; makes the project more unsustainable to manage.&lt;/p&gt;

&lt;p&gt;What do I mean by unsustainable to manage? If more developers in your team started to write &lt;strong&gt;!important&lt;/strong&gt; in the CSS codebase, it becomes increasingly difficult to apply your own styles. You would have to resort to writing &lt;strong&gt;!important&lt;/strong&gt; yourself in hopes to override their styles or you will have to edit their styles to decrease their CSS specificity.&lt;/p&gt;

&lt;p&gt;What does CSS specificity mean? Continue reading to find out more.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;What is CSS Specificity?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;CSS Specificity is the algorithm that determines what CSS property is applied to an element.&lt;/p&gt;

&lt;p&gt;There are 5 main categories that contribute to CSS Specificity. They are &lt;strong&gt;!important, inline styles, ID, Class, and types.&lt;/strong&gt; You can think of these categories in the form of this representation: &lt;strong&gt;0–0–0–0–0&lt;/strong&gt; in that order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;    &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;!important&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;In&lt;/span&gt; &lt;span class="nt"&gt;CSS&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;this&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;important&lt;/span&gt; &lt;span class="nt"&gt;property&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"color: purple"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    In HTML, this is the Inline Styles
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;    &lt;span class="nf"&gt;#abc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;.def&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
     &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
     &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nt"&gt;Any&lt;/span&gt; &lt;span class="nt"&gt;word&lt;/span&gt; &lt;span class="nt"&gt;with&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="s2"&gt;'#'&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;front&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;an&lt;/span&gt; &lt;span class="nt"&gt;ID&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
    &lt;span class="nt"&gt;Any&lt;/span&gt; &lt;span class="nt"&gt;word&lt;/span&gt; &lt;span class="nt"&gt;with&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="s2"&gt;'.'&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;front&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;Class&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
    &lt;span class="nt"&gt;Without&lt;/span&gt; &lt;span class="nt"&gt;anything&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;front&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;Type&lt;/span&gt; &lt;span class="nt"&gt;selector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;    &lt;span class="nf"&gt;#container&lt;/span&gt; &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.home-select&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;.dropdown&lt;/span&gt; &lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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;The above CSS property has the specificity: &lt;strong&gt;0–0–1–2–2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because there is &lt;strong&gt;1 ID&lt;/strong&gt; selector (&lt;strong&gt;#container&lt;/strong&gt;), &lt;strong&gt;2 Class&lt;/strong&gt; selectors (&lt;strong&gt;.home-select&lt;/strong&gt;, &lt;strong&gt;.dropdown&lt;/strong&gt;) and &lt;strong&gt;2 Type&lt;/strong&gt; selectors (&lt;strong&gt;div&lt;/strong&gt;, &lt;strong&gt;span&lt;/strong&gt;). &lt;br&gt;
&lt;em&gt;Assuming there is no inline-styles applied to it.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  So how do I apply this knowledge?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Try to be more &lt;strong&gt;specific&lt;/strong&gt; with your CSS selectors, a &lt;strong&gt;higher&lt;/strong&gt; value will always override a lower value. &lt;br&gt;
Eg. &lt;strong&gt;0–0–1–0–0&lt;/strong&gt; has higher specificity than &lt;strong&gt;0–0–0–10–5&lt;/strong&gt;.&lt;br&gt;
&lt;em&gt;The algorithm will check from left to right column, and whichever value has the higher value, will be applied for the CSS property.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remember the CSS also &lt;strong&gt;Cascades&lt;/strong&gt;, so if you have 2 CSS properties with the same specificity value, put your desired CSS property after (&lt;strong&gt;below&lt;/strong&gt;) the other undesired CSS property.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always try and refrain from using the most powerful tool first, if you do not want to calculate the CSS specificity, you can try to use &lt;strong&gt;inline styles&lt;/strong&gt; instead.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be sure to check out the &lt;a href="https://caniuse.com/css-cascade-layers"&gt;brand new CSS feature&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Cascade"&gt;&lt;strong&gt;CSS Cascade&lt;/strong&gt; for an alternative, modern solution too!&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can also test out your new profound knowledge with &lt;a href="https://dev.to/morninglit/most-developers-fail-this-simple-css-interview-question-css-specificity-pmk"&gt;this &lt;strong&gt;Simple Interview Question&lt;/strong&gt;!&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/morninglit" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HjOOUvoH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--p0xsGche--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/1026477/2de7924c-a0d4-4739-85fb-8e7f01658e4e.png" alt="morninglit"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/morninglit/most-developers-fail-this-simple-css-interview-question-css-specificity-pmk" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Most Developers Fail This Simple CSS Interview Question (CSS Specificity)&lt;/h2&gt;
      &lt;h3&gt;Ambrose Liew ・ Apr 24 ・ 3 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#css&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you felt that you have learned something new, do feel free to &lt;strong&gt;follow&lt;/strong&gt; 🥰, drop a &lt;strong&gt;react 💖&lt;/strong&gt; or write a &lt;strong&gt;comment&lt;/strong&gt; ✍️!&lt;br&gt;
It helps makes me create and share more valuable content for &lt;strong&gt;you&lt;/strong&gt; to become a better &lt;strong&gt;Web Developer!&lt;/strong&gt; 🤗&lt;/p&gt;




&lt;h2&gt;
  
  
  Extras
&lt;/h2&gt;

&lt;p&gt;Since you cannot stack !important and inline styles, the first two values can only be either &lt;strong&gt;1&lt;/strong&gt; or &lt;strong&gt;0&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For those wondering, the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt; is the symbol for the &lt;strong&gt;direct child combinator.&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Attribute selectors&lt;/strong&gt; like &lt;code&gt;[type="radio"]&lt;/code&gt; and &lt;code&gt;[lang|="fr"]&lt;/code&gt;and &lt;strong&gt;Pseudo-classes&lt;/strong&gt; like &lt;code&gt;:hover&lt;/code&gt;, &lt;code&gt;:nth-of-type(3n)&lt;/code&gt;, and &lt;code&gt;:required&lt;/code&gt; all add to the &lt;strong&gt;Class&lt;/strong&gt; specificity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pseudo-elements&lt;/strong&gt; like &lt;code&gt;::before&lt;/code&gt;, &lt;code&gt;::placeholder&lt;/code&gt; all add to the &lt;strong&gt;Type&lt;/strong&gt; specificity.&lt;/p&gt;

&lt;p&gt;There are also certain selectors without any specificity value, eg: &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;where()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can actually stack classes/ids to increase the specificity, eg:&lt;br&gt;
&lt;strong&gt;#home#home#home .container.container&lt;/strong&gt; would have the specificity value of 0–0–3–2–0.&lt;/p&gt;

&lt;p&gt;A pictorial view of what I have explained can be found &lt;a href="https://specifishity.com/"&gt;&lt;strong&gt;here!&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A game about CSS Selectors is found &lt;a href="https://flukeout.github.io/"&gt;&lt;strong&gt;here!&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are more detailed explanations found &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity"&gt;&lt;strong&gt;here!&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>html</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The Full Practical Guide To Type Coercion In JavaScript: Beware Of These 3 Common Mistakes</title>
      <dc:creator>Ambrose Liew</dc:creator>
      <pubDate>Mon, 24 Apr 2023 14:09:33 +0000</pubDate>
      <link>https://dev.to/morninglit/the-full-practical-guide-to-type-coercion-in-javascript-beware-of-these-3-common-mistakes-1f7p</link>
      <guid>https://dev.to/morninglit/the-full-practical-guide-to-type-coercion-in-javascript-beware-of-these-3-common-mistakes-1f7p</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Ever used ‘&lt;strong&gt;==&lt;/strong&gt;’ in &lt;strong&gt;JavaScript?&lt;/strong&gt;&lt;br&gt;
Or &lt;strong&gt;‘if (x)’&lt;/strong&gt;in &lt;strong&gt;JavaScript?&lt;/strong&gt;&lt;br&gt;
Or used &lt;strong&gt;String Concatenation?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Hopefully, after reading this article, you can obtain some insights about the &lt;strong&gt;common mistakes&lt;/strong&gt; to avoid in &lt;strong&gt;JavaScript&lt;/strong&gt;!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0M_ao7yh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/1%2AMpYDyNOCUdcEWuEbo1LsTA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0M_ao7yh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/1%2AMpYDyNOCUdcEWuEbo1LsTA.png" alt="The Mythical Land of JavaScript" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The 7 Primitive Types
&lt;/h2&gt;

&lt;p&gt;Before I begin teaching &lt;strong&gt;Type Coercion&lt;/strong&gt; in &lt;strong&gt;JavaScript&lt;/strong&gt;, I would like to introduce the &lt;a href="https://262.ecma-international.org/13.0/#sec-ecmascript-language-types"&gt;&lt;strong&gt;7 primitive types&lt;/strong&gt; in &lt;strong&gt;JavaScript&lt;/strong&gt;.&lt;/a&gt; And they are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;undefined&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;null&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;boolean&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;symbol&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;number&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;object&lt;/strong&gt; &lt;em&gt;(for those wondering what is the type of &lt;strong&gt;array, arrays&lt;/strong&gt; are a special type of &lt;strong&gt;object&lt;/strong&gt;)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But for the focus of this article, we would be focusing only on the most commonly used types which are: &lt;strong&gt;boolean&lt;/strong&gt;, &lt;strong&gt;string, and number&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Type Coercion?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Type Coercion&lt;/strong&gt; is the &lt;strong&gt;conversion&lt;/strong&gt; of one type to another. There are two forms of &lt;strong&gt;Type Coercion:&lt;/strong&gt; &lt;strong&gt;Explicit&lt;/strong&gt; and &lt;strong&gt;Implicit.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explicit Coercion&lt;/strong&gt; is the conversion of a value from one type to another using explicit methods such as &lt;strong&gt;Number(), parseInt(),&lt;/strong&gt; and &lt;strong&gt;String().&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implicit Coercion&lt;/strong&gt; is the automatic conversion of one type to another. &lt;br&gt;
&lt;em&gt;This occurs because the operation performed on the 2 types is incompatible, and &lt;strong&gt;JavaScript&lt;/strong&gt; has to step in to make it &lt;strong&gt;compatible&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We would be focusing on &lt;strong&gt;Implicit Coercion&lt;/strong&gt; instead as that is where most of the &lt;strong&gt;bugs&lt;/strong&gt; occur.&lt;/p&gt;


&lt;h3&gt;
  
  
  String Concatenation
&lt;/h3&gt;

&lt;p&gt;One common place where &lt;strong&gt;Implicit Coercion&lt;/strong&gt; tends to occur is through &lt;strong&gt;String Concatenation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pkOs3JZN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2Am6CptHsa4987N5j2VJowZA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pkOs3JZN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2Am6CptHsa4987N5j2VJowZA.png" alt="Behavior when a string is concatenated with different types" width="446" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScript&lt;/strong&gt; would convert the non-string type to a &lt;strong&gt;string,&lt;/strong&gt; and &lt;strong&gt;concatenate&lt;/strong&gt; the string to deal with these &lt;strong&gt;incompatible&lt;/strong&gt; types.&lt;/p&gt;
&lt;h3&gt;
  
  
  Evaluation of Truthy Values
&lt;/h3&gt;

&lt;p&gt;Another common use case is through the short-hand usage of &lt;strong&gt;Evaluation of Truthy Values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q1PV1rao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2ACFc9GtmyHehXM-pDquFVXg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q1PV1rao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2ACFc9GtmyHehXM-pDquFVXg.png" alt="Behavior when a Non-Boolean type is being treated as a Boolean" width="407" height="159"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These occur typically in &lt;strong&gt;if statements (if (x)),&lt;/strong&gt; &lt;strong&gt;ternary operators (x ? 1 : 0),&lt;/strong&gt; and basically any place in code where a &lt;strong&gt;non-Boolean&lt;/strong&gt; variable is being treated as a &lt;strong&gt;Boolean&lt;/strong&gt; value.&lt;/p&gt;

&lt;p&gt;That is why it is &lt;strong&gt;dangerous&lt;/strong&gt; to use these short-hand syntaxes if you do not know all the &lt;strong&gt;edge cases&lt;/strong&gt; when converting a &lt;strong&gt;non-Boolean&lt;/strong&gt; type to a &lt;strong&gt;Boolean&lt;/strong&gt; type.&lt;/p&gt;
&lt;h3&gt;
  
  
  ‘==’
&lt;/h3&gt;

&lt;p&gt;Another common area where &lt;strong&gt;Implicit Coercion&lt;/strong&gt; occurs is through the usage of ‘&lt;strong&gt;==&lt;/strong&gt;’.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vg0ynFJ9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AG5m7tUVlHttLgbLNlOIkFQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vg0ynFJ9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AG5m7tUVlHttLgbLNlOIkFQ.png" alt="Behavior when using ‘==’ to compare incompatible types" width="423" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For &lt;strong&gt;number&lt;/strong&gt; and &lt;strong&gt;string&lt;/strong&gt; comparisons, &lt;strong&gt;JavaScript&lt;/strong&gt; will convert the &lt;strong&gt;string&lt;/strong&gt; to a &lt;strong&gt;number&lt;/strong&gt; using &lt;strong&gt;Number()&lt;/strong&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For &lt;strong&gt;boolean&lt;/strong&gt; and &lt;strong&gt;number&lt;/strong&gt; comparisons, &lt;strong&gt;JavaScript&lt;/strong&gt; will convert the &lt;strong&gt;boolean&lt;/strong&gt; to a &lt;strong&gt;number&lt;/strong&gt; using the &lt;strong&gt;Number()&lt;/strong&gt; function. &lt;em&gt;&lt;strong&gt;true = 1, false = 0.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For &lt;strong&gt;boolean&lt;/strong&gt; and &lt;strong&gt;string&lt;/strong&gt; comparisons, &lt;strong&gt;JavaScript&lt;/strong&gt; will convert the &lt;strong&gt;boolean&lt;/strong&gt; to a &lt;strong&gt;number&lt;/strong&gt;, then the &lt;strong&gt;string&lt;/strong&gt; is converted to a &lt;strong&gt;number&lt;/strong&gt;. &lt;em&gt;That is why (&lt;strong&gt;false == ‘0’&lt;/strong&gt;) is evaluated as &lt;strong&gt;true&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Algorithm
&lt;/h2&gt;

&lt;p&gt;If you are curious, you can check out the &lt;strong&gt;algorithm&lt;/strong&gt; for &lt;strong&gt;Implicit Coercion&lt;/strong&gt;, &lt;a href="https://262.ecma-international.org/13.0/#sec-islooselyequal"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sPM0ZIQx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2Ah4zQQEuZNEC4xF-foRgp3Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sPM0ZIQx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2Ah4zQQEuZNEC4xF-foRgp3Q.png" alt="The algorithm for IsLooselyEqual()" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;You can play with the examples above in this &lt;a href="https://jsfiddle.net/MorningLit/vb62r13o/"&gt;&lt;strong&gt;JSFiddle.&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
&lt;iframe src="https://jsfiddle.net/MorningLit/vb62r13o//embedded//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope that after reading this article you would think twice about using these &lt;strong&gt;unsafe&lt;/strong&gt; methods. You can see how by not carefully considering all the &lt;strong&gt;edge cases&lt;/strong&gt; of &lt;strong&gt;Type Coercion&lt;/strong&gt;, &lt;strong&gt;bugs&lt;/strong&gt; can easily occur.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consider these safer alternatives:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Utilising ‘&lt;strong&gt;===&lt;/strong&gt;’ instead of ‘&lt;strong&gt;==&lt;/strong&gt;’ always&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using &lt;a href="https://eslint.org/docs/latest/rules/no-implicit-coercion"&gt;&lt;strong&gt;ESLint&lt;/strong&gt;&lt;/a&gt;, to throw &lt;strong&gt;warnings&lt;/strong&gt; and &lt;strong&gt;errors&lt;/strong&gt; when you write these code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using &lt;strong&gt;TypeScript&lt;/strong&gt;, to help detect potential &lt;strong&gt;run-time&lt;/strong&gt; errors beforehand&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, of course, there is &lt;a href="https://dev.to/morninglit/the-only-situation-to-use-in-javascript-4ipd"&gt;&lt;strong&gt;one&lt;/strong&gt; situation you might want to consider using ‘&lt;strong&gt;==&lt;/strong&gt;’ in &lt;strong&gt;JavaScript&lt;/strong&gt;.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/morninglit" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HjOOUvoH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--p0xsGche--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/1026477/2de7924c-a0d4-4739-85fb-8e7f01658e4e.png" alt="morninglit"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/morninglit/the-only-situation-to-use-in-javascript-4ipd" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;The Only Situation To Use ‘==’ In JavaScript&lt;/h2&gt;
      &lt;h3&gt;Ambrose Liew ・ Apr 24 ・ 2 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Trivia
&lt;/h2&gt;

&lt;p&gt;Did you know? &lt;a href="https://twitter.com/aemkei"&gt;Someone&lt;/a&gt; even took this &lt;strong&gt;Type Coercion&lt;/strong&gt; concept to the extreme and made a &lt;strong&gt;programming language&lt;/strong&gt; out of it.&lt;/p&gt;

&lt;p&gt;Behold &lt;a href="http://www.jsfuck.com/"&gt;&lt;strong&gt;http://www.jsfuck.com/&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It uses only &lt;strong&gt;six&lt;/strong&gt; different characters to write and execute code in &lt;strong&gt;JavaScript.&lt;/strong&gt; Here is a &lt;a href="https://www.youtube.com/watch?v=sRWE5tnaxlI"&gt;video explanation of the makings of the programming language.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though this programming language looks ridiculous, there are &lt;strong&gt;two&lt;/strong&gt; practical uses for it. And that is &lt;a href="https://en.wikipedia.org/wiki/Obfuscation_(software)"&gt;&lt;strong&gt;obfuscation&lt;/strong&gt;&lt;/a&gt; and the crafting of a &lt;strong&gt;malicious payload&lt;/strong&gt; for &lt;a href="https://owasp.org/www-community/attacks/xss/"&gt;&lt;strong&gt;XSS&lt;/strong&gt;.&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;If you felt that you have learned something new, do feel free to &lt;strong&gt;follow&lt;/strong&gt; 🥰, drop a &lt;strong&gt;react 💖&lt;/strong&gt; or write a &lt;strong&gt;comment&lt;/strong&gt; ✍️!&lt;br&gt;
It helps makes me create and share more valuable content for &lt;strong&gt;you&lt;/strong&gt; to become a better &lt;strong&gt;Web Developer!&lt;/strong&gt; 🤗&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The Only Situation To Use ‘==’ In JavaScript</title>
      <dc:creator>Ambrose Liew</dc:creator>
      <pubDate>Mon, 24 Apr 2023 13:53:38 +0000</pubDate>
      <link>https://dev.to/morninglit/the-only-situation-to-use-in-javascript-4ipd</link>
      <guid>https://dev.to/morninglit/the-only-situation-to-use-in-javascript-4ipd</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Ever pondered when to use &lt;strong&gt;‘==’&lt;/strong&gt; in JavaScript?&lt;br&gt;
Or ever had the idea that one should always use &lt;strong&gt;‘===’&lt;/strong&gt; in JavaScript?&lt;br&gt;
Hopefully, by reading till the end of this article, you learned something new!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PejgqiBd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2160/1%2AwQvuYgDodBph-d7_AyQKWw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PejgqiBd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2160/1%2AwQvuYgDodBph-d7_AyQKWw.png" alt="‘==’ in JavaScript" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why?
&lt;/h2&gt;

&lt;p&gt;You might be thinking, why should one ever consider the scenario to use &lt;strong&gt;‘==’&lt;/strong&gt;? Just use &lt;strong&gt;‘===’&lt;/strong&gt; all the time to ensure consistent and expected developer behavior, this also prevents unexpected bugs from occurring.&lt;/p&gt;

&lt;p&gt;And if you’re wondering why it’s bad to use &lt;strong&gt;‘==’&lt;/strong&gt;, the main reason is that with &lt;strong&gt;‘==’&lt;/strong&gt;, &lt;strong&gt;JavaScript&lt;/strong&gt; does a &lt;strong&gt;Type Coercion.&lt;/strong&gt;&lt;br&gt;
If you do not know what that is or want to learn more about &lt;strong&gt;Type Coercion,&lt;/strong&gt; you can read about it &lt;a href="https://dev.to/morninglit/the-full-practical-guide-to-type-coercion-in-javascript-beware-of-these-3-common-mistakes-1f7p"&gt;here.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/morninglit" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HjOOUvoH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--p0xsGche--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/1026477/2de7924c-a0d4-4739-85fb-8e7f01658e4e.png" alt="morninglit"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/morninglit/the-full-practical-guide-to-type-coercion-in-javascript-beware-of-these-3-common-mistakes-1f7p" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;The Full Practical Guide To Type Coercion In JavaScript: Beware Of These 3 Common Mistakes&lt;/h2&gt;
      &lt;h3&gt;Ambrose Liew ・ Apr 24 ・ 3 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;





&lt;p&gt;Well, the main and only reason to utilize &lt;strong&gt;‘==’&lt;/strong&gt; in JavaScript is to check for &lt;strong&gt;null&lt;/strong&gt; and &lt;strong&gt;undefined&lt;/strong&gt; at the same time.&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&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;// both methods are equivalent to checking the variable 'a' &lt;/span&gt;
&lt;span class="c1"&gt;// with null and undefined at the same time.&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&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="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;// the equivalence to writing this out&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus, if you have to do a check for &lt;strong&gt;null&lt;/strong&gt; and &lt;strong&gt;undefined&lt;/strong&gt; at the same time, consider using the &lt;strong&gt;‘==’.&lt;/strong&gt; This does a shortcut instead of the otherwise, lengthier method of checking for both.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;But &lt;strong&gt;wait!&lt;/strong&gt; Ever wondered what is the difference between &lt;strong&gt;null&lt;/strong&gt; and &lt;strong&gt;undefined&lt;/strong&gt;?&lt;br&gt;
And when to use which?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Know that JavaScript will never set any value to &lt;strong&gt;null&lt;/strong&gt;. Both &lt;strong&gt;null&lt;/strong&gt; and &lt;strong&gt;undefined&lt;/strong&gt; represent the absence of value.&lt;/p&gt;

&lt;p&gt;Thus, the only difference between the two is that, whenever you want to set a variable to &lt;strong&gt;undefined&lt;/strong&gt; or &lt;strong&gt;null&lt;/strong&gt;, you should always set it to &lt;strong&gt;null&lt;/strong&gt;. Because it helps you during &lt;strong&gt;debugging&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you ever encounter a &lt;strong&gt;null&lt;/strong&gt; value, this means that somewhere along your code, you had manually set the value to &lt;strong&gt;null&lt;/strong&gt; then. Meanwhile, for &lt;strong&gt;undefined&lt;/strong&gt;, you do not know whether &lt;strong&gt;you&lt;/strong&gt; or &lt;strong&gt;JavaScript&lt;/strong&gt; &lt;em&gt;(in other words, not giving it a value when initializing)&lt;/em&gt; had set the value to be &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;Just as a bonus, &lt;a href="https://dorey.github.io/JavaScript-Equality-Table/"&gt;here is the pictorial representation&lt;/a&gt; of what values are equal to others for &lt;strong&gt;‘==’&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JETsb5Oq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2AgCmJISANYPDHTKgZ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JETsb5Oq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/0%2AgCmJISANYPDHTKgZ.png" alt="Equality Table for ‘==’" width="800" height="761"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, there is not much value for using &lt;strong&gt;‘==’&lt;/strong&gt;, besides checking for &lt;strong&gt;null&lt;/strong&gt; and &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;If you felt that you have learned something new, do feel free to &lt;strong&gt;follow&lt;/strong&gt; 🥰, drop a &lt;strong&gt;react 💖&lt;/strong&gt; or write a &lt;strong&gt;comment&lt;/strong&gt; ✍️!&lt;br&gt;
It helps makes me create and share more valuable content for &lt;strong&gt;you&lt;/strong&gt; to become a better &lt;strong&gt;Web Developer!&lt;/strong&gt; 🤗&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Never Say Never, The never Keyword In TypeScript And Exhaustive Matching</title>
      <dc:creator>Ambrose Liew</dc:creator>
      <pubDate>Mon, 24 Apr 2023 13:35:03 +0000</pubDate>
      <link>https://dev.to/morninglit/never-say-never-the-never-keyword-in-typescript-and-exhaustive-matching-2bi6</link>
      <guid>https://dev.to/morninglit/never-say-never-the-never-keyword-in-typescript-and-exhaustive-matching-2bi6</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Ever heard of the &lt;strong&gt;never&lt;/strong&gt; keyword in &lt;strong&gt;TypeScript&lt;/strong&gt; and know when to use it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ymghxd3K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A7BtXXIgGc_gT8zXEoujiVA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ymghxd3K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A7BtXXIgGc_gT8zXEoujiVA.png" alt="The never keyword in TypeScript" width="300" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Exhaustive Matching
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Exhaustive Matching&lt;/strong&gt; is when you try to match a &lt;strong&gt;discriminant property&lt;/strong&gt; to its corresponding &lt;strong&gt;value/function.&lt;/strong&gt; This is better explained through an example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MbE8MGEY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A-ktrWgctvU4hpv8ewmcGIw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MbE8MGEY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A-ktrWgctvU4hpv8ewmcGIw.png" alt="An example of Exhaustive Matching" title="An example of Exhaustive Matching" width="613" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above &lt;strong&gt;colorToString&lt;/strong&gt; function is an example of &lt;strong&gt;Exhaustive Matching.&lt;/strong&gt; Where we try to map all &lt;strong&gt;enums&lt;/strong&gt; of &lt;strong&gt;COLORS&lt;/strong&gt; to a &lt;strong&gt;corresponding value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The main idea of &lt;strong&gt;Exhaustive Matching&lt;/strong&gt; is we try to match a set of values to another set of values (can also be functions, DOM elements, etc.).&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem?
&lt;/h2&gt;

&lt;p&gt;The problem arises when we try to add another value to our &lt;strong&gt;COLORS enum.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we update our &lt;strong&gt;enums,&lt;/strong&gt; the &lt;strong&gt;compiler&lt;/strong&gt; does not &lt;strong&gt;warn&lt;/strong&gt; us that we have forgotten to update our &lt;strong&gt;colorToString&lt;/strong&gt; function as well. Thus, we might forget to handle this newly added color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EAFrLMQP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AZQIKPtXwzsqyzc6e-hL48w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EAFrLMQP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AZQIKPtXwzsqyzc6e-hL48w.png" alt="No warning from the compiler. Even though we have added a new color." title="No warning from the compiler. Even though we have added a new color." width="519" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution?
&lt;/h2&gt;

&lt;p&gt;Use the &lt;strong&gt;never&lt;/strong&gt; keyword in &lt;strong&gt;TypeScript!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can assign the &lt;strong&gt;color&lt;/strong&gt; to a type of &lt;strong&gt;never&lt;/strong&gt; in &lt;strong&gt;TypeScript.&lt;/strong&gt; This tells the compiler to warn us if we forget to update a function that &lt;strong&gt;Exhaustive Matches.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pPJJ4ytu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2Am7fMCSm0HMvYlyRclRSmOw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pPJJ4ytu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2Am7fMCSm0HMvYlyRclRSmOw.png" alt="Compiler warning when we forget to update our Exhaustive Matching function" title="Compiler warning when we forget to update our Exhaustive Matching function" width="540" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Contrast this to when we handle our function appropriately.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kLQQhevk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AILw8QxLjCqG3JSiHiH95tQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kLQQhevk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AILw8QxLjCqG3JSiHiH95tQ.png" alt="No warnings from the compiler when we handled all our enums" title="No warnings from the compiler when we handled all our enums" width="535" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope that from these simple examples, you have learned a new tool for you! This is an especially important skill to use if you are in a large project and there are multiple such functions scattered around in the project!&lt;/p&gt;




&lt;h2&gt;
  
  
  A Step Further
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can abstract out the &lt;strong&gt;assert never&lt;/strong&gt; into a &lt;strong&gt;function&lt;/strong&gt; for &lt;strong&gt;reusability&lt;/strong&gt; throughout your codebase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vVimoC6s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AZqWmlUHGfFfipeQ1FCii0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vVimoC6s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AZqWmlUHGfFfipeQ1FCii0g.png" alt="Using a function to assert never for reusability" title="Using a function to assert never for reusability" width="582" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Besides coming in as a form of a &lt;strong&gt;switch/case statement,&lt;/strong&gt; &lt;strong&gt;Exhaustive Matching&lt;/strong&gt; functions can be in a form of &lt;strong&gt;if/else statements&lt;/strong&gt; or &lt;strong&gt;if/return statements.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qGLbD8_U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2ARRuVg1tuEapoyVDpp8GP1Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qGLbD8_U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2ARRuVg1tuEapoyVDpp8GP1Q.png" alt="Other forms of Exhaustive Matching functions" title="Other forms of Exhaustive Matching functions" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can play with the examples shown, in this &lt;a href="https://www.typescriptlang.org/play?#code/KYOwrgtgBAwg8gGTgJQMpQN4Cgq6sgUQBEAaKAIQQFUCyBxQggOTIE0CEkB1LAXyywBjAPYgAzgBcoIgDbCATgBVhqCfICWIAOZQAvFAAUshQC5YiFKgCUegHyYceMQHd1EwQAtDx+Tex4A6QBDMWBzJDQAOkIiE0dAwPlgCTB5ECgAIiSAEwyAbnjAwRCw+AjUSMoaOISEpJS0zIAjGTBgfMKA4tDwy0iGAmYa2oD61PSMrSTQDpHs4AAzILAZCWGR3AB6TelRSSgQ0PkJJmAAN2B5MxBzy71duXkCjbxDy5Pb+SNhR6tnl6gY0aGRAwgeClmeH4-AWYBAggk6lEBzERw+Fy+AA8TDcMVYMPwBCJxFIfMoAGLyMBue7fR5mMqWGy6ez+PDqBbeH4KPS6fSMqIxPydPBAiZBAAOEpk7X+uF4UGAMh6HK5j15-IsUSqBGFIzFzVawCal3kAE9IfLFcqwqq6Ty+Zryv1GEw9bUDRkJcAgvJLVAFUqemyEm9jqcMfbfHLEslxplQVAFlS3GJ-dChHtSdylMIABLATG0nwMrXWOwOAJ2nwa3qC4juupx4EAYnJ5IADF2O+nCtWc7WBRUdY3Yw0Ji3ux3272q5yo4Oyy7Bm7Kx7mxOu+2u7PXqj3hHLlG-oVPZOpz2CoTM+IfsBInItFHlKoNNoDEPog2-lBtlBH2IZg5EAA"&gt;TypeScript Playground.&lt;/a&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;a href="https://www.typescriptlang.org/play?#code/KYOwrgtgBAwg8gGTgJQMpQN4Cgq6sgUQBEAaKAIQQFUCyBxQggOTIE0CEkB1LAXyywBjAPYgAzgBcoIgDbCATgBVhqCfICWIAOZQAvFAAUshQC5YiFKgCUegHyYceMQHd1EwQAtDx+Tex4A6QBDMWBzJDQAOkIiE0dAwPlgCTB5ECgAIiSAEwyAbnjAwRCw+AjUSMoaOISEpJS0zIAjGTBgfMKA4tDwy0iGAmYa2oD61PSMrSTQDpHs4AAzILAZCWGR3AB6TelRSSgQ0PkJJmAAN2B5MxBzy71duXkCjbxDy5Pb+SNhR6tnl6gY0aGRAwgeClmeH4-AWYBAggk6lEBzERw+Fy+AA8TDcMVYMPwBCJxFIfMoAGLyMBue7fR5mMqWGy6ez+PDqBbeH4KPS6fSMqIxPydPBAiZBAAOEpk7X+uF4UGAMh6HK5j15-IsUSqBGFIzFzVawCal3kAE9IfLFcqwqq6Ty+Zryv1GEw9bUDRkJcAgvJLVAFUqemyEm9jqcMfbfHLEslxplQVAFlS3GJ-dChHtSdylMIABLATG0nwMrXWOwOAJ2nwa3qC4juupx4EAYnJ5IADF2O+nCtWc7WBRUdY3Yw0Ji3ux3272q5yo4Oyy7Bm7Kx7mxOu+2u7PXqj3hHLlG-oVPZOpz2CoTM+IfsBInItFHlKoNNoDEPog2-lBtlBH2IZg5EAA" rel="noopener noreferrer"&gt;
      typescriptlang.org
    &lt;/a&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you felt that you have learned something new, do feel free to &lt;strong&gt;follow&lt;/strong&gt; 🥰, drop a &lt;strong&gt;react 💖&lt;/strong&gt; or write a &lt;strong&gt;comment&lt;/strong&gt; ✍️!&lt;br&gt;
It helps makes me create and share more valuable content for &lt;strong&gt;you&lt;/strong&gt; to become a better &lt;strong&gt;Web Developer!&lt;/strong&gt; 🤗&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>3 Ways To Enforce Object Immutability In JavaScript</title>
      <dc:creator>Ambrose Liew</dc:creator>
      <pubDate>Mon, 24 Apr 2023 13:19:51 +0000</pubDate>
      <link>https://dev.to/morninglit/3-ways-to-enforce-object-immutability-in-javascript-289k</link>
      <guid>https://dev.to/morninglit/3-ways-to-enforce-object-immutability-in-javascript-289k</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;What happens when an Unstoppable Force meets an Immutable Object?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yaDekBGp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xofmd8hacmlqfvkh2wzu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yaDekBGp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xofmd8hacmlqfvkh2wzu.png" alt="An Immutable Object" title="An Immutable Object" width="720" height="720"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Immutability
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;JavaScript,&lt;/strong&gt; &lt;strong&gt;Object Immutability&lt;/strong&gt; means that once you have made an object &lt;strong&gt;immutable,&lt;/strong&gt; its &lt;strong&gt;properties&lt;/strong&gt; cannot be &lt;strong&gt;added,&lt;/strong&gt; &lt;strong&gt;deleted,&lt;/strong&gt; or &lt;strong&gt;modified&lt;/strong&gt; at any time.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;important&lt;/strong&gt; because it helps &lt;strong&gt;prevent unwanted side effects&lt;/strong&gt; from occurring, making your code &lt;strong&gt;more predictable&lt;/strong&gt; and &lt;strong&gt;easier to reason about.&lt;/strong&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Here are &lt;strong&gt;3 ways&lt;/strong&gt; you can make an &lt;strong&gt;object immutable&lt;/strong&gt; in &lt;strong&gt;JavaScript,&lt;/strong&gt; ordered by the level of &lt;strong&gt;integrity&lt;/strong&gt; they retain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Object.preventExtensions()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Object.preventExtensions()&lt;/code&gt; will &lt;strong&gt;prevent new properties&lt;/strong&gt; from being &lt;strong&gt;added&lt;/strong&gt; to an &lt;strong&gt;object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b76VDPA7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6wawim5vaob04ss8bio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b76VDPA7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6wawim5vaob04ss8bio.png" alt="Demo of Object.preventExtensions()" title="Demo of Object.preventExtensions()" width="555" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, it still allows the &lt;strong&gt;deletion of properties&lt;/strong&gt; and &lt;strong&gt;modification of values&lt;/strong&gt; from an &lt;strong&gt;object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can check whether an &lt;strong&gt;object&lt;/strong&gt; is &lt;strong&gt;extensible&lt;/strong&gt; by using &lt;code&gt;Object.isExtensible()&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Object.seal()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Object.seal()&lt;/code&gt; will perform &lt;code&gt;Object.preventExtensions()&lt;/code&gt; on it and also &lt;strong&gt;prevent&lt;/strong&gt; the &lt;strong&gt;deletion of properties.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aXcPakn3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzef0plod6rtdlc0ig9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aXcPakn3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzef0plod6rtdlc0ig9m.png" alt="Demo of Object.seal()" title="Demo of Object.seal()" width="560" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, it still allows the &lt;strong&gt;modification of values&lt;/strong&gt; of an &lt;strong&gt;object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can check whether an &lt;strong&gt;object&lt;/strong&gt; is &lt;strong&gt;sealed&lt;/strong&gt; by using &lt;code&gt;Object.isSealed()&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Object.freeze()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Object.freeze()&lt;/code&gt; will perform &lt;code&gt;Object.seal()&lt;/code&gt; on it and also &lt;strong&gt;prevent&lt;/strong&gt; the &lt;strong&gt;modification of values&lt;/strong&gt; of an &lt;strong&gt;object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_y1h81q5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef5x4kp3oqgcum5chxwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_y1h81q5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef5x4kp3oqgcum5chxwl.png" alt="Demo of Object.freeze()" title="Demo of Object.freeze()" width="560" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check whether an &lt;strong&gt;object&lt;/strong&gt; is &lt;strong&gt;frozen&lt;/strong&gt; by using &lt;code&gt;Object.isFrozen()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Object.freeze()&lt;/code&gt; is the &lt;strong&gt;highest&lt;/strong&gt; level of &lt;strong&gt;integrity&lt;/strong&gt; that &lt;strong&gt;JavaScript&lt;/strong&gt; offers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extras
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;All of the above methods are applied &lt;strong&gt;shallowly&lt;/strong&gt; on the &lt;strong&gt;object.&lt;/strong&gt; This means that if you have an &lt;strong&gt;object&lt;/strong&gt; within an &lt;strong&gt;object,&lt;/strong&gt; the &lt;strong&gt;inner object&lt;/strong&gt; would still be &lt;strong&gt;mutable.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;To perform a &lt;strong&gt;deep operation&lt;/strong&gt; instead of a &lt;strong&gt;shallow&lt;/strong&gt; one, you have to implement your own &lt;strong&gt;recursive&lt;/strong&gt; function. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze"&gt;MDN&lt;/a&gt; has provided a working example.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1omq1apd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l11vo49x8te6mlgsug2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1omq1apd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l11vo49x8te6mlgsug2.png" alt="MDN’s recursive deep freeze example" title="MDN’s recursive deep freeze example" width="604" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can play with the examples above in this &lt;a href="https://jsfiddle.net/MorningLit/ng2w71t9/"&gt;JSFiddle.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;iframe src="https://jsfiddle.net/MorningLit/ng2w71t9//embedded//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;

&lt;/h2&gt;

&lt;p&gt;If you felt that you have learned something new, do feel free to &lt;strong&gt;follow&lt;/strong&gt; 🥰, &lt;strong&gt;react&lt;/strong&gt; 💖 or write a &lt;strong&gt;comment&lt;/strong&gt; ✍️!&lt;br&gt;
It helps makes me create and share more valuable content for &lt;strong&gt;you&lt;/strong&gt; to become a better &lt;strong&gt;Web Developer!&lt;/strong&gt; 🤗&lt;/p&gt;

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