<?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: Tanwa Sripan</title>
    <description>The latest articles on DEV Community by Tanwa Sripan (@justtanwa).</description>
    <link>https://dev.to/justtanwa</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%2F838909%2Fb071f7f5-45e9-49f1-9103-05bcb70e7864.jpeg</url>
      <title>DEV Community: Tanwa Sripan</title>
      <link>https://dev.to/justtanwa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justtanwa"/>
    <language>en</language>
    <item>
      <title>Introduction to XML</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Sun, 07 Aug 2022 19:00:53 +0000</pubDate>
      <link>https://dev.to/justtanwa/introduction-to-xml-1h62</link>
      <guid>https://dev.to/justtanwa/introduction-to-xml-1h62</guid>
      <description>&lt;p&gt;Hello, hello! 😮‍💨 How the month has flown by! I have not had the time to write anything but that does not mean I have not been learning. While it is fun to learn the newest and latest technologies, you often will have to learn about older technologies as well (especially for jobs).&lt;/p&gt;

&lt;p&gt;So, I recently learnt about Extensible Markup Language or XML. I want to write a short introduction about it so I can use it as a reference for myself later on, and of course for anyone who might need to learn about XML 😀.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is XML?
&lt;/h2&gt;

&lt;p&gt;XML is simple language to store, describe and transport information/data across the network as plain text. It is designed so that the language is human readable with self-describing tags. If you think of JSON as a way of transferring data as plain text in the form of JavaScript Objects, then you can say that XML is a way of transferring data as plain text in a form &lt;em&gt;similar&lt;/em&gt; to HTML.&lt;/p&gt;

&lt;p&gt;XML is a mark up language like HTML, so it bear a resemblance as it is made up of hierarchical tags which store information. However, unlike HTML, it does not have predefined tags so you can create your own tags however you like to store and structure your data as you see fit. It is said that XML separate information from presentation so XML and HTML complements each other.&lt;/p&gt;

&lt;p&gt;It can be used by all sorts of programs and software as a method of transferring data, just like how JSON can be used in all languages.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does it work?
&lt;/h3&gt;

&lt;p&gt;XML documents often start with XML declaration or a prolog (this is optional), which contains meta-data of the document such as encoding.&lt;/p&gt;

&lt;p&gt;It typically look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When creating your own XML tags, keep these points (syntax rules)  in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All elements must have a &lt;em&gt;closing tag&lt;/em&gt; or if it is an empty element you can use the short-form e.g. &lt;code&gt;&amp;lt;emptyTag /&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Attributes must be quoted e.g. &lt;code&gt;&amp;lt;movie genre="comedy" /&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Avoid using characters like &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;amp;&lt;/code&gt; and use character entities instead e.g. &lt;code&gt;&amp;amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;amp;gt&lt;/code&gt;; and &lt;code&gt;&amp;amp;amp;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;There must only be one &lt;strong&gt;root&lt;/strong&gt; element or one &lt;strong&gt;parent&lt;/strong&gt; element that contains all other elements.&lt;/li&gt;
&lt;li&gt;Tags are case sensitive.&lt;/li&gt;
&lt;li&gt;White spaces are preserved (More of a heads up than a rule).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following the above rules will ensure that your XML document is &lt;em&gt;well-formed&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;XML documents, like HTML, follows Document Object Model (DOM) definition, it is a tree-structure with one root node which is the parent to all other elements. This means that you can access, update and manipulate XML documents with programs and scripts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;As usual, the best way to learn is to practice, so open up your favourite code editor and try to create your own XML file with some of you own tags. I will use VSCode. Lets say I wanted to send a list of movies of the year 2020 information, then I could write something like the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;movieList&lt;/span&gt; &lt;span class="na"&gt;year=&lt;/span&gt;&lt;span class="s"&gt;"2020"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;movie&lt;/span&gt; &lt;span class="na"&gt;genre=&lt;/span&gt;&lt;span class="s"&gt;"action"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;The Old Guard&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;rating&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;imdb&amp;gt;&lt;/span&gt;6.6&lt;span class="nt"&gt;&amp;lt;/imdb&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;rottenTomatoes&amp;gt;&lt;/span&gt;80%&lt;span class="nt"&gt;&amp;lt;/rottenTomatoes&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/rating&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/movie&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;movie&lt;/span&gt; &lt;span class="na"&gt;genre=&lt;/span&gt;&lt;span class="s"&gt;"action"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Birds of Prey&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;rating&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;imdb&amp;gt;&lt;/span&gt;6.0&lt;span class="nt"&gt;&amp;lt;/imdb&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;rottenTomatoes&amp;gt;&lt;/span&gt;79%&lt;span class="nt"&gt;&amp;lt;/rottenTomatoes&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/rating&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/movie&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;movie&lt;/span&gt; &lt;span class="na"&gt;genre=&lt;/span&gt;&lt;span class="s"&gt;"comedy"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;My Spy&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;rating&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;imdb&amp;gt;&lt;/span&gt;6.3&lt;span class="nt"&gt;&amp;lt;/imdb&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;rottenTomatoes&amp;gt;&lt;/span&gt;48%&lt;span class="nt"&gt;&amp;lt;/rottenTomatoes&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/rating&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/movie&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;movie&lt;/span&gt; &lt;span class="na"&gt;genre=&lt;/span&gt;&lt;span class="s"&gt;"horror"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;A Quiet Place Part II&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;rating&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;imdb&amp;gt;&lt;/span&gt;7.2&lt;span class="nt"&gt;&amp;lt;/imdb&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;rottenTomatoes&amp;gt;&lt;/span&gt;91%&lt;span class="nt"&gt;&amp;lt;/rottenTomatoes&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/rating&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/movie&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/movieList&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you open up the file you have created on a browser, it should display the XML document as a tree-structure with collapsible arrow for nested elements (I ran into a problem with FireFox, but works fine on Edge and GC).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DQAOwAXQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksodflgansdjcpbkz4s9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DQAOwAXQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksodflgansdjcpbkz4s9.png" alt="Example of XML file on Edge" width="880" height="604"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;XML is a flexible and extensible mark up language similar to HTML but differ in the way that it is used as a standard for transferring or exchanging data over the network. XML is written with non-predetermined tags, this means you can create your own tags and structure your information/data how you will as long as it conforms to the rules of a &lt;em&gt;well-formed&lt;/em&gt; XML document. &lt;/p&gt;

&lt;p&gt;This has been a very short overview of XML. There are must more about XML that I have to learn and I hope to write more about this topic in the future, for now thank you for reading 😃.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RoexUmRi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/originals/68/7c/00/687c0047187a126e1e147cd1f7d8edc3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RoexUmRi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/originals/68/7c/00/687c0047187a126e1e147cd1f7d8edc3.jpg" alt="Mayuri from Steins Gate" width="640" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>xml</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>CSS - Border Radius</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Sun, 10 Jul 2022 21:08:14 +0000</pubDate>
      <link>https://dev.to/justtanwa/css-border-radius-2hn3</link>
      <guid>https://dev.to/justtanwa/css-border-radius-2hn3</guid>
      <description>&lt;p&gt;👋 It's a me! Ma-... Just kidding, Mario is a plumber, not a Software Developer... right? &lt;/p&gt;

&lt;h2&gt;
  
  
  Topic of the day
&lt;/h2&gt;

&lt;p&gt;Anyway, today I want to talk about a very popular CSS property -&amp;gt; &lt;em&gt;border-radius&lt;/em&gt;. This property allows you to create awesome looking elements with nice curved edges/corners. You can use it to create nice circular border for profile images or buttons, for example. &lt;/p&gt;

&lt;p&gt;The typical usage may be using one argument as a percentage or other units to round the corners, as below:&lt;/p&gt;

&lt;h3&gt;
  
  
  One value
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;border-radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;25&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;border-radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="o"&gt;%;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Foyq2ddkkrkwltnqpjkee.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%2Foyq2ddkkrkwltnqpjkee.png" alt="Border Radius one value"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More than one argument
&lt;/h2&gt;

&lt;p&gt;But recently, I learnt that you could have more control over the individual corners with more arguments. This can result in some interesting shapes! &lt;/p&gt;

&lt;p&gt;Lets take a look at how providing different numbers of arguments affect the roundness of each corners. But first, take a look at the below image, it shows the order the values applies to the corners. It starts from TOP LEFT moving clockwise to BOTTOM LEFT.&lt;/p&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%2Fpbkd8jwvvwft7l6k2vop.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%2Fpbkd8jwvvwft7l6k2vop.png" alt="Border Radius Corner order"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Two values
&lt;/h3&gt;

&lt;p&gt;When you use two values, the first represents the TOP LEFT and BOTTOM RIGHT and the second represents the TOP RIGHT and BOTTOM LEFT (like what I color-coded in the above picture).&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="nt"&gt;border-radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;20&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F6ez9c7bwo2tbrvzul8xz.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%2F6ez9c7bwo2tbrvzul8xz.png" alt="Border Radius Two values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Three values
&lt;/h3&gt;

&lt;p&gt;When you use three values, the first represents... TOP LEFT (remember it always starts there), the second represents the TOP RIGHT and BOTTOM LEFT (they are a pair) and the third represents the BOTTOM RIGHT.&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="nt"&gt;border-radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;20&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fvi8ogery8jsi4zcisdzy.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%2Fvi8ogery8jsi4zcisdzy.png" alt="Border Radius Three Values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Four values
&lt;/h3&gt;

&lt;p&gt;You might be able to guess which four values represents which corners... The first value represents the TOP LEFT and it moves clockwise.&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="nt"&gt;border-radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;10&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;30&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;80&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Furha7zc8pz3twub5wgyf.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%2Furha7zc8pz3twub5wgyf.png" alt="Border radius four values"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This concludes the basic options that you can use to control the roundness of each corners. But wait...there's more! 😮&lt;/p&gt;

&lt;h2&gt;
  
  
  More than four values! 8 Values
&lt;/h2&gt;

&lt;p&gt;🤯 Not only can you control each corners individually, if you use 8 values (4 / 4) you can control the horizontal and vertical axes (the semi-major and the semi-minor axes) of each ellipse (circle is a special kind of ellipse) that creates the border radius. This can get a little complicated, but the maths nerd in me found it super fascinating and I spent some time playing (graphing) with the ellipse equation and CSS border-radius to learn more, it was fun.&lt;/p&gt;

&lt;p&gt;Lets start simple, any values on the left of the slash represents the horizontal axis of the ellipse and the corresponding value on the right of the slash represents the vertical axis.&lt;/p&gt;

&lt;p&gt;Note: Remember one value applies to all corners, so we are setting here, all horizontal axis of the ellipse to be &lt;code&gt;100px&lt;/code&gt; and the vertical to be &lt;code&gt;20px&lt;/code&gt;.&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="nt"&gt;border-radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="err"&gt;20&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Ff16uro9oihbpb0impue9.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%2Ff16uro9oihbpb0impue9.png" alt="Border Radius with slash"&gt;&lt;/a&gt;&lt;/p&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%2Fwwnjh5fpqx3t3d97jtix.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%2Fwwnjh5fpqx3t3d97jtix.png" alt="Show ellipse"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that if we provide different values for the horizontal and the vertical, the "roundness" of our corner looks a little off, this is due to the ellipse. As mentioned above, a circle is a special type of ellipse, where both the major and minor axes have the same length, this means that if we set the horizontal and vertical values to be the same, we will get a nice circular border radius.&lt;/p&gt;

&lt;p&gt;Note: Here, I used the value 0 for the 3rd and 4th corners so we can see a clear comparison. You will notice that if you leave out the right-hand side values of the slash, it will take the same value as the left.&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="nt"&gt;border-radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;0&lt;/span&gt; &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="err"&gt;20&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;50&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fbdz2ltxu3c3k5cai21gk.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%2Fbdz2ltxu3c3k5cai21gk.png" alt="Compare ellipse and circle"&gt;&lt;/a&gt;&lt;/p&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%2Ffivxznb53tyv2imuc6bh.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%2Ffivxznb53tyv2imuc6bh.png" alt="Compare ellipse and circle"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;With this, we can fully control how we would like each corner to look like, whether it be fully nice and round or bent out of shape. You can end up with cool blob-like shapes, see below:&lt;/p&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%2Fdxkwso98kc4ds29z1s4i.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%2Fdxkwso98kc4ds29z1s4i.png" alt="Blob"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn more about it, I created a project &lt;a href="https://borderradiusplayground.netlify.app/" rel="noopener noreferrer"&gt;Border Radius Playground&lt;/a&gt; which was inspired by the &lt;a href="https://9elements.github.io/fancy-border-radius/full-control.html" rel="noopener noreferrer"&gt;8 point full control&lt;/a&gt;. I wanted to see how using &lt;em&gt;px&lt;/em&gt; would affect the end shape, especially if the values of each point can be much bigger than the size of the container. Please check out "8 Point Full Control" if you want to read their documentation on border-radius and use this tool to create percentage border-radius for your next fancy border!&lt;/p&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%2Fcdn.myanimelist.net%2Fr%2F360x360%2Fimages%2Fcharacters%2F4%2F450832.jpg%3Fs%3Db15c221776f7b411ad13664ec41932c8" 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%2Fcdn.myanimelist.net%2Fr%2F360x360%2Fimages%2Fcharacters%2F4%2F450832.jpg%3Fs%3Db15c221776f7b411ad13664ec41932c8" alt="Najime from Komi"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
      <category>css</category>
    </item>
    <item>
      <title>What is your favourite ES6 feature?</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Wed, 29 Jun 2022 21:17:42 +0000</pubDate>
      <link>https://dev.to/justtanwa/what-is-your-favourite-es6-feature-2i3f</link>
      <guid>https://dev.to/justtanwa/what-is-your-favourite-es6-feature-2i3f</guid>
      <description>&lt;p&gt;Helloooo everyone! 👋 It has been a while since I last posted, I have been rather busy the past 2 weeks!&lt;/p&gt;

&lt;p&gt;As the title suggests, I am curious to know what your favourite JavaScript ES6 feature is...🤔 &lt;/p&gt;

&lt;p&gt;I was recently asked this in an interview and it surprised me as I have been learning JS with latest features so I don't quite know which features are ES6 and which are newer... So I answered with what came to mind, and it was &lt;code&gt;Promises&lt;/code&gt;. Since I have been learning about backend development with NodeJS, I have learnt a lot about asynchronous programming which is why I chose that answer (I also talked about async/await, which was not introduced until ES7, oops!). Of course, I had to explain why I chose that particular feature and I talked about the benefit of how it helps eliminate &lt;em&gt;callback hell&lt;/em&gt;, improve error handling and how you can use Promise chaining to your advantage.&lt;/p&gt;

&lt;p&gt;So, I present to you the same question, what is/are your favourite ES6 feature/s and why? (If you are preparing for an interview, this could be a good practice to answer out loud 😉).&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>discuss</category>
      <category>career</category>
      <category>interview</category>
    </item>
    <item>
      <title>JavaScript - Destructuring assignment</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Sat, 18 Jun 2022 15:36:54 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-destructuring-assignment-4pfp</link>
      <guid>https://dev.to/justtanwa/javascript-destructuring-assignment-4pfp</guid>
      <description>&lt;p&gt;Hey all! 👋 JavaScript ES6 has an awesome feature called destructuring assignment. Let's talk about it 😀.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction - What is destructuring assignment?
&lt;/h2&gt;

&lt;p&gt;Destructing assignment allow you assign array or object properties to a variable in an easy way. This means that you can write less code in order to unpack (grab things from) arrays and objects and assign it to individual variable. The code can look cleaner and it improves readability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Array destructuring example:
&lt;/h3&gt;

&lt;p&gt;The properties of the arrays are the index, and the syntax resemble an array literal. You can assign a value within the array corresponding to its position in the array and skip values by leaving a blank space (see example below).&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="c1"&gt;// without destructuring&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;monday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tuesday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wednesday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;thursday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;friday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;saturday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sunday&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;days&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TGIF&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;days&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;restDay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;days&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&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="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TGIF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;restDay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "monday" "friday" "sunday"&lt;/span&gt;

&lt;span class="c1"&gt;// with destructuring&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstDay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fifthDay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sunday&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;days&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="nx"&gt;firstDay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fifthDay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sunday&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "monday" "friday" "sunday"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;It is also possible to assign the remaining values using the &lt;code&gt;...&lt;/code&gt; spread syntax when destructuring. You might commonly see it as &lt;code&gt;...rest&lt;/code&gt; this basically means to store the remaining elements to the variable called "rest".&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;secondDay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;remainDays&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;days&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="nx"&gt;remainDays&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//["wednesday","thursday","friday","saturday","sunday"]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Object destructuring example:
&lt;/h3&gt;

&lt;p&gt;Similar to array destructuring, the syntax of object destructuring resemble the object literal and it allows you to assign the properties of an object to a variable. The left hand-side represents the property/variable names and the right hand-side of the equal sign is the object which you would like to unpack. &lt;/p&gt;

&lt;p&gt;If you intend to assign a property to a same name variable, you can simply write &lt;code&gt;{ propertyName }&lt;/code&gt; (as seen in example 1 below) or if you wish to rename you can simply write &lt;code&gt;{ propertyName: desiredVariableName }&lt;/code&gt; (as seen in example 2 below). You can also use nested destructuring for more complex objects.&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="c1"&gt;// Example 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Leanne Graham&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;username&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bret&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sincere@april.biz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;address&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;street&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kulas Light&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;suite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apt. 556&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;city&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Gwenborough&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;phone&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1-770-736-8031 x56442&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;website&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hildegard.org&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;phone&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&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;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; has email &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and reachable on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;phone&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "Leanne Graham has email Sincere@april.biz and reachable on 1-770-736-8031 x56442"&lt;/span&gt;

&lt;span class="c1"&gt;// Example 2&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrestler&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;John Cena&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;moves&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tornado DDT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Side Slam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Five Knuckle Shuffle&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="kd"&gt;const&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="nx"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;moves&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[,,&lt;/span&gt;&lt;span class="nx"&gt;bestMove&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;wrestler&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="nx"&gt;FullName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; best move is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;bestMove&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "John Cena best move is Five Knuckle Shuffle"&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Default values
&lt;/h3&gt;

&lt;p&gt;You can also set a default value for the cases where the property you are trying to extract does not exist (this would typically return undefined).&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&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="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="nx"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 3.14&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;codename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Twilight&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="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="s2"&gt;Loid Forger&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;profession&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Psychiatrist&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;codename&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "Twilight"&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  My recent usage
&lt;/h2&gt;

&lt;p&gt;This is very helpful when you wish to extract only specific property from an object or array. As I work with React, the typically usage for object destructuring is when importing from React library e.g. &lt;code&gt;import React, { useState, useEffect } from 'react'&lt;/code&gt;, destructuring props passed down the components or destructuring objects returned from API calls. I have also recently used array destructuring to easily swap two elements in an array to solve DSA problems:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;grid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;empty&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fulled&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="nx"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;grid&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;grid&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="nx"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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="nx"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["fulled","empty"]&lt;/span&gt;

&lt;span class="c1"&gt;// without array destructuring&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newGrid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newGrid&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="nx"&gt;newGrid&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&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="nx"&gt;newGrid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["two", "one"]&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;p&gt;Destructuring assignment is a useful syntax that allows you to write cleaner code to assign properties of array and objects to individual variables using similar syntax to the array or object literals. Give it a try 😀.&lt;/p&gt;

&lt;p&gt;For more detailed information and example, you can find it here &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading, if you have anything to add please let me know.&lt;/p&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%2Fi0.wp.com%2Fnomisec.com%2Fwp-content%2Fuploads%2F2022%2F05%2FSpy-x-Family-Highlights-Loids-Handsomeness-in-New-Cover-Art.jpg" 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%2Fi0.wp.com%2Fnomisec.com%2Fwp-content%2Fuploads%2F2022%2F05%2FSpy-x-Family-Highlights-Loids-Handsomeness-in-New-Cover-Art.jpg" alt="Loid Forger rom Spy Family"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript - Hoisting</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Sat, 11 Jun 2022 15:13:48 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-hoisting-24if</link>
      <guid>https://dev.to/justtanwa/javascript-hoisting-24if</guid>
      <description>&lt;p&gt;Hello everyone! 👋 Let's talk about hoisting in JS. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Hoisting in JavaScript?
&lt;/h2&gt;

&lt;p&gt;The term &lt;em&gt;hoist&lt;/em&gt; means to "raise (something)" and in JS it is used to describe the behaviour of JS interpreter whereby the use of classes, functions and variables before their declaration does not cause the error: &lt;code&gt;Uncaught ReferenceError: X is not defined&lt;/code&gt;. This is because &lt;em&gt;hoisting&lt;/em&gt; &lt;strong&gt;appears&lt;/strong&gt; to have moved the declaration up to the top. (The variables and functions are actually saved in memory during the creation phase of the Execution Context, you can read more about it &lt;a href="https://www.freecodecamp.org/news/execution-context-how-javascript-works-behind-the-scenes/"&gt;here&lt;/a&gt;.) &lt;/p&gt;

&lt;p&gt;Below, I will talk about the difference between variable hoisting and function hoisting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable Hoisting
&lt;/h3&gt;

&lt;p&gt;When you declare a variable, you can use &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;. Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; get &lt;em&gt;hoisted&lt;/em&gt; but do not get initialized with a default value, this means that you can still get &lt;code&gt;ReferenceError&lt;/code&gt; if you use the variables before its declaration. However, for &lt;code&gt;var&lt;/code&gt;, when it gets &lt;em&gt;hoisted&lt;/em&gt; it also gets initialized with a default value of &lt;code&gt;undefined&lt;/code&gt; so you will not get an error.&lt;/p&gt;

&lt;p&gt;Example:&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;varThing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;varThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;testingVar&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;letThing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;letThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;testingLet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught ReferenceError: Cannot access 'letThing' before initialization&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Hoisting
&lt;/h3&gt;

&lt;p&gt;When a function gets &lt;em&gt;hoisted&lt;/em&gt; this means that you can call/use the function safely without error, this only applies to function created with &lt;code&gt;function&lt;/code&gt; keyword and not function expressions.&lt;/p&gt;

&lt;p&gt;Example:&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getRandomNumber&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;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getRandomNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;max&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&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="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;min&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getRandom&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught TypeError: getRandom is not a function &lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;getRandom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&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;In the example, you can see that the &lt;code&gt;getRandomNumber&lt;/code&gt; function can be called before its declaration, this is due to &lt;em&gt;hoisting&lt;/em&gt;. However, with &lt;code&gt;getRandom&lt;/code&gt;, it has been hoisted as a variable, specifically with value &lt;code&gt;undefined&lt;/code&gt; and so you get a &lt;code&gt;TypeError&lt;/code&gt; when you try to use it as a function.&lt;/p&gt;

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

&lt;p&gt;&lt;em&gt;Hoisting&lt;/em&gt; is the term used to describe the process in which declarations of classes, functions and variables get saved in memory during the Execution Context. This allows for the program to read (use) these values before declaring them. It can cause some problems so it is good to know what kind of error it can cause but also why certain things work even before you declare them. &lt;/p&gt;

&lt;p&gt;It is always best to declare your classes, expression functions and variables before using them to avoid initialization errors.&lt;/p&gt;

&lt;p&gt;Thank you for reading, leave a comment if you found it helpful or have any feedback. 😀&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--suZEEVaC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wikia.nocookie.net/fma/images/0/00/Alphonse_Elric_human.jpg/revision/latest%3Fcb%3D20180630022928" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--suZEEVaC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.wikia.nocookie.net/fma/images/0/00/Alphonse_Elric_human.jpg/revision/latest%3Fcb%3D20180630022928" alt="Alphone from FMA" width="499" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript - JSON</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Sat, 04 Jun 2022 16:56:49 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-json-510f</link>
      <guid>https://dev.to/justtanwa/javascript-json-510f</guid>
      <description>&lt;p&gt;Hey all 👋 I have been learning a lot about backend development and creating APIs with NodeJS, Express and MongoDB. So, I wanted to write a little bit about JavaScript Object Notation (JSON) and share what I know about them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Table of content
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Methods and Examples&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Although the name is JavaScript Object Notation, it is actually widely used in many programming languages not just JS. JSON is a way of formatting data and representing them as plain text and it is often written as key-value pairs similar to the JS Objects. It is commonly used for API as it allows for data to be transferred over network requests and it is often used in configuration files of projects (e.g &lt;code&gt;package.json&lt;/code&gt; 👀). JSON is very easy to read/write and using them is also easy.&lt;/p&gt;

&lt;p&gt;JSON represents data in plain text and it supports many data types such as &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Booleans&lt;/code&gt;, &lt;code&gt;Numbers&lt;/code&gt;, &lt;code&gt;Arrays&lt;/code&gt;, &lt;code&gt;Objects&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt;. However, it cannot represent &lt;code&gt;Functions&lt;/code&gt;, &lt;code&gt;Symbols&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;JSON is language independent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods and Examples &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The two main methods that you can use when dealing with JSON are &lt;code&gt;JSON.stringify()&lt;/code&gt; and &lt;code&gt;JSON.parse()&lt;/code&gt; in JavaScript (other languages will have their own versions of this). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSON.stringify()&lt;/strong&gt; - When you would like to convert JS Objects into a JSON string in preparation to send data over the web for example, you can use this method. If an object to be stringify has its own &lt;code&gt;toJSON&lt;/code&gt; method then &lt;code&gt;JSON.stringify&lt;/code&gt; will use that during the process, an example of this is the &lt;code&gt;Date&lt;/code&gt; object. &lt;/p&gt;

&lt;p&gt;Converting objects to JSON string is sometimes known as &lt;em&gt;serializing&lt;/em&gt; or &lt;em&gt;json-enconding&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Syntax and Example:&lt;/p&gt;

&lt;p&gt;The full syntax is &lt;code&gt;JSON.stringify(value, replacer, space)&lt;/code&gt;, where the replacer allow you to modify how you would like the value to be stringify while the "space" option allow you to define the white spaces for better visual representation of the JSON string.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;Michael Scott&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;dob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;15/03/1964&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;regional manager&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pam&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dwight&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Angela&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kevin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Stanley&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;lover&lt;/span&gt;&lt;span class="p"&gt;:&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="s2"&gt;Holly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;dob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;03/03/1972&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;date_added&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;json_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;json_data&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json_data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// '{"name":"Michael Scott","dob":"15/03/1964","position":"regional manager","employees":["Jim","Pam","Dwight","Angela","Kevin","Stanley"],"lover":{"name":"Holly","dob":"03/03/1972"},"date_added":"2022-06-04T12:33:30.479Z"}'&lt;/span&gt;

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

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;JSON.parse()&lt;/strong&gt; - This method allow you to convert JSON string to JS object. You may use this if you wanted to parse some data you received from an API request for example.&lt;/p&gt;

&lt;p&gt;Syntax and Example:&lt;/p&gt;

&lt;p&gt;The full syntax is &lt;code&gt;JSON.parse(text, reviver)&lt;/code&gt;, where the reviver allow you transform the value before returning it.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;json_encoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"name": "Jim", "position": "salesperson", "likes": ["ham", "cheese" , "Pam"] }&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json_encoded&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;data&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;likes&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="c1"&gt;// "cheese"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;JSON.parse will not automatically parse into the &lt;code&gt;Date&lt;/code&gt; object, so you can use the reviver option to convert the date string into a &lt;code&gt;Date&lt;/code&gt; object, for example:&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;json_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"date":"2022-06-04T12:33:30.479Z"}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reviver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;date&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&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="k"&gt;return&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reviver&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getFullYear&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 2022&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, I have defined the reviver function but you could use anonymous function if you wanted to, it takes the &lt;code&gt;key&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; pair of the JSON string and if the &lt;code&gt;key&lt;/code&gt; matches "date" then we would return a &lt;code&gt;Date&lt;/code&gt; object of the &lt;code&gt;value&lt;/code&gt; rather than the automatic &lt;code&gt;JSON.parse&lt;/code&gt; value.&lt;/p&gt;

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

&lt;p&gt;JSON is a data representation format that is independent of programming languages. You can use &lt;code&gt;JSON.stringify()&lt;/code&gt; and &lt;code&gt;JSON.parse()&lt;/code&gt; in JavaScript to convert to (&lt;em&gt;serialize&lt;/em&gt;) and from (&lt;em&gt;deserialize&lt;/em&gt;) JSON string/text. It can support Primitive values, null, arrays and objects. It is lightweight and easy to use, and you can add options to deal with how you convert to and from JSON.&lt;/p&gt;

&lt;p&gt;That is all for today, if you have anything to add please let me know either in the comment below or via &lt;a href="https://twitter.com/justtanwa"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ov3gKoq1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.direct8.fr/wp-content/uploads/2021/09/detective-conan.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ov3gKoq1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.direct8.fr/wp-content/uploads/2021/09/detective-conan.jpg" alt="Det. Conan Anime" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript - Conditional Operator</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Sun, 29 May 2022 11:00:35 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-conditional-operator-594f</link>
      <guid>https://dev.to/justtanwa/javascript-conditional-operator-594f</guid>
      <description>&lt;h2&gt;
  
  
  What is the Conditional Operator?
&lt;/h2&gt;

&lt;p&gt;The Conditional Operator is sometimes called the Ternary Operator or "question mark" operator. It is an operator that allows us to write the &lt;strong&gt;&lt;code&gt;if ... else&lt;/code&gt;&lt;/strong&gt; statement in a short and concise way. The operator is used with a question mark &lt;code&gt;?&lt;/code&gt; hence the name "question mark" operator. The operator has three operands hence the name "ternary" operator.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax and Example
&lt;/h3&gt;

&lt;p&gt;Normally, you would write an &lt;strong&gt;&lt;code&gt;if ... else&lt;/code&gt;&lt;/strong&gt; statement the following way:&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&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;condition&lt;/span&gt;&lt;span class="p"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&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="k"&gt;else&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the Conditional operator, you can write the above code the following way:&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="c1"&gt;// syntax -&amp;gt; condition ? operand2: operand3; &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;false&lt;/span&gt;&lt;span class="dl"&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 first operand is the condition you want to evaluate, followed by a question mark (&lt;code&gt;?&lt;/code&gt;). The second operand is the expression you wish to execute if the condition is &lt;em&gt;"truthy"&lt;/em&gt;, followed by a colon (&lt;code&gt;:&lt;/code&gt;). And lastly, the third operand is the expression to execute if the condition is &lt;em&gt;"falsy"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can see that with the Conditional operator, you can neatly write an &lt;strong&gt;&lt;code&gt;if ... else&lt;/code&gt;&lt;/strong&gt; statement block into one line and assign it directly to a variable. &lt;/p&gt;

&lt;h3&gt;
  
  
  Nesting/Chaining
&lt;/h3&gt;

&lt;p&gt;The Conditional operator is often used as an alternative to &lt;strong&gt;&lt;code&gt;if ... else&lt;/code&gt;&lt;/strong&gt; statement block because it is shorter to write, sometimes it is still better for code readability to use &lt;strong&gt;&lt;code&gt;if ... else&lt;/code&gt;&lt;/strong&gt; statements. That being said, you can use Conditional operator for &lt;code&gt;if ... else if ... else&lt;/code&gt; statements as well. &lt;/p&gt;

&lt;p&gt;For example:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getAgeGroup&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="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;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adult&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="k"&gt;else&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;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;13&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adolescent&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a function that return the age terminology based on the parameter "age", the function evaluate the input argument with &lt;code&gt;if ... else if ... else&lt;/code&gt; statement and return appropriate term.&lt;br&gt;
If we wanted to use the Ternary Operator, we could do it like so:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getAgeGroup&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adult&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adolescent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child&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;p&gt;You can see that it much shorter, as for whether it is as readable, I will let you be the judge of that 😃.&lt;/p&gt;

&lt;p&gt;(Explanation: In the example, if the first condition is "truthy" it will return the string "adult", otherwise, it will move to the second condition and again check for "truthy" and return the string "adolescent". If the second condition is "falsy" then the string "child" will be returned.)&lt;/p&gt;

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

&lt;p&gt;The Conditional Operator has three operands and can be used as an alternative to &lt;code&gt;if ... else&lt;/code&gt; statement block. It &lt;strong&gt;can&lt;/strong&gt; be difficult to read so it should be used for simple cases. An example of my use case would be in React, where you can't use &lt;code&gt;if&lt;/code&gt; condition directly inside the JSX so you could use this conditional operator for conditional rendering.&lt;/p&gt;

&lt;p&gt;Thank you for reading, please leave a comment if you found it helpful, or feedback if you found a mistake or would like to add to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xQfd2Cm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sportshub.cbsistatic.com/i/2022/05/16/3eee5cbd-0e19-48fd-a094-500cfe42c931/spy-x-family-anya-heh-anime.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xQfd2Cm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://sportshub.cbsistatic.com/i/2022/05/16/3eee5cbd-0e19-48fd-a094-500cfe42c931/spy-x-family-anya-heh-anime.jpg" alt="Anya Forger from Spy Family anime" width="880" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>NodeJS and ExpressJS - Hello World 👨🏻‍💻</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Mon, 23 May 2022 20:25:14 +0000</pubDate>
      <link>https://dev.to/justtanwa/nodejs-and-expressjs-hello-world-3eb3</link>
      <guid>https://dev.to/justtanwa/nodejs-and-expressjs-hello-world-3eb3</guid>
      <description>&lt;p&gt;Hey everyone 👋 I recently finished the freeCodeCamp "Backend Development and APIs", if you are interested you can find it here at &lt;a href="https://www.freecodecamp.org/learn/back-end-development-and-apis"&gt;FCC&lt;/a&gt;. I learnt quite a lot and I thought it was a great introductory to Backend Development with NodeJS. So, I thought why not show you all how to create the classic "Hello World" app using Express and NodeJS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of content
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Pre-requisites&lt;/li&gt;
&lt;li&gt;Server set up&lt;/li&gt;
&lt;li&gt;Hello world&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Introduction &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;With NodeJS, you can now write backend code with JavaScript. While, for some people, it might not be too difficult to learn a new language to write server-side code, I think that being able to write both frontend and backend with the same language makes learning full stack developer easier (maybe 😁). So what is NodeJS? - NodeJS is a JavaScript runtime environment that comes with many tools to help you write server-side code using JS. You can learn more about it with &lt;a href="https://nodejs.dev/learn"&gt;nodejs&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;ExpressJS is a NodeJS framework which is easy to use. It allows you to write handler functions for different HTTP requests (GET, POST, PUT etc...) for different routes (path/URL). It can also be used to serve static files and dynamically generated content using templates. You can learn more about it at &lt;a href="https://www.tutorialspoint.com/expressjs/index.htm"&gt;tutorialpoint&lt;/a&gt; and &lt;a href="https://expressjs.com/en/5x/api.html"&gt;express&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pre-requisites &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;You should: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Know some JavaScript code&lt;/li&gt;
&lt;li&gt;Have NodeJS installed&lt;/li&gt;
&lt;li&gt;Have VSCode installed (this is the editor I use, but you can use others you are comfortable with)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you don't have NodeJS yet, you can download it here at &lt;a href="https://nodejs.dev/download"&gt;nodejs&lt;/a&gt;, this will come with a package manager &lt;code&gt;npm&lt;/code&gt; which you can use to install various packages and dependencies, e.g. ExpressJS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Server set up &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;After you have installed NodeJS, you can create a new folder (call anything you want) and open it up in VSCode, and within VSCode you can open the terminal and initialize a node project with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start a new NodeJS project and create a &lt;code&gt;package.json&lt;/code&gt; file with populated information. You can then run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will add ExpressJS to the dependencies list in the &lt;code&gt;package.json&lt;/code&gt; file and install it along with other node built in modules (you will see the &lt;code&gt;node_modules&lt;/code&gt; folder appear).&lt;/p&gt;

&lt;p&gt;Next, you will need to create a JavaScript file to start coding the "Hello world" app, you can call it app.js or index.js (or whatever).&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Hello World App &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To begin, let's import ExpressJS using &lt;code&gt;require&lt;/code&gt;, as express is a function, you will need to call it and store it in a variable typically called &lt;code&gt;app&lt;/code&gt;. We can then start the server by telling &lt;code&gt;app&lt;/code&gt; (a.k.a express) to listen on a specific port.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// import express&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// initiate express&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is running on http://localhost:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 

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

&lt;/div&gt;



&lt;p&gt;The second argument to the &lt;code&gt;.listen&lt;/code&gt; method lets us define a callback function so we know when the server is up and running.&lt;/p&gt;

&lt;p&gt;We can start the server by going to the terminal and run this command (*note: my js file is called index.js, so you will need to use whatever you named your js file):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right now, you won't be able to do much because we have not set up any routes (end-points), but you can still view and check if the server is up and running. You should see the "Server is running on &lt;a href="http://localhost:8000"&gt;http://localhost:8000&lt;/a&gt;" in the console and if you visit that page on the browser you should get a message of "Cannot GET /".&lt;/p&gt;

&lt;p&gt;Next, let's set up a GET end-point to the home page or the root URL ("/") and response with a "Hello world". Here we will use &lt;code&gt;.get()&lt;/code&gt;, an Express method that tells the server how it should respond to a GET request. The first parameter tells you which route (URL/end-point) you want this to run, in our example we want the root URL ("/") and the second parameter is the handler function which has take two arguments, the request and response object. To print "Hello world", we can use &lt;code&gt;res&lt;/code&gt; object and &lt;code&gt;.send&lt;/code&gt; the message.&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&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="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world!&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;p&gt;VOILA! You have done it! If you visit the page now on the browser, you should see that the server has responded to the GET request with a message of "Hello world!" (or whatever message you used). If you don't see anything, try restarting the server by pressing Ctr + C and run &lt;code&gt;node index.js&lt;/code&gt; again.&lt;/p&gt;

&lt;p&gt;If you followed the steps, hopefully everything should work and you will have coded "Hello world" app using ExpressJS and NodeJS. You can see my example here on &lt;a href="https://replit.com/@JustTanwa/helloworld"&gt;replit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The overall project looks like this:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// import express&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// initiate express&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&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="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello world!&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is running on http://localhost:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;PORT&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;NodeJS is a JavaScript runtime environment which gives you the power to write JS code on the server side. It comes with many helpful built in tools. ExpressJS is a NodeJS framework that help you deal with HTTP requests and more, you can use these two to create a simple "Hello World" app.&lt;/p&gt;

&lt;p&gt;Thank you for reading, if you have any feedback please leave a comment or DM me on Twitter at &lt;a href="https://twitter.com/home"&gt;JustTanwa&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JmzrZ1Lx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://images5.fanpop.com/image/answers/2336000/2336919_1326288195133.27res_500_281.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JmzrZ1Lx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://images5.fanpop.com/image/answers/2336000/2336919_1326288195133.27res_500_281.jpg" alt="L from death note" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript - for...of and for...in</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Wed, 18 May 2022 18:23:29 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-forof-and-forin-3f67</link>
      <guid>https://dev.to/justtanwa/javascript-forof-and-forin-3f67</guid>
      <description>&lt;p&gt;Hello there! Bonjour! Hej!　こんいちは ！👋 Today, I want to talk about when I learnt about &lt;code&gt;for...of&lt;/code&gt; and &lt;code&gt;for...in&lt;/code&gt; in JavaScript. &lt;/p&gt;

&lt;h2&gt;
  
  
  Table of content
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;For...in&lt;/li&gt;
&lt;li&gt;For...of&lt;/li&gt;
&lt;li&gt;Summary&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Introduction &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;If you are learning JavaScript (JS), at some point you will learn about loops in order to run a block of code over and over again. Loops are handy but sometimes they can be the source of your bug/trouble when you don't handle the loop conditions properly, this is especially true with &lt;code&gt;while&lt;/code&gt; loops (I have created infinite loops many times, even more than Christopher Nolan 😋). The first loop statement I learnt in JS was a &lt;code&gt;for&lt;/code&gt; loop, it was cool to be able to get the computer to repeat a task with set conditions but I did think that it required a fair bit of set up with the three statements when setting up the loop (and many times I forget about the semi-colon...😵).&lt;/p&gt;

&lt;p&gt;Later, I started using &lt;a href="https://dev.to/justtanwa/series/17510"&gt;Array Methods&lt;/a&gt; and it was fun to use them and they were very helpful in keeping the code clean while also expanding my knowledge. However, when solving some Data Structure and Algorithm problems, I found that if you need to break out of the loop early, Array methods are not good for those situation as they run on every element in the array, so I learnt about &lt;code&gt;for...in&lt;/code&gt; and &lt;code&gt;for...of&lt;/code&gt; to use something other than a &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  For...in &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;for...in&lt;/code&gt; loop iterates over the "keys" or "properties" of a JS &lt;code&gt;Object&lt;/code&gt; and indexes of a JS &lt;code&gt;Array&lt;/code&gt;, to put it simply. However more generally, it loop over the enumerable properties of objects, these are properties which are of &lt;code&gt;String&lt;/code&gt; datatype and has an internal &lt;code&gt;enumerable&lt;/code&gt; attribute sets to true (almost everything in JS is an object).&lt;/p&gt;

&lt;p&gt;The set up looks something like this.&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;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// something you'd like to do&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to loop through an object in JS, the &lt;code&gt;for...in&lt;/code&gt; loop can be a very easy way to do that as it will iterate over all the properties of the object (including inherited ones) and then you can access the corresponding values. In the above code example, the variable "key" will represent different object properties each iteration.&lt;/p&gt;

&lt;p&gt;For example:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FFMovies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Fast and the Furious&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;second&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2 Fast 2 Furious&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;third&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Fast and the Furious: Tokyo Drift&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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;installment&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;FFMovies&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="nx"&gt;installment&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;FFMovies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;installment&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// first is The Fast and the Furious&lt;/span&gt;
&lt;span class="c1"&gt;// second is 2 Fast 2 Furious&lt;/span&gt;
&lt;span class="c1"&gt;// third is The Fast and the Furious: Tokyo Drift&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;It is recommended not to use &lt;code&gt;for...in&lt;/code&gt; loop to iterate over an Array if the order of matters, instead stick with &lt;code&gt;for&lt;/code&gt; loop or use &lt;code&gt;.forEach()&lt;/code&gt; method. This is to do with the implementation of the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  For...of &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;for...of&lt;/code&gt; loop was introduce with ES6 which allow you to iterate over an iterable object in JS e.g. &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Map&lt;/code&gt; and etc. To simply put, you can iterate over the values of an array for example. It is important to note that you cannot use &lt;code&gt;for...of&lt;/code&gt; on &lt;code&gt;Object&lt;/code&gt; directly. &lt;/p&gt;

&lt;p&gt;The set up of a &lt;code&gt;for...of&lt;/code&gt; is very similar to a &lt;code&gt;for...in&lt;/code&gt; loop except one word. So why would you use &lt;code&gt;for...of&lt;/code&gt; instead of a regular &lt;code&gt;for&lt;/code&gt; loop or a method like &lt;code&gt;.forEach()&lt;/code&gt;? I will show you.&lt;/p&gt;

&lt;p&gt;Example:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FFMovies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&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="s2"&gt;Fast &amp;amp; Furious&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;installment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4th&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;6.5&lt;/span&gt;&lt;span class="p"&gt;},&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="s2"&gt;Fast Five&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;installment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5th&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;7.3&lt;/span&gt;&lt;span class="p"&gt;},&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="s2"&gt;Fast &amp;amp; Furious 6&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;installment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;6th&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;installment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;rated&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;FFMovies&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is the &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;installment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; of the franchise, it was rated &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;rated&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; of out 10.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/*
Fast &amp;amp; Furious is the 4th of the franchise, it was rated 6.5 of out 10.
Fast Five is the 5th of the franchise, it was rated 7.3 of out 10.
Fast &amp;amp; Furious 6 is the 6th of the franchise, it was rated 7 of out 10. 
*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;for...of&lt;/code&gt; you can directly use object destructing or array destructing which allow you to "grab" each properties/element and assign them to a variable to use inside your loop, just like the example above. &lt;/p&gt;

&lt;p&gt;When dealing with matrix problem, I often use this method to grab the directions so I can perform Breadth First Search for example. &lt;/p&gt;

&lt;h3&gt;
  
  
  Summary &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;If you want to iterate through an array where the order of iteration matters, the traditional &lt;code&gt;for&lt;/code&gt; loop is still a good method to use, alternatively you could use a &lt;code&gt;.forEach&lt;/code&gt; array method. If you want to iterate over an object properties, you could use &lt;code&gt;for...in&lt;/code&gt; to iterate over the object keys/properties and do what you want. If you have a more complex Array structure, you could use &lt;code&gt;for...of&lt;/code&gt; loop combined with &lt;em&gt;Array Destructing&lt;/em&gt; or &lt;em&gt;Object Destructing&lt;/em&gt; to write cleaner code.&lt;/p&gt;

&lt;p&gt;That's it. I was aware of these two statements but I had never used them until I started solving DSA problems, and now I have this under my tool belt to use in the future 😄.&lt;/p&gt;

&lt;p&gt;As always, please leave a comment, like, feedback or whatever you want. Thank you for reading.&lt;/p&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%2Fdata.whicdn.com%2Fimages%2F354873326%2Foriginal.jpg" 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%2Fdata.whicdn.com%2Fimages%2F354873326%2Foriginal.jpg" alt="Sukuna from Jujutsu Kaisen"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript - Map</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Sat, 14 May 2022 20:15:45 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-map-ko1</link>
      <guid>https://dev.to/justtanwa/javascript-map-ko1</guid>
      <description>&lt;p&gt;Hey 👋, hope y'all are well! Lets talk about &lt;code&gt;Map&lt;/code&gt; in JavaScript!&lt;/p&gt;

&lt;h2&gt;
  
  
  Map 🗺️
&lt;/h2&gt;

&lt;p&gt;Map in JavaScript (JS) allow you to store information/data using a key-value relationship. But wait... isn't that the same as an &lt;code&gt;Object&lt;/code&gt; in JS? Map does return &lt;code&gt;Object&lt;/code&gt; when you use &lt;code&gt;typeof&lt;/code&gt; but it is not exactly the same. They are very similar in nature but they have distinct differences that determine their uses. &lt;/p&gt;

&lt;p&gt;So what are these distinct differences? Well, if you are using &lt;code&gt;Object&lt;/code&gt; the keys must be a &lt;code&gt;String&lt;/code&gt; or &lt;code&gt;Symbol&lt;/code&gt; datatype whereas the keys in &lt;code&gt;Map&lt;/code&gt; can be any datatype including &lt;code&gt;Function&lt;/code&gt; and &lt;code&gt;Object&lt;/code&gt;. Another thing is that to find the size of an &lt;code&gt;Object&lt;/code&gt; you must do it manually, e.g. grabbing all the keys using &lt;code&gt;Object.keys()&lt;/code&gt; then counting the length. But, &lt;code&gt;Map&lt;/code&gt; has a property &lt;code&gt;.size&lt;/code&gt; which return the size of the &lt;code&gt;Map&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Additionally, Objects are not directly iterable while with Maps you can. &lt;/p&gt;

&lt;p&gt;Example: &lt;code&gt;Map&lt;/code&gt; comes with its own methods and you can see some of them below.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cypher&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;Cypher&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;white&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;raze&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;Raze&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;omen&lt;/span&gt; &lt;span class="o"&gt;=&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="s2"&gt;Omen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;characterRoles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// create an empty Map.&lt;/span&gt;

&lt;span class="c1"&gt;// add new data&lt;/span&gt;

&lt;span class="nx"&gt;characterRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cypher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sentinel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// you can chain the method to add multiple entries&lt;/span&gt;
&lt;span class="nx"&gt;characterRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;raze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;duelist&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;omen&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;smokes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// check size&lt;/span&gt;
&lt;span class="nx"&gt;characterRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="c1"&gt;// iterable&lt;/span&gt;
&lt;span class="nx"&gt;characterRoles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; is a &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Cypher is a sentinel&lt;/span&gt;
&lt;span class="c1"&gt;// Raze is a duelist&lt;/span&gt;
&lt;span class="c1"&gt;// Omen is a smokes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As I continue to learn more about Data Structure and Algorithm, I learnt that &lt;code&gt;Map&lt;/code&gt; is JavaScript's equivalent to a Hashmap and have been using them to solve some problems. &lt;/p&gt;

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

&lt;p&gt;To summarise, Map allow you to store data as key-value pairs that are not limited to &lt;code&gt;String&lt;/code&gt; (or &lt;code&gt;Symbol&lt;/code&gt;) datatype as keys. It also give you the ability to directly iterate through the data. Additionally, Maps have it's own properties and methods that you can use to add, access, edit, and delete the data in the collection.&lt;/p&gt;

&lt;p&gt;Thank you for reading this short post, please leave a comment if you want to add information or give feedback 😀. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m1rhxvOI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://staticg.sportskeeda.com/editor/2022/01/cbc36-16421825565301-1920.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m1rhxvOI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://staticg.sportskeeda.com/editor/2022/01/cbc36-16421825565301-1920.jpg" alt="Anime character Itadori" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript - Set</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Tue, 10 May 2022 21:32:28 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-set-4mbf</link>
      <guid>https://dev.to/justtanwa/javascript-set-4mbf</guid>
      <description>&lt;p&gt;I am currently partaking in the #100DaysOfCode challenge, and everyday I try to solve one Data Structure and Algorithm problem using JavaScript. I have encountered Set and Map and I want to talk a little a bit about the &lt;code&gt;Set&lt;/code&gt; object type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Set&lt;/code&gt; object type was introduced with ECMAScript 2015 (or ES6) which allow you to store all kind of JavaScript data types as a unique set of elements. It is very similar to an &lt;code&gt;Array&lt;/code&gt; in JavaScript but it has other syntax that allows you to access, check, and delete elements in the &lt;code&gt;Set&lt;/code&gt;. This particular object type comes with its own methods, in particular, the &lt;code&gt;.has()&lt;/code&gt; method which allows you to check for elements inside the &lt;code&gt;Set&lt;/code&gt; much like &lt;code&gt;Array.prototype.includes&lt;/code&gt;, and according to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"&gt;MDN Docs&lt;/a&gt; it is quicker than &lt;code&gt;.includes&lt;/code&gt; on average. &lt;/p&gt;

&lt;p&gt;The methods that we can use with &lt;code&gt;Set&lt;/code&gt; object type are &lt;code&gt;.add()&lt;/code&gt;, &lt;code&gt;.clear()&lt;/code&gt;, &lt;code&gt;.delete()&lt;/code&gt;, and &lt;code&gt;.has()&lt;/code&gt;. It also has a property &lt;code&gt;.size&lt;/code&gt; which is similar to &lt;code&gt;Array.prototype.length&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example: Let's look at some of the &lt;code&gt;Set&lt;/code&gt; methods.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;twiceTwice&lt;/span&gt; &lt;span class="o"&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;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;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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;twiceTwice&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="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// { 1, 2, 3, 4, 5 };&lt;/span&gt;

&lt;span class="c1"&gt;// can add new elements, any data type&lt;/span&gt;
&lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rasengan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&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="s2"&gt;Tanwa&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;favouriteColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Red&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/* Set(7) {1, 2, 3, 4, 5, …}
[[Entries]]
0: 1
1: 2
2: 3
3: 4
4: 5
5: "Rasengan"
6:
value: {name: 'Tanwa', favouriteColor: 'Red'}
size: 7 */&lt;/span&gt;

&lt;span class="c1"&gt;// check for an element&lt;/span&gt;
&lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rasengan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// True&lt;/span&gt;

&lt;span class="c1"&gt;// delete an element&lt;/span&gt;
&lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&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="c1"&gt;// True&lt;/span&gt;

&lt;span class="c1"&gt;// check size&lt;/span&gt;
&lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;

&lt;span class="c1"&gt;// clear&lt;/span&gt;
&lt;span class="nx"&gt;uniqueNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example use case
&lt;/h3&gt;

&lt;p&gt;To solve &lt;a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/"&gt;3. Longest Substring Without Repeating Characters on leetcode&lt;/a&gt;, I used &lt;code&gt;Set&lt;/code&gt; to form and store the substring as I iterate through the string using two pointers, and each iteration, checking the Set using &lt;code&gt;.has()&lt;/code&gt; to see if the element already exist before using &lt;code&gt;.add()&lt;/code&gt; to append new values or &lt;code&gt;.delete()&lt;/code&gt; to remove the element starting from the left. You can see my solution on my &lt;a href="https://my100docjourney.netlify.app/archive/day15"&gt;website&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;Set&lt;/code&gt; is an object type that allows you to store unique elements of any type. And these are the methods: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.add()&lt;/code&gt;: you can add a new element to the end of the &lt;code&gt;Set&lt;/code&gt;. Returns the new &lt;code&gt;Set&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.clear()&lt;/code&gt;: you can remove all the elements in the &lt;code&gt;Set&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.delete()&lt;/code&gt;: you can remove a specific value from the &lt;code&gt;Set&lt;/code&gt;. Returns false if an element is not in the &lt;code&gt;Set&lt;/code&gt;. Returns true for successfully removing the element.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.has()&lt;/code&gt;: you can check if an element is in the &lt;code&gt;Set&lt;/code&gt;. Returns a &lt;code&gt;Boolean&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next time you need to remove a specific element from a unique set of values, you can try using &lt;code&gt;Set&lt;/code&gt; and its methods.&lt;/p&gt;

&lt;p&gt;Thank you for reading, I hope you found it useful. Please leave comments and feedbacks if you have them :)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hkl4v9Q_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.anime-internet.com/content/images/2021/09/Shoto-1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hkl4v9Q_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.anime-internet.com/content/images/2021/09/Shoto-1.jpg" alt="Anime character MHA Todoroki" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript - Event Listeners and Handlers</title>
      <dc:creator>Tanwa Sripan</dc:creator>
      <pubDate>Fri, 06 May 2022 18:49:39 +0000</pubDate>
      <link>https://dev.to/justtanwa/javascript-event-listeners-and-handlers-1g9</link>
      <guid>https://dev.to/justtanwa/javascript-event-listeners-and-handlers-1g9</guid>
      <description>&lt;p&gt;Hey everyone 👋 Let's talk about Event Handling in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of content
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Event Listener and Event Handler&lt;/li&gt;
&lt;li&gt;Capture and Bubbling&lt;/li&gt;
&lt;li&gt;Example&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Introduction &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When I started learning JavaScript (JS), I read somewhere that JS is an event-driven programming language, and that the use of JS in the browser was to add interactivity to the web pages. &lt;/p&gt;

&lt;p&gt;But what are events in JS? Well, events are just actions that happen when the user is accessing the web pages and it could be the loading of the document, the fetching of data, the clicking of elements inside the document, the pressing of keys or touchscreen and many more.&lt;/p&gt;

&lt;p&gt;These actions are known as events in JS and we can respond to these events by calling functions and manipulate the DOM to provide that interactivity and feedback to the user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Event Listener and Event Handler &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you have a button, and you would like to know when the user has clicked this button so that you can run a block of code. You can attach an event listener to any element you would like to "listen" for an event type by using &lt;code&gt;.addEventListener()&lt;/code&gt;. This method typically takes two parameters, the first being the event type you would like to listen out for (as a string) and the second is the callback function you would like to run when the event occurs (sometime referred to as "fires"). The function that runs when the event "fires" is sometimes called the event handler. &lt;/p&gt;

&lt;p&gt;Typically, you would grab the element from the HTML DOM by using something like &lt;code&gt;document.getElementById()&lt;/code&gt; or &lt;code&gt;document.querySelector()&lt;/code&gt;. I like to use the &lt;code&gt;.querySelector&lt;/code&gt; because it feels easier since it is the same selector as CSS.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&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="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// some code to manipulate the DOM here&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may notice that in the example, the anonymous function takes in an argument &lt;code&gt;e&lt;/code&gt;. This argument is the event object, it is an object which contains details about the event that occurred so you can capture things like the target of the event, or the coordinate of the cursor, etc.&lt;/p&gt;

&lt;p&gt;You can attach more than one event listener to an element and if they are the same type, the callback function will run in the order the event listener was defined.&lt;/p&gt;

&lt;p&gt;There is also another way to apply event listener to an element, and that is setting the event handler property to a function to handle that event. For example, &lt;code&gt;buttton.onclick = doMagic&lt;/code&gt; but by adding event listener this way you can only add one handler. &lt;/p&gt;

&lt;h3&gt;
  
  
  Capture and Bubble Phase &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Sometimes, you may have an event listener on the parent element but not on the child, or you have it for both. So, to deal with nested elements situation and when each event handler will run, you need to be aware of the &lt;em&gt;Capture&lt;/em&gt; phase and the &lt;em&gt;Bubble&lt;/em&gt; phase. "Bubbling" means that the event "propagate", aka move up and outward from the inner most element all the way to the &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element. And "Capturing" is the opposite, the browser will call the event handler for the outer most element (&lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;) down to the inner most element. &lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Capture&lt;/em&gt; phase always gets called before the &lt;em&gt;Bubble&lt;/em&gt; phase, which means that any element with the event listener set to use capture will fire before all element set with bubble. To use capture, we shall return to &lt;code&gt;.addEventListener()&lt;/code&gt;, you can add a third argument when attaching the event listener &lt;code&gt;{ capture: true }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zFldDf5Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2cax3r2dhyqg0s11jeq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zFldDf5Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2cax3r2dhyqg0s11jeq.png" alt="Capture and Bubbling" width="580" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;I have made something silly with the codepen below. I used &lt;code&gt;window.onload&lt;/code&gt; event handler property to make bubbles show up on the screen once the window has loaded. I added an event listener to each bubble so upon mouse entering the element, the bubble would disappear. I also added a small example of capturing and bubbling at the bottom of the JS file, console logging some words when the element is clicked. You can see the code snippet below, take a guess what the console will log, then click anywhere on the screen and check the console to see if you were correct 😀. Note: canvas is the variable name holding the &lt;code&gt;&amp;lt;main&amp;gt;&lt;/code&gt; element of the page.&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sparkle&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="nx"&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;sparkle&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sparkleTwice&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="nx"&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;sparkle, sparkle&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;dazzle&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="nx"&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;dazzle&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sparkle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;capture&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sparkleTwice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dazzle&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Katakung95/embed/wvyKxVY?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

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

&lt;p&gt;So, to recap everything, if you want to listen out for certain actions, events, then you can attach an event listener to the element. When the event fires the event listener will call the event handler, a callback function taking the event object as a parameter and will run a block of code. If you have nested elements, you now know that bubbling will cause the event to propagate up so if you find an odd behavior or issues with your event listener, this could be the reason why. You are also aware of using capture to trigger your event handlers in a specific order. &lt;/p&gt;

&lt;p&gt;I believe that the best way to understand any concept is to get out there and try to implement something. If you need a starting point, you can break my code, add more event listeners, change the function, play around with it. Or just create your own from scratch! 😀&lt;/p&gt;

&lt;p&gt;Thank you for reading, if you have any comments or feedback please leave them down below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u_i03OBg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://twinfinite.net/wp-content/uploads/2021/04/one-punch-man.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u_i03OBg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://twinfinite.net/wp-content/uploads/2021/04/one-punch-man.jpg" alt="Smiling Saitama Anime Character" width="880" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

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