<?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: Jason</title>
    <description>The latest articles on DEV Community by Jason (@jasonnordheim).</description>
    <link>https://dev.to/jasonnordheim</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%2F371087%2Fd7d70e53-33ca-4cbe-993a-3a006c1de622.jpeg</url>
      <title>DEV Community: Jason</title>
      <link>https://dev.to/jasonnordheim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jasonnordheim"/>
    <language>en</language>
    <item>
      <title> Let's get Sassy 💅</title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Mon, 07 Dec 2020 07:34:14 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/let-s-get-sassy-4onp</link>
      <guid>https://dev.to/jasonnordheim/let-s-get-sassy-4onp</guid>
      <description>&lt;h1&gt;
  
  
  What is Sass?
&lt;/h1&gt;

&lt;p&gt;Sass or &lt;em&gt;syntactically awesome stylesheets&lt;/em&gt; is a preprocessor for CSS that fixes many of the pain points most developers have with CSS. &lt;/p&gt;

&lt;p&gt;There are two types of Sass &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;SCSS (&lt;code&gt;.scss&lt;/code&gt;) → "sassy css" &lt;/li&gt;
&lt;li&gt;Sass (&lt;code&gt;.sass&lt;/code&gt;) → "syntactically awesome stylesheets" &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here we are going to focus on &lt;code&gt;.scss&lt;/code&gt; version of Sass since most the syntax for &lt;code&gt;.scss&lt;/code&gt; is &lt;em&gt;nearly&lt;/em&gt; the same as traditional &lt;code&gt;.css&lt;/code&gt; files, and will be more useful to most developers than learning an entirely new syntax. Along the same vein, comparing &lt;code&gt;.scss&lt;/code&gt; with &lt;code&gt;.css&lt;/code&gt; files will be easier than &lt;code&gt;.sass&lt;/code&gt; files since they look more python-like in their syntax. &lt;/p&gt;

&lt;h1&gt;
  
  
  What's wrong with CSS? 😑
&lt;/h1&gt;

&lt;p&gt;Since Sass is a preprocessor of CSS, you could write &lt;code&gt;.scss&lt;/code&gt; files in vanilla CSS, but you'd be missing out on all the awesome features that the Sass preprocessor provides. The core of those features really aim to address the scalability and maintainability of CSS in modern web applications. &lt;/p&gt;

&lt;p&gt;With sassy css, you have all the power of CSS... plus&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;variables (_technically you can use variables in &lt;code&gt;.css&lt;/code&gt; files, but its just better with sass)&lt;/li&gt;
&lt;li&gt;nesting &lt;/li&gt;
&lt;li&gt;mixins &lt;/li&gt;
&lt;li&gt;inheritance &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Getting Started  🏁
&lt;/h1&gt;

&lt;p&gt;So before you can do anything, you've need to get the Sass compiler installed. For the latest instruction, check out the &lt;a href="https://sass-lang.com/install"&gt;sass install documentation&lt;/a&gt;, but for the &lt;code&gt;tl;dr;&lt;/code&gt; version you can get the Sass compiler setup globally using Node.js/NPM using the &lt;code&gt;sass&lt;/code&gt; module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; sass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also install the Sass preprocessor using homebrew.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;sass/sass/sass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;this installs the Node sass implementation of the pre-processor which is &lt;a href="https://github.com/sass/node-sass#node-sass"&gt;currently deprecated&lt;/a&gt; - while the sassy engineering team has said that this package will continue to receive updates the most up-to-date version is &lt;a href="https://sass-lang.com/dart-sass"&gt;dart sass&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once its installed, you can compile &lt;code&gt;.scss&lt;/code&gt; files (or &lt;code&gt;.sass&lt;/code&gt; files) using &lt;code&gt;sass &amp;lt;source-file&amp;gt; &amp;lt;output-file&amp;gt;&lt;/code&gt; like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sass &lt;span class="nb"&gt;source&lt;/span&gt;/stylesheets/index.scss build/stylesheets/index.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the compiler installed, lets get sassy! &lt;/p&gt;

&lt;h1&gt;
  
  
  Sass Selectors 💃
&lt;/h1&gt;

&lt;p&gt;As mentioned previously, files written in &lt;code&gt;.css&lt;/code&gt; are 100% valid as &lt;code&gt;.scss&lt;/code&gt; files. So comments and selecting html elements can be done in the same syntax: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;comments 

&lt;ul&gt;
&lt;li&gt;single line comments can be made using a double slash (&lt;code&gt;//&lt;/code&gt;) &lt;/li&gt;
&lt;li&gt;multi-line comments can be denoted slash-start (&lt;code&gt;/*&lt;/code&gt;) and  star-slash (&lt;code&gt;*/&lt;/code&gt;) , with the &lt;code&gt;/*&lt;/code&gt; denoting the start of the multi-line comment and the &lt;code&gt;*/&lt;/code&gt; denoting the end of the multi-line comment &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;selectors 

&lt;ul&gt;
&lt;li&gt;tags 

&lt;ul&gt;
&lt;li&gt;tags are selected simply by placing the tag name &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;header&lt;/code&gt; selects &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;classes 

&lt;ul&gt;
&lt;li&gt;select classes using &lt;code&gt;.&lt;/code&gt; followed by the class name (e.g. &lt;code&gt;nav-links&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.nav-link&lt;/code&gt; selects any elements with a &lt;code&gt;class="nav-link"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;id 

&lt;ul&gt;
&lt;li&gt;select an element by it's id using &lt;code&gt;#&lt;/code&gt; followed by the id &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#root&lt;/code&gt; selects any element with &lt;code&gt;id="root"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


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

&lt;ul&gt;
&lt;li&gt;the actual rules of &lt;code&gt;.css&lt;/code&gt; and &lt;code&gt;.scss&lt;/code&gt; files &lt;/li&gt;
&lt;li&gt;defined between curly braces (&lt;code&gt;{}&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;each line / statement should terminate with a semi-colon &lt;code&gt;;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* select an element by using its name */&lt;/span&gt; 
&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/* select an element by its ID */&lt;/span&gt;
&lt;span class="nn"&gt;#root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/* selecting elements by class */&lt;/span&gt;
&lt;span class="nc"&gt;.nav-links&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&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;h1&gt;
  
  
  Sassy selecting 👌
&lt;/h1&gt;

&lt;p&gt;One of the first features &lt;code&gt;.scss&lt;/code&gt; improves on from &lt;code&gt;.css&lt;/code&gt; is the selection of &lt;strong&gt;nested elements&lt;/strong&gt;. Imagine you are styling a basic navbar for a website:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--- index.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;header&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;nav&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"logo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Sassy&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; 
          &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;About&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; 
          &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Contact&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; 
          &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;/header&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://codepen.io/collection/XkmkPv"&gt;codepen&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You might do something like:&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="c"&gt;/* CSS */&lt;/span&gt;
&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;border-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#1F7A8C&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="nc"&gt;.logo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;cursive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;list-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#BFDBF7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.2rem&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#1F7A8C&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#053C5E&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;inherit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;monospace&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1.3rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;place-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&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;h1&gt;
  
  
  Nesting selectors ⇣
&lt;/h1&gt;

&lt;p&gt;Instead of repeating &lt;code&gt;nav ul&lt;/code&gt; at the beginning of each selection statement as we did in or &lt;code&gt;.css&lt;/code&gt; file, we can nest the selector definitions inside one another.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nt"&gt;header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="mh"&gt;#1F7A8C&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="nc"&gt;.logo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;cursive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
          &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
          &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;list-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#053C5E&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#BFDBF7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.2rem&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="mh"&gt;#1F7A8C&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;inherit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
                &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;monospace&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
                &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
                &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="mi"&gt;.3rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="na"&gt;place-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
                &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&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="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;Notice how each selector is &lt;strong&gt;nested&lt;/strong&gt; inside the curly-braces (&lt;code&gt;{}&lt;/code&gt;) comprising it's parent selector. &lt;/p&gt;

&lt;p&gt;Notice how the &lt;code&gt;.scss&lt;/code&gt; version shorter, but it's easier to understand and easier to read. &lt;/p&gt;

&lt;p&gt;Solely with the information of the &lt;code&gt;.scss&lt;/code&gt; file above, we know that the &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt; element is a child of the &lt;code&gt;&amp;lt;header&amp;gt;&lt;/code&gt; element because the &lt;code&gt;nav&lt;/code&gt; selector is nested inside the &lt;code&gt;header&lt;/code&gt; selector. We don't need to repeat selectors in nested selector statements either. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In this simple example, the benefit might seem negligible, but this becomes increasingly valuable as your stylesheets grow in size. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Sassy Pseudo selectors 🥸
&lt;/h1&gt;

&lt;p&gt;Given the previous &lt;code&gt;.html&lt;/code&gt; file we looked at before, how would you add the &lt;code&gt;:hover&lt;/code&gt; pseudo selector in CSS?&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="c"&gt;/* redefine the entire selector hierarchy (nav ul li) and apped pseudo selector */&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1s&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="n"&gt;ease&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c"&gt;/* make the text black */&lt;/span&gt;
    &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;invert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c"&gt;/* invert the default colors */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Sassy way emphasizes the DRY principle, instead providing its own pseudo selector to address these style definitions. &lt;/p&gt;

&lt;p&gt;In Sass, the ampersand (&lt;code&gt;&amp;amp;&lt;/code&gt;) represents the parent selector, and enables chaining. So if we can define seperate styles for mouse over (&lt;code&gt;:hover&lt;/code&gt;), visited pages (&lt;code&gt;:visited&lt;/code&gt;), the last item (&lt;code&gt;nth()&lt;/code&gt;) and all the other pseudo selectors like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* abbreviated .scss file */&lt;/span&gt;
&lt;span class="cm"&gt;/* css removed from most of document for simplicity */&lt;/span&gt; 
&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* styles.. */&lt;/span&gt;
  &lt;span class="nt"&gt;header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* styles.. */&lt;/span&gt;
    &lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="cm"&gt;/* styles.. */&lt;/span&gt;
        &lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="nc"&gt;.logo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* styles.. */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* styles.. */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="cm"&gt;/* &amp;amp; represents parent (li) */&lt;/span&gt; 
            &lt;span class="cm"&gt;/* ⬇️ hover styling ⬇️ */&lt;/span&gt;
            &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1s&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="n"&gt;ease&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
                &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="cm"&gt;/* make the text black */&lt;/span&gt;
                &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;invert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="cm"&gt;/* invert the default colors */&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="nt"&gt;a&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;amp;&lt;/code&gt; refers to the selector parent, so since we are in the body of the &lt;code&gt;li&lt;/code&gt;'s styling, the &lt;code&gt;li&lt;/code&gt; tag is the parent select the &lt;code&gt;&amp;amp;&lt;/code&gt; symbol is referencing. &lt;/p&gt;

&lt;p&gt;We can use the &lt;code&gt;&amp;amp;&lt;/code&gt; operator in our sassy &lt;code&gt;.scss&lt;/code&gt; files in conjunctions with all the same &lt;a href="https://www.w3schools.com/css/css_pseudo_classes.asp"&gt;pseudo selectors&lt;/a&gt; as &lt;code&gt;.css&lt;/code&gt; files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* hover pseudo selector */&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:focus&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* focus pseudo selector */&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:visited&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="cm"&gt;/* visited pseudo selector */&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;But we can get sassier. In &lt;code&gt;.scss&lt;/code&gt; we can truly embrace the DRY principles and use the &lt;code&gt;&amp;amp;&lt;/code&gt; operator to append to the parent selector. &lt;/p&gt;

&lt;p&gt;So if we have an element with the class &lt;code&gt;.hero&lt;/code&gt;, and inside that element, there are other elements that have classes starting with &lt;code&gt;.hero&lt;/code&gt;, we can append to the class selector using the &lt;code&gt;&amp;amp;&lt;/code&gt; operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.hero&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;-container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* equivalent to `hero-container` */&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;-title&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* equivalent to `hero-title` */&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;-sub-title&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* equivalent to `hero-sub-title` */&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;h2&gt;
  
  
  🕺 Variables with sass
&lt;/h2&gt;

&lt;p&gt;Variables in &lt;code&gt;.scss&lt;/code&gt; files start with the dollar-sign symbol (&lt;code&gt;$&lt;/code&gt;), and can store any valid CSS value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nv"&gt;$primary-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#053C5E&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="nv"&gt;$secondary-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#BFDBF7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then assign the values stored in our sassy variables simply by placing where ever we need it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$secondary-color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#053C5E&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;When the &lt;code&gt;.scss&lt;/code&gt; file is run through the Sass compiler, the variables values will be replaced with the value assigned to the variable. While native CSS does allow for variables, Sass does it better. &lt;/p&gt;

&lt;p&gt;Another benefit to &lt;code&gt;.scss&lt;/code&gt; variables is that they can be mutated from the start of the file to the end, without altering the v&lt;/p&gt;

&lt;p&gt;Sass variables support being mutated from the start of the file to the end. If you assign &lt;code&gt;$primary-color: #053C5E&lt;/code&gt;, on line 10, and re-assign it on line 254 mutated the value such that &lt;code&gt;$primary-color: #000&lt;/code&gt;, every subsequent block will substitute &lt;code&gt;$primary-color&lt;/code&gt; will receive the color code of &lt;code&gt;#000&lt;/code&gt;, while every element before its re-assignment on line 254 will remain the original value of &lt;code&gt;#053C5E&lt;/code&gt;. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Sass&lt;/th&gt;
&lt;th&gt;CSS&lt;/th&gt;
&lt;th&gt;Ramifications&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Sass variables are replaced with their values by the Sass compiler&lt;/td&gt;
&lt;td&gt;CSS variables are loaded and interpreted by the browser&lt;/td&gt;
&lt;td&gt;more overhead for the client browser&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sass variables are pre-fixed with &lt;code&gt;$varName&lt;/code&gt; and called using &lt;code&gt;$varName&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;CSS variables are declared using &lt;code&gt;--varName&lt;/code&gt; and are scoped to the tag that they are defined in&lt;/td&gt;
&lt;td&gt;using variables is more complex, syntactically longer, and more difficult to read&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sass is transpile to basic CSS&lt;/td&gt;
&lt;td&gt;CSS variables are only supported by newer browsers (explorer will not work)&lt;/td&gt;
&lt;td&gt;Sass compiles &lt;code&gt;.scss&lt;/code&gt; files such that the resulting configuration has more compatibility than CSS variables&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Just like most developers are used to, variables defined using &lt;code&gt;$varName&lt;/code&gt; are scoped to the block, (i.e. between &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;) that they were defined in. If you define at the root of your &lt;code&gt;.scss&lt;/code&gt; document, it will be scoped to the entire document, define it within the scope of a selector it will be scoped to the parent selector and its children.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* variables defined here will only be accessible within the body 
     * of the selector and its nested selectors */&lt;/span&gt;
    &lt;span class="nv"&gt;$font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* 👍 we CAN access $font-size here ✅ */&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/* 👎 we CANNOT access $front-size here ⛔️ */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sass variables mutated from the original values start of the file to the end. If you assign &lt;code&gt;$primary-color: #053C5E&lt;/code&gt;, on line 10, and re-assign it on line 254 mutated the value such that &lt;code&gt;$primary-color: #000&lt;/code&gt;, every subsequent block will substitute &lt;code&gt;$primary-color&lt;/code&gt; will receive the color code of &lt;code&gt;#000&lt;/code&gt;, while every element before its re-assignment on line 254 will remain the original value of &lt;code&gt;#053C5E&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nv"&gt;$color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#444&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* mutation scoped to this block */&lt;/span&gt;
    &lt;span class="na"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* color → #444 */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;footer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/* color → #333 */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Sassy Mixins and At-rules 🌀
&lt;/h1&gt;

&lt;p&gt;Mixins are like JavaScript functions: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they can be declared as a variable and re-used in multiple locations &lt;/li&gt;
&lt;li&gt;they can take 0 or more arguments &lt;/li&gt;
&lt;li&gt;they can be scoped (like a function assigned to &lt;code&gt;const&lt;/code&gt;) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You begin a &lt;em&gt;mixin&lt;/em&gt; with &lt;code&gt;@mixin&lt;/code&gt; followed by the name of the &lt;em&gt;mixin&lt;/em&gt; you are defining. &lt;/p&gt;

&lt;p&gt;Then place all the &lt;code&gt;.css&lt;/code&gt; rules between the opening curly brace (&lt;code&gt;{&lt;/code&gt;) and the closing curly brace (&lt;code&gt;}&lt;/code&gt;) that make up the mixins body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* reset properties associated with a list */&lt;/span&gt;
&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;reset-list&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;list-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&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;We can then use apply our &lt;code&gt;@mixin reset-list&lt;/code&gt;  anywhere it makes sense to do so, &lt;strong&gt;including inside another &lt;code&gt;@mixin&lt;/code&gt; definition&lt;/strong&gt; using the &lt;code&gt;@include&lt;/code&gt; at-rule, followed by the name of the mixin (e.g. &lt;code&gt;reset-list&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* apply mixin to navbar  */&lt;/span&gt;
&lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@include&lt;/span&gt; &lt;span class="nd"&gt;horizontal-list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* apply mixin to another mixin */&lt;/span&gt;
&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;horizontal-list&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@include&lt;/span&gt; &lt;span class="nd"&gt;reset-list&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-2px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2em&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://sass-lang.com/documentation/at-rules/mixin"&gt;sass docs&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adding arguments to sassy &lt;code&gt;@mixin&lt;/code&gt; is remarkable similar to the syntax used in JavaScript as well. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@mixin variable-name&lt;/code&gt; remains the same, but before the opening curly brace defining the &lt;code&gt;.css&lt;/code&gt; properties, we place any parameters delimited by commas (&lt;code&gt;,&lt;/code&gt;) and surrounded by parenthesis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;rtl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$ltr-value&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$rtl-value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="nd"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;ltr-value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;rtl&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="nd"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;rtl-value&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="nc"&gt;.sidebar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@include&lt;/span&gt; &lt;span class="nd"&gt;rtl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;left&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;right&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;Every rule previously mention applies here as well. Each variable (&lt;code&gt;$variableName&lt;/code&gt;) will be subsituted for the parameter provided to the mixin when we declare the mixin using the &lt;code&gt;@include mixin-name&lt;/code&gt;  followed by the parameters wrapped in parenthesis. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mutating a mixin will only affect subsequent code &lt;/li&gt;
&lt;li&gt;same scoping rules apply &lt;/li&gt;
&lt;li&gt;same syntax for declaring and using variables &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can define default values for &lt;code&gt;@mixins&lt;/code&gt; using a colon (&lt;code&gt;:&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;replace-text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$image&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$y&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;text-indent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-99999em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$image&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="na"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$x&lt;/span&gt; &lt;span class="nv"&gt;$y&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="nc"&gt;.mail-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@include&lt;/span&gt; &lt;span class="nd"&gt;replace-text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sx"&gt;url("/images/mail.svg")&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&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;h2&gt;
  
  
  Interpolation
&lt;/h2&gt;

&lt;p&gt;Interpolation can be used almost anywhere in a Sass stylesheet. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to define classes &lt;/li&gt;
&lt;li&gt;to define css properties &lt;/li&gt;
&lt;li&gt;to define values of css properties &lt;/li&gt;
&lt;li&gt;to combine variables
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;inline-animation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$duration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nf"&gt;unique-id&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;@content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nl"&gt;animation-name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;animation-duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$duration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;animation-iteration-count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;infinite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.pulse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@include&lt;/span&gt; &lt;span class="nd"&gt;inline-animation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nt"&gt;from&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using a variable in a property value doesn't require interpolation. (e.g. &lt;code&gt;color:#{$accent}&lt;/code&gt; === &lt;code&gt;color: $accent&lt;/code&gt;) &lt;/li&gt;
&lt;li&gt;interpolated variables will be returned as strings. &lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Mixin and Flow Control 🪖
&lt;/h1&gt;

&lt;p&gt;Mixins provide for a variety of flow control  with &lt;code&gt;@if&lt;/code&gt;, &lt;code&gt;@else&lt;/code&gt;, &lt;code&gt;@while&lt;/code&gt; ,&lt;code&gt;@each&lt;/code&gt; and &lt;code&gt;@for&lt;/code&gt; at-rules. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@if&lt;/code&gt; determines whether or not a block is evaluated.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@each&lt;/code&gt; evaluates a block for each element in a list or each pair in a map.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@for&lt;/code&gt;evaluates a block a certain number of times.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@while&lt;/code&gt; evaluates a block until a certain condition is met.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* only apply a border-radius if the the radius is not 0  */&lt;/span&gt;
&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$size&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;@if&lt;/span&gt; &lt;span class="nv"&gt;$radius&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* must evaluate to True/False */&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$radius&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="cm"&gt;/* can only be included if there is an `@if` at-rule */&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;unset&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;You can implement &lt;code&gt;if&lt;/code&gt; → &lt;code&gt;else if&lt;/code&gt; → &lt;code&gt;else&lt;/code&gt; logic by inserting a &lt;code&gt;@else if&lt;/code&gt; at-rule following an &lt;code&gt;@if&lt;/code&gt; at-rule and before an &lt;code&gt;@else&lt;/code&gt; at rule&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;triangle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$size&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$direction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nl"&gt;border-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$size&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;@if&lt;/span&gt; &lt;span class="nv"&gt;$direction&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;up&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border-bottom-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$color&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="n"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;$direction&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;right&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border-left-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$color&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="n"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;$direction&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border-top-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$color&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="n"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;$direction&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;left&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border-right-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$color&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;@error&lt;/span&gt; &lt;span class="s2"&gt;"Unknown direction &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nv"&gt;$direction&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&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;blockquote&gt;
&lt;p&gt;anything with a determined value is truthy and anything that is &lt;code&gt;null&lt;/code&gt; is falsey &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Looping ➰
&lt;/h2&gt;

&lt;p&gt;We can implement looping in &lt;code&gt;@mixin&lt;/code&gt;s using &lt;code&gt;@for&lt;/code&gt; at-rule and interpolation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$height&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$selectors&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="cm"&gt;/* increase the margin top for each element selected 
     * in $selectors */&lt;/span&gt;
  &lt;span class="k"&gt;@for&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="ow"&gt;from&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;to&lt;/span&gt; &lt;span class="nf"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$selectors&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="cm"&gt;/* interpolate the selector using the `nth` function */&lt;/span&gt;
    &lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nf"&gt;nth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$selectors&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;margin-top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$height&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;@each&lt;/code&gt; at-rule works similarly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* collection of sizes */&lt;/span&gt;
&lt;span class="nv"&gt;$sizes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40px&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;80px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* loop through the values applying the size to the 
 * matching class (e.g. `icon-50px` ) */&lt;/span&gt;
&lt;span class="k"&gt;@each&lt;/span&gt; &lt;span class="nv"&gt;$size&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;$sizes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.icon-&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nv"&gt;$size&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$size&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;You can also loop through collections using the &lt;code&gt;@while&lt;/code&gt; mixin, but doing so typically is implented using functions, which definitely goes beyond the basics, and we've already covered a lot here. &lt;/p&gt;

&lt;h1&gt;
  
  
  Wrap up
&lt;/h1&gt;

&lt;p&gt;These are the fundamentals of Sass, but there's lots more to it. There's more &lt;a href="https://sass-lang.com/documentation/at-rules"&gt;at-rules&lt;/a&gt; as well as functions and modules. However, even without functions, or mixins, Sass makes styling a more readable, more maintainable, and more concise way to add styling to html. &lt;/p&gt;

&lt;p&gt;So the question is, do you want to get sassy? 💅&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>codenewbie</category>
      <category>design</category>
    </item>
    <item>
      <title>🧐 Demystifying Big 🅾️ </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Mon, 30 Nov 2020 06:27:22 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/demystifying-big-1djf</link>
      <guid>https://dev.to/jasonnordheim/demystifying-big-1djf</guid>
      <description>&lt;h1&gt;
  
  
  What is Big 🅾?
&lt;/h1&gt;

&lt;p&gt;Big O is fundamental in the world of computer science, yet many developers and engineers often struggle to understand it. &lt;/p&gt;

&lt;p&gt;Most people know its important, and its commonly something software engineers to ruminate upon, exaggerating the complexity and difficulties in understanding and using Big O. &lt;br&gt;
This fear can often results in avoidance of the topic. &lt;/p&gt;

&lt;p&gt;Let's stop fearing it, and start getting excited about it. Big O can be a helpful tool to help developers and software engineers to improve the quality and efficiency of code. &lt;/p&gt;
&lt;h2&gt;
  
  
  Learning Objectives
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;provide an introduction into Big O notation &lt;/li&gt;
&lt;li&gt; define "algorithms" in the context of Big O &lt;/li&gt;
&lt;li&gt;define time complexity/efficiency 

&lt;ul&gt;
&lt;li&gt;asymptotic behavior &lt;/li&gt;
&lt;li&gt;worst-case analysis &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;explain how to calculate time complexity 

&lt;ul&gt;
&lt;li&gt;step-by-step breakdown &lt;/li&gt;
&lt;li&gt;code examples &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; define the fundamental types of asymptotic complexity &lt;/li&gt;
&lt;li&gt;Differentiate between O, o, θ, Ω and ⍵&lt;/li&gt;
&lt;/ol&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%2Freachinghighernh.org%2Fwp-content%2Fuploads%2F2018%2F07%2Fcompetency-portfolio-learning.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%2Freachinghighernh.org%2Fwp-content%2Fuploads%2F2018%2F07%2Fcompetency-portfolio-learning.png" alt="learn"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Here we will focus Big O are in terms of time complexity since this is what most people are referring to when discussing Big O.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;
  
  
  Algorithms → Big 🅾
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Algorithms&lt;/strong&gt; are programmatic operations that performs a computation to calculate a value or values. &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%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2F20191016135223%2FWhat-is-Algorithm_-1024x631.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%2Fmedia.geeksforgeeks.org%2Fwp-content%2Fcdn-uploads%2F20191016135223%2FWhat-is-Algorithm_-1024x631.jpg" alt="algorithm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Big O&lt;/strong&gt; is the standard notation computer scientists use to define, discuss, compare and quantify the performance of algorithms using mathematical terms. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;define how the algorithm responds to an increasing number of input values (quantify how algorithm scales) &lt;/li&gt;
&lt;li&gt;mathematically compare the efficiency of different algorithms &lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Complexity Analysis &amp;amp; Time Complexity
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Complexity analysis&lt;/strong&gt; is a tool for determining how an algorithm responds to increasing inputs. It can be defined in terms of both &lt;em&gt;spacial efficiency&lt;/em&gt; as well as &lt;em&gt;time complexity&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;time complexity&lt;/strong&gt; refers to the efficiency of an algorithm in respect to the amount of time an algorithm requires to execute completely as its input increases.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spacial complexity&lt;/strong&gt; refers to the efficiency of an algorithm in respect to the amount of memory (RAM) it consumes as the input increases.&lt;/li&gt;
&lt;/ul&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%2Fs3.amazonaws.com%2Fmentoring.redesign%2Fs3fs-public%2Fefficiency.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%2Fs3.amazonaws.com%2Fmentoring.redesign%2Fs3fs-public%2Fefficiency.jpg" alt="effeciency"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: this article, we will be focusing on &lt;em&gt;time complexity&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Lots of Factors
&lt;/h3&gt;

&lt;p&gt;The speed at which a computer can execute (complete) and algorithm is impacted by a wide variety of factors: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hardware 

&lt;ul&gt;
&lt;li&gt;CPU Clock Speed &lt;/li&gt;
&lt;li&gt;CPU Cores (dual-core, quad-core, etc.)&lt;/li&gt;
&lt;li&gt;Cooling Ability (ability to dispense head from critical components under load)&lt;/li&gt;
&lt;li&gt;RAM read/write speed&lt;/li&gt;
&lt;li&gt;Hard-drive read/write speed &lt;/li&gt;
&lt;li&gt;Available disc &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Network 

&lt;ul&gt;
&lt;li&gt;Network speed (up/down)&lt;/li&gt;
&lt;li&gt;Concurrent Network Operations &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Software

&lt;ul&gt;
&lt;li&gt;Operating System (Mac, Windows, Linux) &lt;/li&gt;
&lt;li&gt;Concurrent applications &lt;/li&gt;
&lt;li&gt;Security &amp;amp; monitoring software &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key idea here is that finite execution speed is highly variable. When we calculate an algorithms &lt;strong&gt;time complexity&lt;/strong&gt; we simplify the calculation to only include the &lt;em&gt;specific instructions executed as a direct result of an algorithm&lt;/em&gt;, ignoring all the factors that may impact execution speed when they are outside the scope of the algorithm.&lt;/p&gt;
&lt;h2&gt;
  
  
  Calculating Time-Complexity
&lt;/h2&gt;

&lt;p&gt;Calculating an algorithm's time complexity begins by breaking down the operation into individual steps. &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%2Fhrwatchdog.calchamber.com%2Fwp-content%2Fuploads%2FArithmeticFact-1-e1563553095922.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%2Fhrwatchdog.calchamber.com%2Fwp-content%2Fuploads%2FArithmeticFact-1-e1563553095922.jpg" alt="calculating"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What's a step?
&lt;/h3&gt;

&lt;p&gt;In the context of big O and algorithmic complexity... &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a step is &lt;strong&gt;NOT&lt;/strong&gt; → 🚫

&lt;ul&gt;
&lt;li&gt;a function &lt;/li&gt;
&lt;li&gt;a line of code &lt;/li&gt;
&lt;li&gt;a statement in code &lt;/li&gt;
&lt;li&gt;a block of code &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;a step is → ✅

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;a single unit of work executed by the central processing unit (CPU)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This includes operations like: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;assigning a value to a variable &lt;/li&gt;
&lt;li&gt;looking up the value of an element in an array&lt;/li&gt;
&lt;li&gt;comparing two values &lt;/li&gt;
&lt;li&gt;incrementing/decrementing a value &lt;/li&gt;
&lt;li&gt;a single arithmetic operation (addition, subtraction, multiplication, division) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and many more. &lt;/p&gt;

&lt;p&gt;The goal is to break down an algorithm into the smallest unit of work that a computer can perform in a single step. &lt;/p&gt;
&lt;h3&gt;
  
  
  Learn by Example
&lt;/h3&gt;

&lt;p&gt;One of the best ways to illustrate calculating time-complexity is to work through a real example. &lt;/p&gt;

&lt;p&gt;Take the &lt;code&gt;maximum&lt;/code&gt; function 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;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;67&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;94&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;47&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// input &lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;maximum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&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;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&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="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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;max&lt;/span&gt;&lt;span class="p"&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;maximum&lt;/code&gt; defined above is an example of an algorithm. It is a strictly defined process for finding the largest value in a collection of numbers. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;maximum&lt;/code&gt; function involves the following steps &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;create &lt;code&gt;max&lt;/code&gt; variable (allocate memory)
&lt;/li&gt;
&lt;li&gt;look up value of item at index &lt;code&gt;0&lt;/code&gt; in &lt;code&gt;arr&lt;/code&gt; (e.g. &lt;code&gt;arr[0]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;assign the value returned as to &lt;code&gt;max&lt;/code&gt; variable (e.g. &lt;code&gt;max = arr[0]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;create &lt;code&gt;i&lt;/code&gt; variable (e.g. &lt;code&gt;let i&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;assign &lt;code&gt;i&lt;/code&gt; a value of &lt;code&gt;1&lt;/code&gt; (e.g. &lt;code&gt;let i = 1&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;define loop termination condition (e.g. &lt;code&gt;i &amp;lt; arr.length&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;define loop increment (&lt;code&gt;i++&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;retrieve the value at position &lt;code&gt;1&lt;/code&gt; in &lt;code&gt;arr&lt;/code&gt; (e.g. &lt;code&gt;arr[1]&lt;/code&gt; → &lt;code&gt;23&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;compare the value of the element retrieved to &lt;code&gt;max&lt;/code&gt; (e.g. &lt;code&gt;arr[i] &amp;gt; max&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;since &lt;code&gt;arr[1]&lt;/code&gt; is &lt;code&gt;23&lt;/code&gt; in the example above, the loop concludes and restarts &lt;/li&gt;
&lt;li&gt;increment &lt;code&gt;i&lt;/code&gt; (e.g. &lt;code&gt;i++&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;lookup value at &lt;code&gt;arr[2]&lt;/code&gt; (since &lt;code&gt;i = 2&lt;/code&gt;) &lt;/li&gt;
&lt;li&gt;compare &lt;code&gt;arr[2]&lt;/code&gt; with &lt;code&gt;max&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;since &lt;code&gt;arr[2] = 67&lt;/code&gt; and &lt;code&gt;max = 45&lt;/code&gt;, &lt;code&gt;arr[i] &amp;gt; max&lt;/code&gt; evaluates to &lt;code&gt;true&lt;/code&gt; (e.g &lt;code&gt;67 &amp;gt; 45&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;enter &lt;code&gt;if&lt;/code&gt; block &lt;/li&gt;
&lt;li&gt;re-assign &lt;code&gt;max = arr[i]&lt;/code&gt; (e.g. &lt;code&gt;max = 67&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;increment &lt;code&gt;i&lt;/code&gt; (e.g. &lt;code&gt;i++&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;... and continuing the loop block until the loops terminating condition (&lt;code&gt;i &amp;lt;arr.length&lt;/code&gt;) is reached.  &lt;/p&gt;

&lt;p&gt;Now since we are focused on time-complexity, we need to differentiate between setup steps, and the steps that are impacted by the size of the input. In other words, define which steps would increase as the input to the algorithm increases, from the steps that would execute regardless of the size of the input. &lt;/p&gt;

&lt;p&gt;So given the &lt;code&gt;maximum&lt;/code&gt; algorithm: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Independent Steps (required regardless of input size) 

&lt;ul&gt;
&lt;li&gt;create &lt;code&gt;max&lt;/code&gt; variable &lt;/li&gt;
&lt;li&gt;assign &lt;code&gt;max&lt;/code&gt; to the first value of the input (&lt;code&gt;arr[0]&lt;/code&gt;) &lt;/li&gt;
&lt;li&gt;creating loop statement 

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;i&lt;/code&gt; variable &lt;/li&gt;
&lt;li&gt;assigning the &lt;code&gt;i&lt;/code&gt; variable a value of &lt;code&gt;1&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;returning the &lt;code&gt;max&lt;/code&gt; value &lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;Dependent Operations (execute a variable number of times depending on input size)

&lt;ul&gt;
&lt;li&gt;Number of times we have to increment &lt;code&gt;i&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Number of times we have to lookup a value from the input &lt;code&gt;arr[i]&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;comparing the value of &lt;code&gt;arr[i]&lt;/code&gt; to &lt;code&gt;max&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;assigning &lt;code&gt;max&lt;/code&gt; a value &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember: we are time complexity is quantifying how an algorithm responds to an increasing input &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Given the above, we can state objectively that the &lt;code&gt;maximum&lt;/code&gt; function has&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4 independent operations&lt;/li&gt;
&lt;li&gt;4 dependent operations &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or as a mathematical function where &lt;code&gt;n&lt;/code&gt; represents the size of the input: &lt;code&gt;f(n) = 4n + 4&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;4&lt;/code&gt; referring to the operations that are &lt;strong&gt;not&lt;/strong&gt; affected by &lt;code&gt;n&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;4n&lt;/code&gt; referring to the operations affected by &lt;code&gt;n&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Worst-case Scenario
&lt;/h3&gt;

&lt;p&gt;After distinguishing the individual steps of an algorithm, we need to determine how those execution steps are affected by different types of input. &lt;/p&gt;

&lt;p&gt;We start evaluating input types with what is called the &lt;strong&gt;worst-case scenario&lt;/strong&gt;. In terms of time-complexity, the &lt;strong&gt;worst case scenario&lt;/strong&gt; refers to an input that would require the most operations to complete. &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%2Fnlgroup.com%2Fwp-content%2Fuploads%2F2018%2F01%2FWorst-Case-Scenario.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%2Fnlgroup.com%2Fwp-content%2Fuploads%2F2018%2F01%2FWorst-Case-Scenario.jpg" alt="worst case"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example of the &lt;code&gt;maximum&lt;/code&gt; function, the &lt;strong&gt;worst-case scenario&lt;/strong&gt; would be the scenario in which the input array was sorted smallest to largest, which would require every iteration through the &lt;code&gt;for&lt;/code&gt; loop to evaluate the &lt;code&gt;if (arr[i] &amp;gt; max)&lt;/code&gt; as &lt;code&gt;true&lt;/code&gt;, and thus executing an additional steps defined inside the &lt;code&gt;if&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;max&lt;/span&gt; &lt;span class="p"&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;So using the 4 independent steps, 4 dependent steps, and 2 conditional steps, we can alter the previous formula to add the conditionally dependent steps outlined as part of the worst-case scenario: &lt;code&gt;f(n) = 4n + 4 + 2n&lt;/code&gt; or simplified as &lt;code&gt;f(n) = 6n + 4&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Asymptotic behavior
&lt;/h2&gt;

&lt;p&gt;In complexity analysis, we are primarily concerned with how the function (&lt;code&gt;f(n)&lt;/code&gt;) behaves as the input (&lt;code&gt;n&lt;/code&gt;) grows. &lt;/p&gt;

&lt;p&gt;So the next step in calculating the time-complexity is to eliminate (ignore) all the independent variables (the steps that occur regardless of the input size) from the equation. &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%2Fencrypted-tbn0.gstatic.com%2Fimages%3Fq%3Dtbn%3AANd9GcQsvaXJAgLesoxCXaATzOZ3ESJ15hMrlljtKA%26usqp%3DCAU" 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%2Fencrypted-tbn0.gstatic.com%2Fimages%3Fq%3Dtbn%3AANd9GcQsvaXJAgLesoxCXaATzOZ3ESJ15hMrlljtKA%26usqp%3DCAU" alt="graph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the case of the mathematical function defined earlier (&lt;code&gt;f(n) = 6n + 4&lt;/code&gt;), &lt;code&gt;4&lt;/code&gt; is constant regardless of the input size, however the &lt;code&gt;6n&lt;/code&gt; grows directly as the size of the input grows larger. We refer to these independent operations as &lt;code&gt;4&lt;/code&gt; "initialization constants". &lt;/p&gt;

&lt;h4&gt;
  
  
  Initialization Constants
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Initialization constants&lt;/strong&gt; are the steps of an algorithm that will execute regardless of the size of the input. Including "behind the scene" initialization operations like the virtual machine required for Java.  &lt;/p&gt;

&lt;p&gt;When we remove our initialization constants, our mathematical function for &lt;code&gt;maximum&lt;/code&gt; is stated simply as &lt;code&gt;f(n) = 6n&lt;/code&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We remove initialization constants because these steps are not affected by the input size, or the input values. Instead they are a direct result of the syntax and rules of a high-level programming language. Initialization constant can vary quite dramatically from one language to another, and do not help to clarify how an algorithm responds to increasing input sizes (time complexity). &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Asymptotic behavior refers to an algorithms time complexity with intialization constants removed.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Common Types of Asymptotic Behavior
&lt;/h3&gt;

&lt;p&gt;Given this behavior we can conclude: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A algorithm that does not require any looping will have a complexity of &lt;code&gt;f(n) = 1&lt;/code&gt; since the number of instructions is constant regardless of the size of the input&lt;/li&gt;
&lt;li&gt;A algorithm with a single loop, which loops from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;n&lt;/code&gt; will have a complexity of &lt;code&gt;f(n) = n&lt;/code&gt; since it will perform the instructions inside the loop a constant number of times. &lt;/li&gt;
&lt;li&gt;An algorithm with a nested loop (a loop inside another loop) will have a complexity of &lt;code&gt;f(n) = n²&lt;/code&gt; since the execution time will increases by a factor of &lt;code&gt;n&lt;/code&gt; as the size of the input (&lt;code&gt;n&lt;/code&gt;) increases in size. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now to take everything that we've discussed an put it in the computer science language of Big O is pretty simple. Essentially we've already done it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;f(1)&lt;/code&gt; 

&lt;ul&gt;
&lt;li&gt;represents algorithms with a constant number of instructions &lt;/li&gt;
&lt;li&gt;alternatively, you could say &lt;em&gt;the size of the input does &lt;strong&gt;not&lt;/strong&gt; affect the execution time of the algorithm&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;mathematically, we would represent this as  &lt;code&gt;f(n) = 1&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;is referred to as a "constant time" &lt;/li&gt;
&lt;li&gt;execution time is constant regardless of the size of the input &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;f(n)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;represents an algorithm that's execution time grows in direct proportion to the size of the input &lt;/li&gt;
&lt;li&gt;alternatively, &lt;em&gt;the size of the input directly affects the algorithms execution time&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;mathematically, we would represent this as &lt;code&gt;f(n) = n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;is referred to as a "linear time" &lt;/li&gt;
&lt;li&gt;execution time grows in direct proportion to the input (&lt;code&gt;n&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;f(n²)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;represents an algorithm that executes proportionally at a rate of n squared compared to the input siz &lt;/li&gt;
&lt;li&gt;mathematically, we would represent this as &lt;code&gt;f(n) = n²&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;is referred to as quadratic time &lt;/li&gt;
&lt;li&gt;execution time grows in a quadratic fashion as the input increases. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;... and this would continue as the exponents representing the function grows.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember the key idea here - we are trying to indicate how an algorithm scales as the input to the algorithm increases. Algorithms with a larger θ will execute more slowly than algorithms with a smaller θ&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Asymptotic Complexity &amp;amp; Bounds
&lt;/h3&gt;

&lt;p&gt;It is important to understand that &lt;code&gt;O(n)&lt;/code&gt; (pronounced "Oh of n") and &lt;code&gt;θ(n)&lt;/code&gt; (pronounced "theta of n") do not have the same meaning. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Algorithms expressed in terms of &lt;code&gt;θ&lt;/code&gt; ("theta") are expressed in terms of "tight bounds". &lt;/li&gt;
&lt;li&gt;With tightly bound complexity, an algorithm will consistently execute at the rate expressed by the function of theta. &lt;/li&gt;
&lt;li&gt;Algorithms with complexities expressed in &lt;code&gt;O&lt;/code&gt; are defining the &lt;em&gt;upper bounds&lt;/em&gt; or &lt;em&gt;worse case scenario&lt;/em&gt; for execution steps. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The difference here is between &lt;code&gt;O&lt;/code&gt; and &lt;code&gt;θ&lt;/code&gt; really comes down to how much variability there is in the equation. &lt;/p&gt;

&lt;p&gt;Take &lt;code&gt;Array.find(x =&amp;gt; condition)&lt;/code&gt; function:&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;input&lt;/span&gt; &lt;span class="o"&gt;=&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;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&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;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&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;found&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.find&lt;/code&gt; method iterates through every element in the array, and return the first element that matches the provided logical comparison. &lt;/p&gt;

&lt;p&gt;Under the hood, the &lt;code&gt;.find&lt;/code&gt; function is performing the following:&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="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;collection&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="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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="nf"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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;item&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="kc"&gt;null&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 summary &lt;code&gt;.find&lt;/code&gt;: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;loops through a collection&lt;/li&gt;
&lt;li&gt;With each iteration passing each element as a parameter to the function provided (&lt;code&gt;condition&lt;/code&gt;) &lt;/li&gt;
&lt;li&gt;Depending on the return value of &lt;code&gt;condition(item)&lt;/code&gt; 

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;true&lt;/code&gt; indicates a match has been found and the value is returned&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;false&lt;/code&gt; indicates a non-match, and the loop continues &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If no match has been found by the end of the loop through the collection, then a &lt;code&gt;null&lt;/code&gt; value is returned &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Assuming &lt;code&gt;Array.find&lt;/code&gt; works like the function defined above: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the first element in the collection matches the provided condition to the &lt;code&gt;Array.find&lt;/code&gt; algorithm could complete with only a single iteration. This possibility indicates an algorithm that's &lt;strong&gt;worst-case scenario&lt;/strong&gt; involves looping through the entire collection. 

&lt;ul&gt;
&lt;li&gt;This would be a linear-time-complexity or &lt;code&gt;f(n) = n&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Since this is the &lt;em&gt;upper bound&lt;/em&gt;, we could also express it as &lt;code&gt;o(n)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;There are 3 primary types of bounds in asymptotic functions: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;O(n)&lt;/code&gt; denotes the mathematical function in the worst case scenario 

&lt;ul&gt;
&lt;li&gt;pronounced "big O of n" &lt;/li&gt;
&lt;li&gt;referred to as &lt;em&gt;upper bound&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Worst-case scenario 

&lt;ul&gt;
&lt;li&gt;Most execution steps &lt;/li&gt;
&lt;li&gt;Longest execution time &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Greatest time-complexity&lt;/li&gt;

&lt;li&gt;Least efficient
&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;θ(n)&lt;/code&gt;  denotes the mathematical function of the exact time-complexity of a an asymptotic function given any input. 

&lt;ul&gt;
&lt;li&gt;pronounced "theta of n" &lt;/li&gt;
&lt;li&gt;referred to as &lt;em&gt;tightly-bound&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Exact &lt;/li&gt;
&lt;li&gt;exact execution steps &lt;/li&gt;
&lt;li&gt;consistent response to increasing input size &lt;/li&gt;
&lt;li&gt;consistent efficiency regardless of input values &lt;/li&gt;
&lt;li&gt;consistent complexity regardless of input values&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;Ω(n)&lt;/code&gt; denotes the mathematical function provided the best case scenario (fastest execution)

&lt;ul&gt;
&lt;li&gt;pronounced "omega of n" &lt;/li&gt;
&lt;li&gt;referred to as &lt;em&gt;lower bound&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Best-case &lt;/li&gt;
&lt;li&gt;Least execution steps &lt;/li&gt;
&lt;li&gt;Best-case scenario 

&lt;ul&gt;
&lt;li&gt;Lest execution steps &lt;/li&gt;
&lt;li&gt;Fastest execution time &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Least time-complexity &lt;/li&gt;

&lt;li&gt;Most efficient &lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;This can be broken down further between &lt;code&gt;O&lt;/code&gt; vs. &lt;code&gt;o&lt;/code&gt; and &lt;code&gt;Ω&lt;/code&gt; vs &lt;code&gt;ꞷ&lt;/code&gt; or "big O vs little O" and "big omega vs little omega": &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Notation&lt;/th&gt;
&lt;th&gt;Pronunciation&lt;/th&gt;
&lt;th&gt;Bound&lt;/th&gt;
&lt;th&gt;Numeric Operator&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;O&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"big O"&lt;/td&gt;
&lt;td&gt;tight-upper&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;maximum possible number of execution steps (tightly bound)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;o&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"little O"&lt;/td&gt;
&lt;td&gt;upper&lt;/td&gt;
&lt;td&gt;&lt;code&gt;≤&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;highest possible number of execution steps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;θ&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"theta"&lt;/td&gt;
&lt;td&gt;tight&lt;/td&gt;
&lt;td&gt;&lt;code&gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;exact number of execution steps (tightly bound)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Ω&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"big omega"&lt;/td&gt;
&lt;td&gt;lower&lt;/td&gt;
&lt;td&gt;&lt;code&gt;≥&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;least number of execution steps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ꞷ&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;"little omega"&lt;/td&gt;
&lt;td&gt;tight-lower&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;least number of execution steps (tightly bound)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Tight refers to how accurate the mathematical function representing the asymptotic complexity is for a given algorithm. &lt;/p&gt;

&lt;h2&gt;
  
  
  Learn by example
&lt;/h2&gt;

&lt;p&gt;The best way to learn is to do it, so join me in taking apart a sorting algorithm and examining its asymptotic time-complexity. &lt;/p&gt;

&lt;h3&gt;
  
  
  Sorting Algorithms
&lt;/h3&gt;

&lt;p&gt;Sorting Algorithms are algorithms that sort a collection of items by a specific attribute. These algorithms are prevalent in nearly every modern application. &lt;/p&gt;

&lt;p&gt;Examples of sorting algorithms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sorting files in Finder/Windows explorer (Alphabetically, by size, by file type, etc.) &lt;/li&gt;
&lt;li&gt;Sorting shopping results (by price, by rating, by sales, etc.)&lt;/li&gt;
&lt;li&gt;Sorting contacts (by first name, by last name, by date of birth, by workplace, etc.)&lt;/li&gt;
&lt;li&gt;Sorting job posting (by post date, by industry, by company, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are numerous realistic and practical application for sorting algorithms, and like many algorithms, there is multiple ways to tackle the same problem. &lt;/p&gt;

&lt;h4&gt;
  
  
  Selection Sort
&lt;/h4&gt;

&lt;p&gt;A "selection sort" algorithm takes an input, and sorts it according to some arbitrary value. Take a number selection sorting algorithm like the one 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;function&lt;/span&gt; &lt;span class="nf"&gt;selectionSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&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;inputArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;arr&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;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&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;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inputArr&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;let&lt;/span&gt; &lt;span class="nx"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;inputArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inputArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                &lt;span class="nx"&gt;minIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; 
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;minIndex&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="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic sorting function (&lt;code&gt;selectionSort&lt;/code&gt;) defined above sorts values from smallest to largest by: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; copying the input (to avoid modifying by reference) &lt;/li&gt;
&lt;li&gt; iterating through the copied array (&lt;code&gt;inputArr&lt;/code&gt;) and removing the smallest element (&lt;code&gt;min&lt;/code&gt;) from the input array amd adding it to the &lt;code&gt;output&lt;/code&gt; array. &lt;/li&gt;
&lt;li&gt;repeating step 2 until the &lt;code&gt;inputArr&lt;/code&gt; has no elements left&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Challenge&lt;/strong&gt; Go ahead and see if you can calculate the complexity (in big O notation and θ notation) yourself. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Calculating Selection Sort Time Complexity
&lt;/h5&gt;

&lt;p&gt;Recall, we mathematically determine complexity by: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;determining the individual steps executed by the CPU 

&lt;ol&gt;
&lt;li&gt;create &lt;code&gt;inputArr&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;copy &lt;code&gt;arr&lt;/code&gt; into &lt;code&gt;inputArr&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;create &lt;code&gt;output&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;initialize &lt;code&gt;output = []&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;create loop

&lt;ol&gt;
&lt;li&gt;create &lt;code&gt;i&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;assign &lt;code&gt;i = 1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;lookup &lt;code&gt;inputArr[i]&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;compare &lt;code&gt;inputArr[i] &amp;lt; min&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;optionally, assign &lt;code&gt;min = inputArr[i]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;optionally, assign &lt;code&gt;minIndex = i&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;increment &lt;code&gt;i&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;check condition (&lt;code&gt;i &amp;lt; inputArr.length&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;repeat loop while &lt;code&gt;i &amp;lt; inputArr.length&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;remove &lt;code&gt;min&lt;/code&gt; element from &lt;code&gt;inputArr&lt;/code&gt; &lt;/li&gt;

&lt;li&gt;add it to minimum value to &lt;code&gt;output&lt;/code&gt; array &lt;/li&gt;

&lt;li&gt;check &lt;code&gt;inputArr.length &amp;gt; 0&lt;/code&gt;
&lt;/li&gt;

&lt;li&gt;optionally, loop &lt;/li&gt;

&lt;/ol&gt;

&lt;/li&gt;

&lt;li&gt;determine which steps are dependent steps

&lt;ul&gt;
&lt;li&gt;dependent 

&lt;ul&gt;
&lt;li&gt;lookup &lt;code&gt;inputArr[i]&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;inside loop:

&lt;ul&gt;
&lt;li&gt;compare &lt;code&gt;inputArr[i] &amp;lt; min&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;optionally, assign &lt;code&gt;min = inputArr[i]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;optionally, assign &lt;code&gt;minIndex = i&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;increment &lt;code&gt;i&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;repeat loop while &lt;code&gt;i &amp;lt; inputArr.length&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;remove &lt;code&gt;min&lt;/code&gt; element from &lt;code&gt;inputArr&lt;/code&gt; &lt;/li&gt;

&lt;li&gt;add it to minimum value to &lt;code&gt;output&lt;/code&gt; array &lt;/li&gt;

&lt;li&gt;check &lt;code&gt;inputArr.length &amp;gt; 0&lt;/code&gt;
&lt;/li&gt;

&lt;li&gt;optionally, loop &lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;independent 

&lt;ul&gt;
&lt;li&gt;create &lt;code&gt;inputArr&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;copy &lt;code&gt;arr&lt;/code&gt; into &lt;code&gt;inputArr&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;create &lt;code&gt;output&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;initialize &lt;code&gt;output = []&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;create loop

&lt;ul&gt;
&lt;li&gt;create &lt;code&gt;i&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;assign &lt;code&gt;i = 1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;determine the worst-case scenario 

&lt;ul&gt;
&lt;li&gt;the execution of both loops (&lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt;) are both directly impacted by the size of the input, and unaffected (in terms of execution time) by the values of the input. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;remove initialization constants &lt;/li&gt;

&lt;/ol&gt;

&lt;h4&gt;
  
  
  Walking through the Selection Sort
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop that states we continue looping until every element from &lt;code&gt;inputArr&lt;/code&gt; has been removed. Then with each iteration through the &lt;code&gt;while&lt;/code&gt; loop, a nested &lt;code&gt;for&lt;/code&gt; loop through the same collection of elements is executed to find the smallest value. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the first pass &lt;code&gt;inputArr&lt;/code&gt;, will determine &lt;code&gt;11&lt;/code&gt; is the smallest element in the &lt;code&gt;inputArr&lt;/code&gt; and remove it, placing it in &lt;code&gt;output&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The second pass through &lt;code&gt;inputArr&lt;/code&gt; will determine &lt;code&gt;12&lt;/code&gt; is the smallest value, and remove it from &lt;code&gt;inputArr&lt;/code&gt;, placing the value in &lt;code&gt;output&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;The third pass through &lt;code&gt;inputArr&lt;/code&gt; , will determine &lt;code&gt;22&lt;/code&gt; is the smallest value, remove it from &lt;code&gt;inputArr&lt;/code&gt;, placing the value in &lt;code&gt;output&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The fourth pass through &lt;code&gt;inputArr&lt;/code&gt; , will determine &lt;code&gt;25&lt;/code&gt; is the smallest value, remove it from &lt;code&gt;inputArr&lt;/code&gt;, placing the value in &lt;code&gt;output&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The fifth pass through &lt;code&gt;inputArr&lt;/code&gt; , will determine &lt;code&gt;64&lt;/code&gt; is the smallest value, remove it from &lt;code&gt;inputArr&lt;/code&gt;, placing the value in &lt;code&gt;output&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The sixth pass through &lt;code&gt;inputArr&lt;/code&gt; , will determine that there are no more elements within &lt;code&gt;inputArr&lt;/code&gt;, and exit the loop. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;output&lt;/code&gt; will be returned with the elements sorted from smallest to largest &lt;code&gt;[11, 12, 22, 25, 64]&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall we can summarize the behavior of an input &lt;code&gt;[11, 25, 12, 22, 65]&lt;/code&gt; with the following table: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;loop&lt;/th&gt;
&lt;th&gt;&lt;code&gt;output&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;inputArr&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;min&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;inputArr.length&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;output.length&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[11, 25, 12, 22, 64]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;11&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[11]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[25, 12, 22, 64]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;12&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[11, 12]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[25, 22, 64]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;22&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[11, 12, 22]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[25, 64]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;25&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[11, 12, 22, 25]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[64]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[11, 12, 22, 25, 64]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Overall the &lt;code&gt;selectionSort&lt;/code&gt; function

&lt;ul&gt;
&lt;li&gt;Is comprised of two (2) loops &lt;/li&gt;
&lt;li&gt;The outer-most loop executes &lt;code&gt;n&lt;/code&gt; times &lt;/li&gt;
&lt;li&gt;the inner loop executes &lt;code&gt;n&lt;/code&gt; times &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In the &lt;em&gt;worse case scenario&lt;/em&gt; both loops are executed &lt;code&gt;5&lt;/code&gt; times for an input with &lt;code&gt;5&lt;/code&gt; elements. In terms of a mathematical function, we could represent this as &lt;code&gt;f(n) = n * n&lt;/code&gt; or &lt;code&gt;f(n) = n²&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Since the there is no scenario in which this algorithm will execute faster than &lt;code&gt;f(n) = n²&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Cheers 🥂 to you if you got that 👍&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The sorting algorithm outlined above is a &lt;strong&gt;highly inefficient&lt;/strong&gt; method of sorting 🧠 If you are up for a challenge, see if you can think of a more efficient algorithm for sorting numbers 🧠 &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Recursion
&lt;/h2&gt;

&lt;p&gt;Recursion refers to the process in which a function calls itself to determine the end result. Recursion occurs very commonly, you'll find recursive functions in: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;calculating the number of &lt;code&gt;.jpeg&lt;/code&gt; files in a folder 

&lt;ul&gt;
&lt;li&gt;every folder inside the root folder must also run the same function &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;calculating the size of a folder 

&lt;ul&gt;
&lt;li&gt;every folder inside the folder must also be calculated &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;factorials 

&lt;ul&gt;
&lt;li&gt;to find the factorial of &lt;code&gt;5&lt;/code&gt;, you first find the factorials of &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;... and many more. &lt;/p&gt;

&lt;h3&gt;
  
  
  Recursive Complexity
&lt;/h3&gt;

&lt;p&gt;One of the simplest examples of a recursive algorithm is factorials. A factorial is a defined as the product of every integer between a number and 0. So determining the factorials of &lt;code&gt;1&lt;/code&gt; through &lt;code&gt;5&lt;/code&gt; would look like: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Math Equation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(1) = 1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(2) = 2 * 1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;3&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(3) = 3 * 2 * 1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;4&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(4) = 4 * 3 * 2 * 1&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Functionally, we would calculate a factorial using recursion.&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="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Given the above function, can you determine the asymptotic complexity? &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;factorial(number)&lt;/code&gt; function above does not have any loops. Previously algorithms without any loops had a constant runtime, however with the &lt;code&gt;factorial(number)&lt;/code&gt; function, this is not the case. As you can imagine the greater the input value, the greater the number of execution steps, and the simple recursive function above would have a &lt;em&gt;linear complexity&lt;/em&gt; or complexity of &lt;code&gt;θ(n)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This begs the question: &lt;em&gt;Do recursive functions always have linear complexities?&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Another recursive function referred to as "binary search" is slightly more complicated. In binary search algorithms, the function receives a collection of &lt;em&gt;sorted&lt;/em&gt; values. Since the input to the function is sorted, some optimizations can be made to more quickly find the desired value. &lt;/p&gt;

&lt;p&gt;Imagine the function:&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="nf"&gt;findIndexFromSorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&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;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 
    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;end&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;middle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&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;// make sure we always have a valid middle value &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;middle&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;middle&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; 

        &lt;span class="c1"&gt;// see if middle element is target &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;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;middle&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&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;middle&lt;/span&gt;  

        &lt;span class="c1"&gt;// check right side &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;target&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;middle&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;middle&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 
            &lt;span class="k"&gt;continue&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// check left side &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;target&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;middle&lt;/span&gt;&lt;span class="p"&gt;]){&lt;/span&gt;
            &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;middle&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;continue&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 begin by checking the value at the middle of the &lt;code&gt;array&lt;/code&gt; to see if it is the &lt;code&gt;target&lt;/code&gt;. If it is, the function has found the &lt;code&gt;target&lt;/code&gt; and should return the index. If not, we see if the value of the middle of the &lt;code&gt;array&lt;/code&gt; is less than or greater than the &lt;code&gt;target&lt;/code&gt;. Then we change our search parameters (&lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt;) to inspect the right-side (higher numbers) or left-side (lower number) of &lt;code&gt;array&lt;/code&gt;. With each iteration through the &lt;code&gt;while&lt;/code&gt; loop, &lt;code&gt;n&lt;/code&gt; or the number elements (relevant input) shrinks. &lt;/p&gt;

&lt;p&gt;So with each iteration through the recursive algorithm, the number of elements inspected shrinks by a factor of 2. Or as a mathematical function:  &lt;code&gt;f(n) = n / (2 ^ i)&lt;/code&gt; / &lt;code&gt;f(n) = n / 2ⁱ&lt;/code&gt; where &lt;code&gt;i&lt;/code&gt; represents the iteration.&lt;/p&gt;

&lt;p&gt;Let's represent the iteration with &lt;code&gt;i&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Iteration&lt;/th&gt;
&lt;th&gt;Number of relevant elements&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;i&lt;/code&gt; function&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;n&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;n / 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2²&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;n / 4&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2³&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;n / 8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2⁴&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;n / 16&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2⁵&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;But what is the value of &lt;code&gt;i&lt;/code&gt; for a given input &lt;code&gt;n&lt;/code&gt;?&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;n&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;i&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;n&lt;/code&gt; function&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;n&lt;/code&gt; value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2¹&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(1) = 1/2¹ = 1/2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.5&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2²&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(2) = 2/2² = 2/4&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.25&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2³&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(3) = 3/2³ = 3/8&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.375&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2⁴&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(4) = 4/2⁴ = 4/16&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.25&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2⁵&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(5) = 5/2⁵ = 5/32&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.15625&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;&lt;code&gt;i = n / 2⁶&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f(6) = 6/64 = 3/32&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.09375&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's may seem kinda messy, but given this information we can solve for &lt;code&gt;n&lt;/code&gt; using  &lt;code&gt;1 = n/2ⁱ&lt;/code&gt;. Multiply both side of the equation by &lt;code&gt;2&lt;/code&gt;, we get &lt;code&gt;2ⁱ = n&lt;/code&gt; or &lt;code&gt;n = 2ⁱ&lt;/code&gt;. Recall back to algebra, how do you define a variable in terms of the exponent it was raised to? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Answer: Logarithms &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Logarithms and Complexity
&lt;/h3&gt;

&lt;p&gt;A logarithm answers the question "How many of this number do we multiply to get that number?" &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the base of the logarithm refers to the number being raised to an exponent &lt;/li&gt;
&lt;li&gt;the number in parenthesis specifies the desired value of the base raised to that number &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Example: &lt;br&gt;
|Sentence | Logarithm | &lt;br&gt;
|:--------|:----| &lt;br&gt;
| What exponent does &lt;code&gt;2&lt;/code&gt; need to be raised to, in order to get the value of &lt;code&gt;8&lt;/code&gt;? | &lt;code&gt;log₂(8)&lt;/code&gt; | &lt;br&gt;
| What exponent does &lt;code&gt;2&lt;/code&gt; need to be raised to, in order to get the value of &lt;code&gt;256&lt;/code&gt;? | &lt;code&gt;log₂(256)&lt;/code&gt; | &lt;/p&gt;

&lt;p&gt;Using logarithms, we could define the binary search algorithm above as: &lt;code&gt;f(n) = n / 2 * log₂(n)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This introduces the last type of asymptotic complexity, logarithmic complexity. In Logarithmic asymptotic algorithms which the algorithms, as the input &lt;code&gt;n&lt;/code&gt; increases, the efficiency of the algorithm increases. &lt;/p&gt;

&lt;p&gt;We can summarize these various algorithms with a chart to demonstrate how the algorithm responds to increasing inputs: &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%2Fdanielmiessler.com%2Fimages%2Fbig-o-chart-tutorial-bazar-aymptotic-notations-1.png.webp" 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%2Fdanielmiessler.com%2Fimages%2Fbig-o-chart-tutorial-bazar-aymptotic-notations-1.png.webp" alt="Big O chart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  All Together
&lt;/h2&gt;

&lt;p&gt;In short, here are the general rules regarding Big O and calculating algorithmic complexity &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Algorithms that execute faster with a larger input, will often execute faster will smaller inputs as well. &lt;/li&gt;
&lt;li&gt;Many simple algorithms can be analyzed by counting the loops

&lt;ul&gt;
&lt;li&gt;An algorithm with a single loop, will generally have a &lt;em&gt;linear&lt;/em&gt; complexity. &lt;/li&gt;
&lt;li&gt;Every nested loop will increase the complexity beyond &lt;code&gt;f(n) = n&lt;/code&gt; by a factor of n

&lt;ul&gt;
&lt;li&gt;a single loop will have a complexity of &lt;code&gt;f(n) = n&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;a nested loop with will have a complexity of &lt;code&gt;f(n) = n²&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;a nested loop nested inside another nested loop (3 loops) will have a complexity of &lt;code&gt;f(n) = n³&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Given an algorithm with a series of loops (not nested loops), the slowest loop (e.g. the loop with the most computational steps) will determine the asymptotic behavior of the algorithm.

&lt;ul&gt;
&lt;li&gt;Two nested loops followed by a single loop is asymptotically the same as the nested loops since the nested loops will have a greater impact on execution time as the input grows. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;While all the symbols &lt;code&gt;O&lt;/code&gt;, &lt;code&gt;o&lt;/code&gt;, &lt;code&gt;Ω&lt;/code&gt;, &lt;code&gt;ω&lt;/code&gt; and &lt;code&gt;Θ&lt;/code&gt; are useful at times, O is the one used more commonly, as it's easier to determine than Θ and more practically useful than &lt;code&gt;Ω&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;It is easier to determine &lt;code&gt;O&lt;/code&gt; complexity than &lt;code&gt;θ&lt;/code&gt; complexity or &lt;code&gt;Ω&lt;/code&gt; complexity &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;O&lt;/code&gt; complexity provides the best insight into performance, since it assumes the worst-case scenario &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Logarithmic complexity generally occurs in recursive function, and represents an algorithm that gets more efficient as the input increases. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dionysis Zindros -  &lt;a href="http://discrete.gr/complexity/" rel="noopener noreferrer"&gt;A Gentle Introduction to Algorithm Complexity Analysis&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Big O Cheat Sheet - &lt;a href="https://www.bigocheatsheet.com/" rel="noopener noreferrer"&gt;Know Thy Complexities!&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>What are microservices, and why should you care?</title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Mon, 23 Nov 2020 01:54:23 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/what-are-microservices-and-why-should-you-care-21na</link>
      <guid>https://dev.to/jasonnordheim/what-are-microservices-and-why-should-you-care-21na</guid>
      <description>&lt;h1&gt;
  
  
  What are microservices
&lt;/h1&gt;

&lt;p&gt;As a software engineer or web developer, you've probably heard the term "microservice" before, but for less seasoned developers, the concept of microservices is likely an unfamiliar and seemingly daunting term. &lt;/p&gt;

&lt;p&gt;The goal here is to break it down - explain what it is and why you should care. &lt;/p&gt;

&lt;h2&gt;
  
  
  Monoliths and the "monolithic server"
&lt;/h2&gt;

&lt;p&gt;Before we can understand microservices, it's important to understand the traditional architecture used for building applications, commonly referred to as the "monolithic server". &lt;/p&gt;

&lt;p&gt;The traditional full-stack applications is engineered into two primary parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The client (front-end) &lt;/li&gt;
&lt;li&gt;The server (back-end)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This segments the presentation and of information and the implementation of business logic into the front-end, while the backend supports those features and enables persistence of data. &lt;/p&gt;

&lt;h3&gt;
  
  
  Monoliths
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.merriam-webster.com/dictionary/monolithic" rel="noopener noreferrer"&gt;Meriam-webster&lt;/a&gt; defines "monolith" as: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;"a single great stone often in the form of an obelisk or column" &lt;/li&gt;
&lt;li&gt;"a massive structure" &lt;/li&gt;
&lt;li&gt;"an organized whole that acts as a single unified powerful or influential force" &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While not specific to software, the analogy is the same. &lt;/p&gt;

&lt;p&gt;Typically the monolithic architecture is referencing the back-end (server) that supports data persistence and handles all the logic pertaining to creating, modifying, deleting, and retrieving information related to an application (or applications). &lt;/p&gt;

&lt;p&gt;With Monolithic servers, every feature is run on the &lt;em&gt;same&lt;/em&gt; system (computer/server) and is part of the &lt;em&gt;same&lt;/em&gt; codebase and the &lt;em&gt;same&lt;/em&gt; database. &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%2Fmiro.medium.com%2Fmax%2F958%2F1%2A6fbTgnuP51xy0eZfQESYRw.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%2Fmiro.medium.com%2Fmax%2F958%2F1%2A6fbTgnuP51xy0eZfQESYRw.png" alt="monolithic server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In short → a monolithic application, implements every feature of the application.&lt;/strong&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Microservice Applications
&lt;/h3&gt;

&lt;p&gt;Microservices are design around the applications features.With each microservice operating independently from each other, but working together to support all the functionality of the application. &lt;/p&gt;

&lt;p&gt;In the microservice application architecture, each feature has &lt;em&gt;its own small service&lt;/em&gt; called a "microservice" with no dependencies on the other services or features offered as part of the application.  &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%2Fmiro.medium.com%2Fmax%2F1152%2F1%2AF3LB7EdCNPKOF0Ii3WTwzA.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%2Fmiro.medium.com%2Fmax%2F1152%2F1%2AF3LB7EdCNPKOF0Ii3WTwzA.png" alt="microservice architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Microservices vs. Monoliths
&lt;/h3&gt;

&lt;p&gt;If each approach was taken to the extreme... &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;monolithic&lt;/strong&gt; server architecture would consist of a single server responsible for all the features, functions and middleware of the application such as: 

&lt;ul&gt;
&lt;li&gt;routing &lt;/li&gt;
&lt;li&gt;authentication&lt;/li&gt;
&lt;li&gt;business logic &lt;/li&gt;
&lt;li&gt;database interactions&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;a &lt;strong&gt;microservice&lt;/strong&gt; based architecture would consist of multiple servers. Each server supporting all the functionality associated with a single feature of the application, and each microservice having independent routing, independent code bases, independent business logic, independent authentication, and independent data management.
&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implications
&lt;/h3&gt;

&lt;p&gt;The important take-away is that each microservice is &lt;strong&gt;self-contained&lt;/strong&gt;, without any dependence on the other microservices. Using more technical terms, the features assocaited with the application in a monolithic application architecture are &lt;a href="https://stackoverflow.com/questions/2832017/what-is-the-difference-between-loose-coupling-and-tight-coupling-in-the-object-o#:~:text=Tight%20coupling%20is%20when%20a,than%20having%20its%20own%20class." rel="noopener noreferrer"&gt;&lt;em&gt;tightly coupled&lt;/em&gt;&lt;/a&gt; together. &lt;/p&gt;

&lt;p&gt;This results in an application and development workflow that tightly ties together different developers from different teams, working on different features to a single shared code base, running the same shared middleware, and accessing the same shared database. In short, the application's features are highly dependent upon each other and a single feature breaking could result in the entire application failing. &lt;/p&gt;

&lt;p&gt;Microservices seek to address this architectural issue, by making independent servers or services ("microservices") that &lt;strong&gt;only&lt;/strong&gt; are responsible for a single feature of the application. &lt;/p&gt;

&lt;p&gt;In a microservice based architecture, routing, authentication, business logic, and database interactions, are all wrapped into small independent pieces ("microservices") and have can operate on their own. With microservices, no feature of the application is dependent on shared code or servers. &lt;/p&gt;

&lt;p&gt;This is similar to the idea behind "thin vertical slices" ([here] is an explanation of thin vertical slices if you are not familiar with the approach) commonly embraced in agile methodologies as well as the &lt;a href="https://rb.gy/ihrzdu" rel="noopener noreferrer"&gt;single responsibility principle&lt;/a&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  The problem with the monolithic approach
&lt;/h4&gt;

&lt;p&gt;If a hardware failure occurs in a monolithic architecture, the entire application can be broken, since all of the features, routing, middleware, business logic, and database interactions all occur on the same system. &lt;/p&gt;

&lt;p&gt;Similarly, since every feature is running on a single server, that server is using a large code base, that is shared between multiple developer/engineers and teams. As a result each feature is highly dependent on the others, and any addition or modification to the functionality of the application means modifying the code for the entire application. &lt;/p&gt;

&lt;h4&gt;
  
  
  Drawbacks of Microservices
&lt;/h4&gt;

&lt;p&gt;The microservice approach sounds great so far, but there are some drawbacks.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Communication between services

&lt;ul&gt;
&lt;li&gt;most applications require information from one feature in order to implement another feature. Since the microservice makes each feature independent of each other, data is managed independently and is not inherently available to other parts of the application. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Greater resource requirements

&lt;ul&gt;
&lt;li&gt;Data  management 

&lt;ul&gt;
&lt;li&gt;Data Duplication: 

&lt;ul&gt;
&lt;li&gt;to avoid being reliant on other microservices, most applications implemented with a microservice architecture duplicate data, with each service having its own copy of the information it needs to operate.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Data model associations

&lt;ul&gt;
&lt;li&gt;associating information from one microservice to another is a complex process, since each service has no direct access to the database pertaining to another feature of the application. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;More computing resources:

&lt;ul&gt;
&lt;li&gt;in order to be truly independent from the other microservices, each microservice must run on a server/computer that is only used for that feature.&lt;/li&gt;
&lt;li&gt;for every feature to work in microservice architecture, multiple servers must all be running at the same time (compared to a single server in the monolithic application architecture)&lt;/li&gt;
&lt;li&gt;development teams will often duplicate efforts implemented in other microservices, writing code that is very similar (if not the same) to the code implementing similar functionality in a different microservice. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;More complex testing &amp;amp; debugging 

&lt;ul&gt;
&lt;li&gt;integration testing involving multiple features, is more complicated under the microservice based approach. &lt;/li&gt;
&lt;li&gt;logs for each microservice (feature) are on different servers, and could be in completely different formats. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Still unclear?
&lt;/h2&gt;

&lt;p&gt;Hopefully the difference between the fundamental paradigms behind application architecture (microservice vs. monolith) is a little clearer. However, I firmly believe the best approach to understanding anything in software development is to build it. &lt;/p&gt;

&lt;p&gt;If that's of interest to you, look out for a follow-up tutorial demonstrating the microservice architecture. &lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>architecture</category>
      <category>microservices</category>
    </item>
    <item>
      <title>GraphQL vs. REST  </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Mon, 16 Nov 2020 02:00:27 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/graphql-vs-rest-4mkl</link>
      <guid>https://dev.to/jasonnordheim/graphql-vs-rest-4mkl</guid>
      <description>&lt;h1&gt;
  
  
  What is GraphQL?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="http://www.graphql.org/"&gt;GraphQL&lt;/a&gt; is an API standard  as an &lt;a href="https://facebook.github.io/react/blog/2015/02/20/introducing-relay-and-graphql.html"&gt;open-source project&lt;/a&gt; by &lt;a href="https://www.facebook.com/"&gt;Facebook&lt;/a&gt; to be an alternative to REST.  &lt;/p&gt;

&lt;p&gt;Instead of many endpoints that return fixed data-structures, &lt;a href="http://www.graphql.org/"&gt;GraphQL&lt;/a&gt; only exposes a single API endpoint that responds precisely with the data specified in the HTTP request. &lt;/p&gt;

&lt;h2&gt;
  
  
  More efficient how?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Only the necessary data is transferred over the network, resulting in minimization of network consumption.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;GraphQL exposes a single endpoint that responds to HTTP requests for data (GraphQL requests), and evaluates the query and and replies to HTTP request with only the data specified. No more, no less. Unlike REST APIs, which have several endpoints and respond very structured responses to requests, often returning excess information and/or requiring multiple HTTP requests to piece together objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  REST vs. GraphQL
&lt;/h3&gt;

&lt;p&gt;There is a blog application that has the following objects and relationships: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;user&lt;/code&gt; - represents a user model. 

&lt;ul&gt;
&lt;li&gt;properties: 

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;user.id&lt;/code&gt; &lt;em&gt;integer&lt;/em&gt; (primary key, auto-incrementing)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user.firstName&lt;/code&gt; &lt;em&gt;string&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user.lastName&lt;/code&gt; &lt;em&gt;string&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user.password_digest&lt;/code&gt; &lt;em&gt;string&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user.dob&lt;/code&gt; &lt;em&gt;date-time&lt;/em&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;relationships: 

&lt;ul&gt;
&lt;li&gt;has-many &lt;code&gt;followers&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;follow&lt;/code&gt; - represents a subscription to another user's blog posts 

&lt;ul&gt;
&lt;li&gt;properties: 

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;follow.id&lt;/code&gt; &lt;em&gt;integer&lt;/em&gt; (primary key, auto-incrementing)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;follow.subscriber&lt;/code&gt; &lt;em&gt;integer&lt;/em&gt; (foreign key, user who "follows" another user)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;follow.user&lt;/code&gt; &lt;em&gt;integer&lt;/em&gt; (foreign key, user that is "followed")&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;relationships: 

&lt;ul&gt;
&lt;li&gt;has-many &lt;code&gt;users&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;belongs-to &lt;code&gt;users&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;


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

&lt;h4&gt;
  
  
  REST Approach
&lt;/h4&gt;

&lt;p&gt;A REST API for this blog application would have a &lt;code&gt;/users&lt;/code&gt;" endpoint and a &lt;code&gt;/follows&lt;/code&gt; endpoint with the following routes: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Users REST Endpoint&lt;/strong&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;INDEX - GET &lt;code&gt;/users&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;SHOW - GET &lt;code&gt;/users/:id&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;CREATE - POST &lt;code&gt;/users/&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;body (JSON):
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dob&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;UPDATE - PATCH &lt;code&gt;/users/:id&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;body (JSON):
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dob&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DELETE - DELETE &lt;code&gt;/users/:id&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Follows REST Endpoint&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The endpoint &lt;code&gt;/follows/&lt;/code&gt; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;INDEX - GET &lt;code&gt;/follows&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;SHOW - GET &lt;code&gt;/follows/:id&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;CREATE - POST &lt;code&gt;/follows&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;body (JSON):
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;follow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;subscriber&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;UPDATE - PATCH &lt;code&gt;/follows/:id&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;body (JSON):
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;follow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;subscriber&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DELETE - DELETE &lt;code&gt;/follows/:id&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In order to get all the users that follow the user currently logged in (assuming basic setup and no nested routes) the following logic would be executed: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Front-end (client) executes a HTTP&lt;code&gt;GET&lt;/code&gt; request to the &lt;code&gt;/follows&lt;/code&gt; endpoint (index)&lt;/li&gt;
&lt;li&gt;REST API (backend/server) queries the all the &lt;code&gt;follow&lt;/code&gt; records in the database, and encodes them as JSON in the body of the HTTP response (status &lt;code&gt;200&lt;/code&gt; &lt;em&gt;success&lt;/em&gt;) returned to the client. &lt;/li&gt;
&lt;li&gt;Front-end (client) decodes HTTP request and parses the JSON data from the body creating an array in memory with all the &lt;code&gt;follow&lt;/code&gt; objects returned from the &lt;code&gt;GET&lt;/code&gt; request. &lt;/li&gt;
&lt;li&gt;Front-end (client) executes a reduce function, filtering down the array of &lt;code&gt;follow&lt;/code&gt; objects so that it only includes the ones whose &lt;code&gt;user&lt;/code&gt; field matches the &lt;code&gt;id&lt;/code&gt; field of the authenticated user. &lt;/li&gt;
&lt;li&gt;Front-end (client) executes a HTTP &lt;code&gt;GET&lt;/code&gt; request to &lt;code&gt;/users&lt;/code&gt; endpoint (index)&lt;/li&gt;
&lt;li&gt;REST API (backend/server) queries database for all the &lt;code&gt;user&lt;/code&gt; records in the database, and encodes them as JSON data in the body of an HTTP response (status &lt;code&gt;200&lt;/code&gt; &lt;em&gt;success&lt;/em&gt;) returned to the client. &lt;/li&gt;
&lt;li&gt;Front-end (client) creates an array to store the associated &lt;code&gt;user&lt;/code&gt; objects, then loops over the &lt;code&gt;user&lt;/code&gt; (foreign key) field filtered from the &lt;code&gt;GET&lt;/code&gt; request to the follow &lt;code&gt;/follow&lt;/code&gt; adding the user's with the corresponding &lt;code&gt;ids&lt;/code&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  GraphQL Approach
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Front-end (client) sends GraphQL query to server: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;body:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;Follow&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;Followers&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="w"&gt; 
            &lt;/span&gt;&lt;span class="n"&gt;LastName&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GraphQL API (backend/server) queries the database for all the records of &lt;code&gt;follows&lt;/code&gt; the whose &lt;code&gt;user&lt;/code&gt; propety is  &lt;code&gt;23&lt;/code&gt;, then takes those foreign keys (&lt;code&gt;follow.user&lt;/code&gt;) to query the database for all the &lt;code&gt;user&lt;/code&gt; records contained in the result of the first query and returns an HTTP response to the client containing an array of objects each with &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; fields of &lt;strong&gt;only&lt;/strong&gt; the &lt;code&gt;user&lt;/code&gt; models with matching &lt;code&gt;follow.user&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  The difference
&lt;/h4&gt;

&lt;p&gt;REST APIs are built with 5-7 routes for each of the endpoints. Each endpoint represents a collection of records in a database (in a "table" in relational databases), and return either a single record (object) or all of the records (objects). The client (frontend) is then responsible for parsing the responses, filtering the data (if needed) and making additional subsequent requests for more information (if needed), which returns individual records (objects) or all records associated with an endpoint (or "table") in strictly defined data structures which could result in repeating the filtering of data, and making additional/supplemental HTTP requests over and over until all the data is necessary information is gathered. &lt;/p&gt;

&lt;p&gt;GraphQL APIs reduce the number of HTTP requests/responses by enabling the developer to "declare" exactly the information needed, and allow the API to join, filter and sort data in a single HTTP request (similar to filtering done in SQL databases using the &lt;code&gt;where&lt;/code&gt; operator). &lt;/p&gt;

&lt;p&gt;Instead of providing excess records (e.g. REST &lt;code&gt;INDEX&lt;/code&gt;) The HTTP response generated from the GraphQL server contains only the records that match the conditions specified in by the client, and each object only has the fields specified, stripping out additional fields and objects, and (in some cases) dramatically reducing the size of the HTTP response. &lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR;
&lt;/h2&gt;

&lt;p&gt;GraphQL takes a declarative, SQL-&lt;em&gt;like&lt;/em&gt; approach to data fetching (APIs), and increases application efficiency and flexibility by enabling client (frontend) request to &lt;em&gt;declare&lt;/em&gt; the required information, then querying,  filtering, ordering, and joining records/objects (like a SQL statement) and returning &lt;strong&gt;only&lt;/strong&gt; the &lt;em&gt;declared&lt;/em&gt; records and fields. No extraneous records, and only the fields &lt;em&gt;declared&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;This approach allows GraphQL to reduce both the number/frequency of HTTP request and size of HTTP requests.   &lt;/p&gt;

</description>
      <category>graphql</category>
      <category>codenewbie</category>
      <category>database</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Debounce, Performance and React </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Fri, 06 Nov 2020 02:43:39 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/debounce-performance-and-react-4de1</link>
      <guid>https://dev.to/jasonnordheim/debounce-performance-and-react-4de1</guid>
      <description>&lt;h1&gt;
  
  
  Debounce, Performance and React
&lt;/h1&gt;

&lt;p&gt;While "debounce" is a more broad software development pattern, this article will focus on debounce implemented in &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt;. &lt;/p&gt;

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

&lt;p&gt;Debounce is a way of &lt;em&gt;delaying some peice of code, until a specified time&lt;/em&gt; to avoid unnecessary CPU cycles and increase software performance. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why does it matter?
&lt;/h2&gt;

&lt;p&gt;Performance.&lt;/p&gt;

&lt;p&gt;Debounce allows us to increase application performance by limit the frequency of "expensive operations". &lt;/p&gt;

&lt;p&gt;Specifically, operations that require significant resources (CPU, Memory, Disk) to execute. "Expensive operations" or  slow application load times, cause freezes and delays in the user-interface, and require more of your network than is ultimately necessary.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding through example
&lt;/h3&gt;

&lt;p&gt;Debounce makes the most sense in context. &lt;/p&gt;

&lt;p&gt;Imagine we have a simple movie search application:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// to simplify HTTP request &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./App.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Root Application Component
 */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// store/update search text &amp;amp; api request results in state&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;search&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSearch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Event handler for clicking search
   * @param {event} e
   */&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleSearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// no refresh&lt;/span&gt;
    &lt;span class="k"&gt;try&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;searchResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;searchAny&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchResults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="nx"&gt;Movies&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/header&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Search&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setSearch&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onSearch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSearch&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Movies&lt;/span&gt; &lt;span class="nx"&gt;searchResults&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/main&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Movie Card component
 * @param {{movie}} props with movie object containing movie data
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MovieCard&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;movie&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;movieCard&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h4&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;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h4&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Poster&lt;/span&gt; &lt;span class="o"&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="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Container to hold all the movies
 * @param {searchResults} param0
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Movies&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;searchResults&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;movies&lt;/span&gt;&lt;span class="dl"&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;searchResults&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;searchResults&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="nx"&gt;searchResults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MovieCard&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;imdbID&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="cm"&gt;/**
 * Search bar
 * @param {{string, function, function}} props
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Search&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="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onSearch&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;search&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Movie name...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&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="nx"&gt;setValue&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="nx"&gt;currentTarget&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="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onSearch&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;Search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;In the sample React application outlined above, HTTP request (the "expensive operation") containing the search string (title of the movie) to the &lt;a href="http://www.omdbapi.com/"&gt;OMDb API&lt;/a&gt; are made when the user clicks the "Search" button. The API responds with a list of movies in JSON. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The example above does NOT implement the Debounce pattern.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Not Debounce
&lt;/h2&gt;

&lt;p&gt;Since the "expensive operation" in the example React application above &lt;strong&gt;only&lt;/strong&gt; executes the HTTP request (i.e. "searches for movies") when the "Search" button within the &lt;code&gt;&amp;lt;Search /&amp;gt;&lt;/code&gt; component is clicked - Debouncing would have little or no effect on the application's performance. &lt;/p&gt;

&lt;p&gt;But that's not how most people use modern web applications. &lt;/p&gt;

&lt;p&gt;We are used to web-apps responding immediately as we enter text with our search results (e.g. &lt;a href="//www.google.com"&gt;google&lt;/a&gt;). So what happens if we refactor the code to work in that fashion? &lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamically Searching
&lt;/h2&gt;

&lt;p&gt;Well the most straight-forward approach would be to listen to the &lt;code&gt;onChange&lt;/code&gt; event for the &lt;code&gt;&amp;lt;Search /&amp;gt;&lt;/code&gt; component, and re-execute the HTTP request (the search) every-time the text changes.  &lt;/p&gt;

&lt;p&gt;That means that if you were to search "Terminator", the &lt;code&gt;onChange&lt;/code&gt; event would get called for each character in the string. Assuming it was typed with no typos, this would create at least 9 &lt;code&gt;get&lt;/code&gt; HTTP requests: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;"t"&lt;/li&gt;
&lt;li&gt;"te" &lt;/li&gt;
&lt;li&gt;"ter"&lt;/li&gt;
&lt;li&gt;"term"&lt;/li&gt;
&lt;li&gt;"termi"&lt;/li&gt;
&lt;li&gt;"termina" &lt;/li&gt;
&lt;li&gt;"terminat" &lt;/li&gt;
&lt;li&gt;"terminato"&lt;/li&gt;
&lt;li&gt;"terminator" &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's 9 or more HTTP requests that may be re-executed so rapidly that the first request has not been responded to - not to mention processed, and rendered - before the next request is made. &lt;/p&gt;

&lt;h3&gt;
  
  
  Expensive Operations
&lt;/h3&gt;

&lt;p&gt;HTTP request are referred to as "expensive" operations because they involve creating a request, encoding the request, transmitting the request over the web, an API receiving the request, then the process repeats in reverse as the request is processed by the API and returned to the source (our React application). &lt;/p&gt;

&lt;p&gt;To make things worse, in our example, each HTTP response must be processed and mapped to components (&lt;code&gt;&amp;lt;Movies /&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;MovieCard /&amp;gt;&lt;/code&gt;) to display the movie information. &lt;/p&gt;

&lt;p&gt;Since each &lt;code&gt;&amp;lt;MovieCard /&amp;gt;&lt;/code&gt; component has an image of the movie, each of this card's will then have to create another HTTP request to another resource to retrieve the image. &lt;/p&gt;

&lt;p&gt;Alternatively, we could keep execution of the search as it was originally, only initiating the &lt;code&gt;get&lt;/code&gt; request, when the &lt;code&gt;&amp;lt;Search /&amp;gt;&lt;/code&gt; component's click event is triggered. &lt;/p&gt;

&lt;p&gt;Problem solved? &lt;/p&gt;

&lt;p&gt;Sure, for this simple example - however what happens when you add filtering:&lt;/p&gt;

&lt;p&gt;Every movie returned from the &lt;a href="http://www.omdbapi.com/"&gt;OMDb API&lt;/a&gt; has &lt;code&gt;Poster&lt;/code&gt;,&lt;code&gt;Title&lt;/code&gt;,&lt;code&gt;Type&lt;/code&gt;,&lt;code&gt;Year&lt;/code&gt;, and &lt;code&gt;imdbID&lt;/code&gt; properties. Realistically, we might want to filter the returned results by &lt;code&gt;Year&lt;/code&gt;, or &lt;code&gt;Type&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;For simplicity, let's just explore filtering by &lt;code&gt;Year&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We can create a &lt;code&gt;&amp;lt;YearFilter /&amp;gt;&lt;/code&gt; component that will take in the search results as a prop, and then we can use a &lt;code&gt;.reduce()&lt;/code&gt; function to get all the years of the movies being rendered:&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;// use `reduce()` to get all the different years&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;years&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;searchResults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;movie&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Year&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Year&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;acc&lt;/span&gt; 
  &lt;span class="p"&gt;},[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we would need create a select, and map all the different years in &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt; elements within that &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map the different years to&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;select&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;years&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;year&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;option&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;year&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/option&lt;/span&gt;&lt;span class="err"&gt;&amp;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;Combine these two functions, and we should have a &lt;code&gt;&amp;lt;YearFilter&amp;gt;&lt;/code&gt; component that displays the years of the movies returned by the search. &lt;/p&gt;

&lt;p&gt;It might look something like:&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;// imports &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="cm"&gt;/**
 * Component for filtering the movies 
 * @param {{searchResults}} props 
 */&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;YearFilter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;searchResults&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;// no filter if &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;searchResults&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;searchResults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;

  &lt;span class="c1"&gt;// get all the different years&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;years&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;searchResults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;movie&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Year&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;movie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Year&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;acc&lt;/span&gt; 
  &lt;span class="p"&gt;},[]);&lt;/span&gt;


  &lt;span class="c1"&gt;// map the different years to&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;years&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;year&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;option&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;year&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/option&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// return JSX &lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yearFilter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Year&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;select&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;options&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/select&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;YearFilter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we would monitor for the &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt;'s &lt;code&gt;onChange&lt;/code&gt; event, and filter out all the displayed movies to only those that match the result. &lt;/p&gt;

&lt;p&gt;I hope at this point you're getting the idea. To keep this article from turning into a tutorial, I'll pause on the example. &lt;/p&gt;

&lt;p&gt;The Problem we are solving is that we have a scenario in which we our React application has an expensive operation that is being re-executed rapidly, so rapidly that operation ("effect") may not even finish its execution before another call to the function "effect" is called. &lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing Debounce
&lt;/h2&gt;

&lt;p&gt;With Debounce, we tell React to only re-execute the query after a certain amount of time. The simplest way to implement this would be to leverage the native &lt;code&gt;setTimeout()&lt;/code&gt; function provided by JavaScript, and wrap the timeout around the "expensive operation". &lt;/p&gt;

&lt;p&gt;So let's focus just on the operation we are concerned about: retrieving movie titles. Logically, we may want to wait to make the request until someone has stopped typing, or once all the filters have been selected.&lt;/p&gt;

&lt;p&gt;Since the &lt;a href="http://www.omdbapi.com/"&gt;OMDb API&lt;/a&gt;'s free tier only allows 1,000 request per day, we also may want to limit how many requests are made for that reason as well. &lt;/p&gt;

&lt;p&gt;So here I've simplified the expensive operation we want to Debounce inside a &lt;code&gt;useEffect&lt;/code&gt; hook:&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;useEffect&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;// using Axios for simplicity &lt;/span&gt;
  &lt;span class="nx"&gt;Axios&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="nx"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR-API-KEY&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;searchTitle&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Search&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;searchTitle&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's wrap our effect with a &lt;code&gt;setTimeout()&lt;/code&gt; ensuring that the effect will only re-execute after a delay.&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;useEffect&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;// capture the timeout &lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;Axios&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="nx"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR-API-KEY&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="na"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;searchTitle&lt;/span&gt; 
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Search&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// timeout of 250 milliseconds &lt;/span&gt;

  &lt;span class="c1"&gt;// clear the timeout &lt;/span&gt;
  &lt;span class="k"&gt;return&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;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeout&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;searchTitle&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;setTimeout()&lt;/code&gt; function wrapped around the HTTP request to our API in this example now ensures that no matter how many times the effect is called (i.e. anytime the &lt;code&gt;searchTitle&lt;/code&gt; changes), the actual network request cannot be called more frequently than in intervals of &lt;code&gt;400&lt;/code&gt; milliseconds.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The time is an arbitrary number, adjust as needed &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Keeping it "DRY"
&lt;/h2&gt;

&lt;p&gt;In all most real-world React applications, there isn't just a single network request. Well, "copy and paste" is never a good option in software development. If we simply copied the effect above and changed the function wrapped inside, we make the first programming mistake of repeating ourselves, and assume technical debt that could be problematic later. &lt;/p&gt;

&lt;p&gt;Rather than "copy and pasting" and modifying to suit unique needs, we can abstract the behavior. &lt;/p&gt;

&lt;p&gt;In React, we can abstract this functionality using a custom hook.&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;// useDebounce.js &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useDebounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&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;// store the provided effect in a `useCallback` hook to avoid &lt;/span&gt;
  &lt;span class="c1"&gt;// having the callback function execute on each render &lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// wrap our callback function in a `setTimeout` function &lt;/span&gt;
  &lt;span class="c1"&gt;// and clear the tim out when completed &lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&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;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; 
  &lt;span class="c1"&gt;// re-execute  the effect if the delay or callback changes&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;useDebounce&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now anywhere there's an expensive operation that has potential to be executed often and/rapidly, we simply wrap that function ("effect") within the custom &lt;code&gt;useDebounce&lt;/code&gt; hook:&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;useDebounce&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;// effect &lt;/span&gt;
  &lt;span class="nx"&gt;Axios&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="nx"&gt;baseUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;apiKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR-API-KEY&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="na"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;searchTitle&lt;/span&gt; 
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Search&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;searchTitle&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// [dependencies, delay]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's Debounce, and how you can abstract the behavior of Debounce to re-use that logic (in a maintainable way) throughout your application. &lt;/p&gt;

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

&lt;p&gt;Implementing debounce in react applications can help avoid unnecessary operations and increase performance. By increasing performance, our React application becomes faster, more responsive to user-input, and provides an improved user-experience. &lt;/p&gt;

&lt;p&gt;This pattern can even be abstracted to a custom hook so that the pattern is easy to implement throughout your application, but will be &lt;em&gt;most&lt;/em&gt; impactful to "expensive operations" or "effects" that are frequently or rapidly re-executed (and it is not necessary to re-execute).  &lt;/p&gt;

&lt;p&gt;What do you think? Does Debounce make sense to you? Will you use it? &lt;/p&gt;

</description>
      <category>react</category>
      <category>performance</category>
      <category>javascript</category>
      <category>tips</category>
    </item>
    <item>
      <title>How JavaScript came to dominate web development </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Mon, 26 Oct 2020 17:54:12 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/how-javascript-came-to-dominate-web-development-3nom</link>
      <guid>https://dev.to/jasonnordheim/how-javascript-came-to-dominate-web-development-3nom</guid>
      <description>&lt;h1&gt;
  
  
  The story of JavaScript
&lt;/h1&gt;

&lt;h2&gt;
  
  
  The web gets accessible
&lt;/h2&gt;

&lt;p&gt;In the 1990s, the first user-friendly, graphical web-browser ("&lt;a href="https://en.wikipedia.org/wiki/Mosaic_(web_browser)"&gt;Mosaic&lt;/a&gt;) was released by developer &lt;a href="https://en.wikipedia.org/wiki/Marc_Andreessen"&gt;Marc Andreessen&lt;/a&gt; from the &lt;a href="http://www.ncsa.illinois.edu/"&gt;National Center for Supercomputing Applications&lt;/a&gt; at the University of Illinois. ("&lt;a href="https://en.wikipedia.org/wiki/Mosaic_(web_browser)"&gt;Mosaic&lt;/a&gt;) was then replaced with the Mosaic Navigator, which later became the Netscape Navigator. &lt;/p&gt;

&lt;p&gt;These "navigators" - which we now refer to as web "browsers" - made the web accessible to every one; not just those who knew how to use a command-line. &lt;/p&gt;

&lt;h2&gt;
  
  
  Java and the Web
&lt;/h2&gt;

&lt;p&gt;Sun Microsystems, bought by released [Java] in 1995. Java borrowed syntax from C/C++ languages, but had a key a difference: Java was a compiled language that could be run on any operating system as long as the Java Virtual Machine was installed on the system. This embraced the idea of "WORA" or "write once, run anywhere". By re-designing the code base to be run through a virtual machine, Developers could develop a single code base, speeding development and reducing bugs. &lt;/p&gt;

&lt;p&gt;Java "applets" were introduced to build on this idea, but in the context of the internet. A Java applet was simply a Java application embedded in an HTML web page. While simple, this enabled Java developers to not only deploy desktop applications, but also web applications using the same robust, high-level programming language they were used to. Now Java applications could be delivered to any operating system over the internet and be run on most computers, as long as the Java virtual machine was installed on the client system. &lt;/p&gt;

&lt;p&gt;Undeniably useful; Applets had a critical flaw - Java applets were in-effect isolated from the DOM (Document Object Model). This separation meant that Java applets could not "see" (be aware of) or mutate (modify) the DOM. Java applets, just like all other Java code had to be compiled through a virtual machine before it could be run on the client system, and the Virtual machine couldn't parse the DOM, just the Java applet - in-effect isolating the applet from the web page. &lt;/p&gt;

&lt;h2&gt;
  
  
  Netscape and JavaScript
&lt;/h2&gt;

&lt;p&gt;To make more dynamic applications that didn't have the limitations imposed by the compilation process and virtual machine required to run Java applets, &lt;a href="https://en.wikipedia.org/wiki/Netscape"&gt;Netscape&lt;/a&gt; contracted &lt;a href="https://en.wikipedia.org/wiki/Brendan_Eich"&gt;Brendan Eich&lt;/a&gt; to build a brand-new "scripting" language that would enable developers to add interactivity and functionality to HTML documents, animate HTML content, perform conditional validations, and lay the foundation for a more dynamic and more comprehensive (i.e. "more desktop like") browser experience. &lt;/p&gt;

&lt;p&gt;Netscape required Eich create this new "scripting" language with a couple of requirements: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Eich's new language needed to combine great amounts of functionality into minimal and simple code&lt;/li&gt;
&lt;li&gt;The language should use a syntax that was familiar and approachable to existing developers by resembling Java&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To make the language, do a lot with little work; Eich sought to employ &lt;a href="https://tinyurl.com/y4d5mfkp"&gt;functional programming&lt;/a&gt; schemes that made it quick, and simple to write procedures that would could process and/or generate data, as well as respond to input with very few lines of code. &lt;/p&gt;

&lt;p&gt;While it can be debated which programming paradigm is "best", Eich sought to combine the the the ability to encapsulate functionality and data within the OOP structure of "objects" and "classes", but remove the rigid structure and more elaborate setup common with traditionally OOP languages like Java. &lt;/p&gt;

&lt;p&gt;With a these requirements in mind, Eich created a new programming language called "Mocha". Mocha was an interpreted and weakly typed functional programming language designed specifically for the web. Despite many name changes, Mocha (aka "JavaScript") was the foundation for the ECMAScript or JavaScript we know today. It required no compilation, no virtual machine, and could interact with the DOM natively. This new programming language was the first viable alternative to Java applets for web applications. &lt;/p&gt;

&lt;h2&gt;
  
  
  Competition &amp;amp; Browser Wars
&lt;/h2&gt;

&lt;p&gt;As Netscape's development continued to gain traction and market share and revenue for web applications became significant, many other players stepped in to try to grab their share of the web application market. In 1996, software giant Microsoft reverse-engineered JavaScript to run its "navigator" or web-browser; &lt;a href="https://en.wikipedia.org/wiki/Internet_Explorer"&gt;Internet Explorer&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Determined to dominate the market, Microsoft not only supported JavaScript in Internet Explorer, but also bundled the browser with it's Windows 95 operating system. End-user's on the Windows Operating System now had a browser pre-installed and ready-to-go when the booted up their computer for the first time. No installation or configuration necessary.&lt;/p&gt;

&lt;p&gt;Microsoft's strategy proved to be successful, but Microsoft didn't just want to compete in this new market, Microsoft hoped to dominate it. In an effort to accomplish this goal, Microsoft developed a web-language of its own CSS or "cascading stylesheets". With CSS, developers could make their web pages not only interactive, but also beautiful. &lt;/p&gt;

&lt;p&gt;It wasn't long before Internet Explorer became the default browser used by most people. Microsoft's strategy had worked. Netscape responded by launching anti-trust lawsuits against against Microsoft, and &lt;a href="https://medium.com/@ddprrt/tales-from-the-browser-wars-mozilla-stomps-internet-explorer-799035887cb1"&gt;even defacing the "e" logo statue outside Microsoft's office&lt;/a&gt;. Despite Netscape's effort's agains the software giant, by 1999 - Internet Explorer controlled 99% of the market. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ending the war
&lt;/h2&gt;

&lt;p&gt;Netscape was fighting a losing battle, and rapidly losing market share. While the company may not survive, their mission to advance web development remained vital to the company's leaders. &lt;/p&gt;

&lt;p&gt;In an effort to ensure the web remained open and accessible for everyone, Netscape took their technology open-source, handing over ownership to non-for-profit Mozilla. Under Mozilla, "Mocha" which had became "LiveScript" then JavaScript, was standardized as "ECMAScript" by the ECMA International standards organization in 1997. &lt;/p&gt;

&lt;p&gt;Continuing on its' mission to keep the web open and accessible; Mozilla developed and released it's own open-source web-browser - "Firefox". As competitors continued to enter the market introducing their own browsers (Opera, Safari, FireFox, etc.), Internet explorer slowly started to lose its domination of the market. Despite competitors, Internet Explorer continued to be the dominant browser with market share only falling to 50% by 2010. &lt;/p&gt;

&lt;h2&gt;
  
  
  OOP or Functional?
&lt;/h2&gt;

&lt;p&gt;Starting with a functional approach Mocha/LiveScript/ECMAScript/JavaScript centered around the idea of a executing procedures on a "scheme". This scheme we now call the DOM or Document Object Model. This functional approach made simple applications a breeze to develop, but was fundamentally different from the OOP languages that had been common in application development. &lt;/p&gt;

&lt;p&gt;Eich sought to enable OOP design principles in JavaScript through an idea of "prototypes" and "prototypal inheritance". With the addition of prototypes and prototypal inheritance, JavaScript employed principles from both functional and OOP programming programming paradigms. &lt;/p&gt;

&lt;p&gt;As with most changes of significance, controversial and largely unpopular at first, because it was different. No programming language before had combined OOP and functional programming paradigms like JavaScript did, and no language had been designed exclusively to be executed in a browser. &lt;/p&gt;

&lt;p&gt;Some developers resented the lack of structure with JavaScript, primarily as JavaScript code could be written using OOP principles, or Functional principles. While flexible, this meant that the format and structure of JavaScript applications varied dramatically. Additionally, the weak-type system, varying browser support, and interpreted nature of JavaScript sometimes resulted in web applications that could look different on different browser's and were often buggier then their strongly-typed counterparts.   &lt;/p&gt;

&lt;p&gt;Despite the controversy, demand and use of web applications continued to rise and JavaScript became the primary programming language for web development. With a large developer base, and open-sourcing, the language was quickly integrated and improved upon with the third version of ECMAScript (which had started as "LiveScript", and had been "Mocha" before that) was released in 1999 further increasing the performance and feature-set offered. &lt;/p&gt;

&lt;h2&gt;
  
  
  Blurring the desktop and web experience
&lt;/h2&gt;

&lt;p&gt;In 2005 a ES4 (ECMAScript version 4) arrived with a headline feature: &lt;strong&gt;AJAX&lt;/strong&gt;. AJAX or "Asynchronous JavaScript and XML" enabled asynchronous execution of functions/procedures. With asynchronous code execution, web applications could remain responsive to input even while executing expensive (i.e. "demanding") operations. &lt;/p&gt;

&lt;p&gt;JavaScript continued to evolve rapidly as projects, libraries, and frameworks made it faster and easier than ever to quickly build web applications. Libraries like &lt;a href="https://jquery.com/"&gt;jQuery&lt;/a&gt; were built on top of JavaScript, and reduced development time with useful helper functions for common operations, while simultaneously abstracting these operations so that the end-result was the same regardless of the client's browser of choice. &lt;/p&gt;

&lt;p&gt;In 2009, ES5 (ECMAScript 5) was released, followed by ES6 in 2015. With each version came increased browser support, increased performance, as well as additional features that was easier to execute common functions and more readable/intuitive code. With the release of ES6 (officially published as "ES2015") new syntax and more controlled scoping largely closed the gap in feature-set offered by JavaScript in comparison to traditional languages. &lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript today
&lt;/h2&gt;

&lt;p&gt;Allowing development of web applications that utilize OOP or functional programming principles - JavaScript remains the primary avenue for creating web applications. JavaScript remains a flexible, modern, and powerful programming language for a wide variety of applications, and is the most popular programming language used by developers today.  &lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.coursereport.com/blog/history-of-javascript"&gt;Course Report&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/ajax/what_is_ajax.htm"&gt;Tutorials Point - AJAX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/java/java_overview.htm"&gt;Tutorials Point - Java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.britannica.com/topic/Netscape-Communications-Corp"&gt;Britannica - Netscape&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.mozilla.org/en-US/firefox/browsers/browser-history/"&gt;Mozilla Browser History&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@ddprrt/tales-from-the-browser-wars-mozilla-stomps-internet-explorer-799035887cb1"&gt;Medium - Browser Wars&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Lets talk TypeScript</title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Wed, 21 Oct 2020 06:19:11 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/lets-talk-typescript-18ae</link>
      <guid>https://dev.to/jasonnordheim/lets-talk-typescript-18ae</guid>
      <description>&lt;h1&gt;
  
  
  Lets talk TypeScript
&lt;/h1&gt;

&lt;p&gt;Ever wonder what the buzz surrounding TypeScript is all about? Keep reading and come on a deep dive on TypeScript addressing the fundamental questions surrounding TypeScript: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is TypeScript? &lt;/li&gt;
&lt;li&gt;What features/benefits does TypeScript offer&lt;/li&gt;
&lt;li&gt;What are the fundamentals a JavaScript developer needs to get started with TypeScript? &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;It is assumed that readers of this post understand basic JavaScript and have some experience creating JavaScript applications. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is TypeScript
&lt;/h2&gt;

&lt;p&gt;As with anything, a good place to start is with defining "TypeScript". &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt; is: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a super-set of JavaScript &lt;/li&gt;
&lt;li&gt;open-source &lt;/li&gt;
&lt;li&gt;statically typed langauge &lt;/li&gt;
&lt;li&gt;strongly typed language &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's the appeal?
&lt;/h2&gt;

&lt;p&gt;More apprehensive developers may ask: "Why would I want to learn TypeScript?" and "How would TypeScript improve code?". To answer this as concisely as possible: TypeScript doesn't offer any functionality that isn't offered by JavaScript, TypeScript simply adds &lt;a href="https://stackoverflow.com/questions/2690544/what-is-the-difference-between-a-strongly-typed-language-and-a-statically-typed"&gt;strong-typing&lt;/a&gt; on top of JavaScript. &lt;/p&gt;

&lt;p&gt;Personally, I began learning programming in the strongly typed langauges like C# and Java. Transitioning to a weakly typed langauge like JavaScript was uncomfortable. JavaScript meant that the rules of strongly-typed programming langauges were no longer true. The idea that I could declare a variable with a value of &lt;code&gt;23&lt;/code&gt; and later change it to the string &lt;code&gt;"Sally"&lt;/code&gt;, and that was "ok" seemed like bad behavior, like breaking the rules. &lt;/p&gt;

&lt;p&gt;This is where TypeScript comes in; it adds strong typing (and the associated benefits/drawbacks) to the langauge of the web. &lt;/p&gt;

&lt;h3&gt;
  
  
  Strongly and Weakly typed languages
&lt;/h3&gt;

&lt;p&gt;Weakly typed langauges (like JavaScript) often made me feel like I was doing something bad - "breaking the rules", because when I learned how to write my first piece of code, I was also taught the various data types, and that these types &lt;strong&gt;must&lt;/strong&gt; be declared, or the code will not compile. End of story.&lt;/p&gt;

&lt;p&gt;Enter JavaScript - which will infer and coerce variable to the desired type whenever possible. With weakly typed langauges like JavaScript a variable's type is mutable (can be changed). A variable could start out holding a string value, and later hold a number, or an object or a boolean, etc. &lt;/p&gt;

&lt;p&gt;Essentially the fundamental rules embedded in someone working with Java and C# were thrown out the window. &lt;/p&gt;

&lt;p&gt;With a strongly typed langauge, variable must be declared with a type. This type would define a contract that the variable assigned to the type would be required to follow. This type would be declared when the variable was created and could not be changed (immutable type) once declared.   &lt;/p&gt;

&lt;p&gt;With strong-typing; variables, functions, and objects have strictly defined rules that could not be broken. Any piece of code that fails to adhere to the rules defined by the type or interface defined would throw an error, and fail to compile. &lt;/p&gt;

&lt;p&gt;These contracts mean that the developer writing the code, or building features implementing third-party code (that is strongly-typed) cannot write code that doesn't follow the defined contract. A variable initally defined as a number must always be a number. &lt;/p&gt;

&lt;p&gt;It also means that functions in strongly-typed langauges like TypeScript have contracts for both the input (parameters) as well as the output (the return value), and that if the code was attempted to be used in a way that violates the terms of the contract an error is thrown and the code will fail to compile. &lt;/p&gt;

&lt;p&gt;Personally, I loved the tooling that strongly-typed langauges offered in modern IDEs: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;intelligent code completion of methods/function, variables, fields, classes, interfaces, modules, properties/attributes, and more.&lt;/li&gt;
&lt;li&gt;in-line access access to third-party library documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Weighing Pros and Cons
&lt;/h2&gt;

&lt;p&gt;While I personally love the structure that comes with strongly typed languages, I would feel remis if I didn't mention the benefits of weakly-typed languages. The main benefit; flexibility. &lt;/p&gt;

&lt;p&gt;With weakly typed langauges, a function can return one data-type in one case and a totally different value-type in another case. No overloading, interfaces, or generics required - it just works. &lt;/p&gt;

&lt;p&gt;The JavaScript compilier doesn't care about the type of values provided to a function, class or method. Additionally, the type of the return value of the function is also irrelevant to the JavaScript compiler. &lt;/p&gt;

&lt;p&gt;In JavaScript, a function that takes two arguments/parameters and adds them together can return different data types, and the code will compile without issue. This could be fine, but it could also result in "bugs" that are difficult to find and de-bug as there is no garuntee to the type or structure of the data going into, or returning from a function.&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;// function to add 2 variables together &lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* by changing the data-type of parameters provided to the 
 * function, we also can change the data-type returned by the function */&lt;/span&gt;
&lt;span class="nx"&gt;add&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="c1"&gt;// =&amp;gt; 5 (number)&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="s1"&gt;2&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="s1"&gt;3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; '23' (string)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the function &lt;code&gt;add(x,y)&lt;/code&gt; takes in two parameters (&lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;) and returns &lt;code&gt;x + y&lt;/code&gt;. Used as intended this would return the sum of the two numbers provided. However, if we alter those one or both of those variables to have a data-type of &lt;code&gt;string&lt;/code&gt;, the function will return a string where the parameters have been concatenated.&lt;/p&gt;

&lt;p&gt;There are scenarios where it may be desireable to have different data-types returned by a function, depending on the parameters provided to the function. In this way, we do not need interfaces or generics to implement abstract functionality, we can simply ignore the data-type.&lt;/p&gt;

&lt;p&gt;This can make JavaScript code more concise. Avoiding type/generic definitions, interfaces, and casting. It could be argued that weakly-typed langauges like JavaScript enables developers to be more expressive, and more flexible code (polymorphism, mixins, etc.). &lt;/p&gt;

&lt;p&gt;However, since the compilier has no defined rules on the data-types of variables, the parameters provided to a function or the return value of a function, the compilier cannot identify unexpected behavior (because we haven't defined what the expected behavior is). &lt;/p&gt;

&lt;p&gt;As a result, working in weakly-typed langauges means that unexpected behavior may not appear until an application is published and unexpected inputs are provided that break the functionality of the application. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thing about it: would you rather be alerted to problems as you develop by a compilier or after deployment, when the application is being used be real customers? &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Strongly typed langauges also enable (somewhat) self-documenting code; allowing IDEs to automatically display information about the names, types, and return values of functions/methods/procedures and provide this inline (within the code editor) as the code is typed, and even auto-completing code in some scenarios. &lt;/p&gt;

&lt;p&gt;In short, weakly-typed langauges benefit from: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;more concise code &lt;/li&gt;
&lt;li&gt;more flexible code &lt;/li&gt;
&lt;li&gt;more expressive code &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While, strongly-typed langauges benefit from: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implicit documentation &lt;/li&gt;
&lt;li&gt;Fewer errors at runtime via strong typing &lt;/li&gt;
&lt;li&gt;Increased performance through optimization (sometimes)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A Metaphor
&lt;/h3&gt;

&lt;p&gt;In my head, weakly-typed languages seem to me like a highway that has no speed limit and no rules. There are no rules about the speed at which you travel, the mode of transportation, safety regulations, etc. &lt;/p&gt;

&lt;p&gt;If used as intended a highway like this has the potential to function fine, maybe even better in specific situations. As with weakly-typed langauges, we are trading structure and rigid rules for flexibility. &lt;/p&gt;

&lt;p&gt;If such a highway (a metaphor for a weakly-typed variable or function) existed, I can easily imagine people driving faster, on both sides and in both directions, failing to signal or use seatbelts, and countless other things that would appual a rule-abiding citizen. &lt;/p&gt;

&lt;h2&gt;
  
  
  Enter TypeScript
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt; was created developed by Microsoft in 2012 and it seeks to add the structure and rules of strongly-typed langauges to "the langauge of the web" (JavaScript) without requiring changing the experience for end-users. &lt;/p&gt;

&lt;h2&gt;
  
  
  TypeScript Fundamentals
&lt;/h2&gt;

&lt;p&gt;As a superset of JavaScript, all JavaScript &lt;strong&gt;is valid&lt;/strong&gt; TypeScript. In other words; any valid JavaScript code is also valid in TypeScript; however it doesn't recieve the benefits (or drawbacks) of strong typing unless the JavaScript is annotated with types. This is significant for a couple reasons: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Progressive Adoption - Since TypeScript is a superset of JavaScript, strong-typing can be added incrementally, without requiring re-writes of entire applications since the TypeScript is compiled to JavaScript anyway. &lt;/li&gt;
&lt;li&gt;Future Proofing &amp;amp; Compatability - Since TypeScript cannot run in its default state and must transpiled into JavaScript in order to be run - developers using TypeScript do not need to be concerned with browser support as TypeScript code can be transpiled into various versions of JavaScript with release dates as far back as 1999 (which the TypeScript compilier does by default). &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;TypeScript can be installed via &lt;a href="https://www.npmjs.com/get-npm"&gt;NPM&lt;/a&gt; using the command &lt;code&gt;npm install -g typescript&lt;/code&gt; which will install the TypeScript compilier globally. Once installed, we can see what version of typescript we have by running &lt;code&gt;tsc --version&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Setup and Configuration
&lt;/h3&gt;

&lt;p&gt;There are numerous options that can configure the way the TypeScript compilier transpilies TypeScript code into JavaScript code. These options can be executed manually at the time of compilation (as command line arguments) or can be picked up automatically with a JSON configuration; &lt;code&gt;tsconfig.json&lt;/code&gt; placed in the project's root directory, and will automatically be picked up by the TypeScript compilier. &lt;/p&gt;

&lt;p&gt;There are numerous options here, but most are just that: "options", meaning that you do not &lt;strong&gt;need&lt;/strong&gt; to provide them. However, there are some common one's that I'd like to bring to discuss: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;"target"&lt;/code&gt; - allows configuration of the target version of JavaScript. Defaults to &lt;code&gt;"es3"&lt;/code&gt;. Can be configured to the latest version of JavaScript by specifying &lt;code&gt;"esnext"&lt;/code&gt; instead:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;tsconfig.json&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"esnext"&lt;/span&gt;&lt;span class="w"&gt; 
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;"watch"&lt;/code&gt; - allows automatic re-compiliing of TypeScript to JavaScript as changes are saved to a TypeScript file, removing the need to run the &lt;code&gt;tsc&lt;/code&gt; command to recompile TypeScript code to JavaScript. Disabled by default.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;tsconfig.json&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"esnext"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"watch"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;"lib"&lt;/code&gt; - enables included type declarations for common technologies/features found in modern web applications like the DOM without any compiliation errors along with access to integrated documentation within most IDE's.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;specify&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;native&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;support&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;common&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;DOM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;elements&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;that&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;exist&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;as&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;variables&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;classes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;like&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;`document`,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;`window`,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;`URL`,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;etc.&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;modern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;version&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;JavaScript&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"esnext"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"watch"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="nl"&gt;"lib"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"dom"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"es2017"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whether run manually, or automatically using the "watch" function configured in a &lt;code&gt;tsconfig&lt;/code&gt; file: TypeScript code placed in &lt;code&gt;.ts&lt;/code&gt; files, will be converted into its configured version JavaScript code (ES3 by default) with the same file names, but with the &lt;code&gt;.js&lt;/code&gt; extension. &lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring variable types
&lt;/h3&gt;

&lt;p&gt;In TypeScript, we define and assign types to variables. Once assigned the type cannot be changed. &lt;/p&gt;

&lt;h4&gt;
  
  
  Implicit vs. Explicit Type Declarations
&lt;/h4&gt;

&lt;p&gt;Type declarations can be declared/implented in two ways; &lt;em&gt;explicitly&lt;/em&gt; or &lt;em&gt;implicitly&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;To &lt;em&gt;implicitly&lt;/em&gt; declare a the data type of a variable, we can define the value of the variable at the time of declaration, which allows the compilier to infer the data type of the variable and enforce its type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* implicit declaration */&lt;/span&gt; 
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;

&lt;span class="cm"&gt;/* attempting to assign a string to a variable implicitly declared 
 * as a number is not allowed and will create a compile-time error */&lt;/span&gt; 
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twenty-three&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// [ts] Type "twenty-three" is not assignable to type 'number' &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: In JavaScript, this would be allowed and could create a bug that likely would be difficult to find - but due to the strong typing of TypeScript - the code will fail to compile. Since TypeScript is a superset of JavaScript, we can "opt out" of the strong typing inherent to TypeScript by declaring the type of &lt;code&gt;any&lt;/code&gt; which will indicate to the compilier that it should not validate changes to the value of that variable. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we don't have a value to assign to the variable at declaration, we can &lt;em&gt;explicitely&lt;/em&gt; declare the variable type by annotating the variable declaration with its type. Without a type annotation TypeScript variables will be declared as &lt;code&gt;any&lt;/code&gt; meaning that they are not type-checked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* No Type Anotation */&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// will be inferred as `any` data type and will not be type-checked by the compiler &lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; valid &lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;suzie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; valid &lt;/span&gt;

&lt;span class="cm"&gt;/* Explicity Type declaration */&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lucky&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// indicates that only booleans (true/false) values can be assigned to the `lucky` variable &lt;/span&gt;
&lt;span class="nx"&gt;lucky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;suzie&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; type error &lt;/span&gt;
&lt;span class="nx"&gt;lucky&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;//=&amp;gt; valid &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Best Practice: If you have a value to assign at the time of the variable declaration, do not explicitly declare its type as it would be redundant. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Moving beyond "primitive" data-types
&lt;/h3&gt;

&lt;p&gt;In JavaScript (and TypeScript) there are six (6) primitive data types: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;undefined&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;number&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;string&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;bigint&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;symbol&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;More complex pieces of information are representing with what is referred to as "Structural Types". This includes; arrays, maps, sets, dates, and any other "object" where it is necessary to encapsulate more than one primitive data-type, or that need to structure data in a specific manner. &lt;/p&gt;

&lt;h4&gt;
  
  
  Custom Types
&lt;/h4&gt;

&lt;p&gt;With TypeScript, custom "types" can be declared using the keyword: &lt;code&gt;type&lt;/code&gt; followed by the name of the type (in Pascal case) and setting it equal to (&lt;code&gt;=&lt;/code&gt;) the type definition. This sets up a contract that can define the format of a variable, the format of parameters to a function as well as the format of a function's return value. &lt;/p&gt;

&lt;p&gt;Once declared, a custom type is implemented exactly like a primitive type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* declare custom type of "Font" which will be required to always be a string value */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; 

&lt;span class="cm"&gt;/* declare variable to have a type of "Font" */&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myFont&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;Font&lt;/span&gt; 

&lt;span class="c1"&gt;// valid &lt;/span&gt;
&lt;span class="nx"&gt;myFont&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;span class="nx"&gt;myFont&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Italic&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;// invalid &lt;/span&gt;
&lt;span class="nx"&gt;myFont&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Union Types
&lt;/h4&gt;

&lt;p&gt;TypeScript goes beyond primitive and custom types by providing "union types". With union types, not only is the structure and type of data enforced, but the actual value is limited to the value(s) outlined within the union type declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* be defining the `Style` type as a union type, 
 * the TypeScript compilier will ensure that any 
 * variables assigned as that union type will only 
 * have values matching the prescribed values */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Style&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;italic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bold&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;regular&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="c1"&gt;// Explicitely declare strong type&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;font&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;Style&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// valid &lt;/span&gt;
&lt;span class="nx"&gt;font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;italic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 

&lt;span class="c1"&gt;//invalid &lt;/span&gt;
&lt;span class="nx"&gt;font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;helvetica&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Interfaces
&lt;/h3&gt;

&lt;p&gt;Another to define the structure in TypeScript is through &lt;strong&gt;interfaces&lt;/strong&gt;. Interfaces specify the shape of an object or class without strictly requiring the value be of a specific type. In this way, TypeScript provides abstraction and flexibility. &lt;/p&gt;

&lt;p&gt;As long as a variable, parameter or return value aheres to the rules established in the interface definition - the variable, parameter and/or return value can be of any type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* declare a custom `type` of person, which is represented 
 * as an object with a 'first' property which is a string, 
 * and a `last` property that is also a string */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Person&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="kr"&gt;string&lt;/span&gt; 
  &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* explicitely define variable type */&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// valid &lt;/span&gt;
&lt;span class="nx"&gt;winner&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;Usain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// invalid &lt;/span&gt;
&lt;span class="nx"&gt;winner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Usain Bolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;span class="nx"&gt;winner&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;Usain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jamaica&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;In this case, a variable implementing the interface &lt;code&gt;Person&lt;/code&gt; ensures that the variable &lt;code&gt;winner&lt;/code&gt; must be an object with a property for &lt;code&gt;first&lt;/code&gt; that is has a type &lt;code&gt;string&lt;/code&gt; and property named &lt;code&gt;last&lt;/code&gt; which is also of type string. &lt;/p&gt;

&lt;p&gt;All variables implementing the &lt;code&gt;Person&lt;/code&gt; interface must adhere to these rules. They &lt;strong&gt;cannot&lt;/strong&gt; have any additional  properties (like &lt;code&gt;country&lt;/code&gt;), would throw an error and that assining any assignment to the variable &lt;code&gt;winner&lt;/code&gt; cannot deviate from the rules defined by the interface. Any violation of those rules would throw an error. &lt;/p&gt;

&lt;h4&gt;
  
  
  Making more flexible interfaces
&lt;/h4&gt;

&lt;p&gt;In some situations, the rigid definition of types and interfaces can restrict functionality. One such scenario is in the event where there is a collection of items that all have &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;last&lt;/code&gt; properties that are both strings, but could have additional properties beyond that as long as the &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;last&lt;/code&gt; properties exist. &lt;/p&gt;

&lt;p&gt;This restriction can be circumvented wiht a little creativity by adding a little bit to the the type definition: &lt;/p&gt;

&lt;p&gt;So if the goal was to have enable the scenario where we have a collection of objects that have &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;last&lt;/code&gt;  properties that are &lt;code&gt;string&lt;/code&gt;s, we can specify that an additional property named as a &lt;code&gt;string&lt;/code&gt; will have an associated type of &lt;code&gt;any&lt;/code&gt;, enabling greater flexibility through polymorphism. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is just like a dictionary&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* adding an addtional key value pair to be stored with any name and any value */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Person&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="kr"&gt;string&lt;/span&gt; 
  &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; 
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* explicitely define variable type */&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// valid &lt;/span&gt;
&lt;span class="nx"&gt;winner&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;Usain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;winner&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;Usain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jamaica&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;winner&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;Usain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;fast&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="c1"&gt;// invalid &lt;/span&gt;
&lt;span class="nx"&gt;winner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Usain Bolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Types and Functions
&lt;/h3&gt;

&lt;p&gt;In addition to defining types and interfaces for variables, TypeScript enables (and encourages) defining data-types in the function definition such that the parameters of a specific function adhere to the types declared, and return a value that adheres to the type specified as the return type.&lt;/p&gt;

&lt;p&gt;Strongly typing parameters of functions and their return values uses the same syntax as type/interface declarations (exluding the &lt;code&gt;const&lt;/code&gt;/&lt;code&gt;let&lt;/code&gt; used with variable declarations). First we define a name for each parameter, for each named parameter, the type is defined using a colon (&lt;code&gt;:&lt;/code&gt;) followed by the type (e.g. &lt;code&gt;x:number&lt;/code&gt;). The &lt;em&gt;return value&lt;/em&gt; of the function is defined following the closing parenthesis (&lt;code&gt;)&lt;/code&gt;) of the function's parameter list and before the opening curly-brace (&lt;code&gt;{&lt;/code&gt;) of the function's body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* function to raise x to a power of y WITHOUT type declarations */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* The same function to raise x to a power of y WITH type declarations */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kr"&gt;number&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;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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;Function that do &lt;strong&gt;not&lt;/strong&gt; return a anything, (like event listeners, side-effects, etc.) should be defined as having a return type of &lt;code&gt;void&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Example of a functiont that does not return any value */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MouseEvent&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ... execute event handler &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding strong typing to parameters and return values of functions, the TypeScript compilier can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;validate parameters to functions are of the correct type &lt;/li&gt;
&lt;li&gt;validate the return value of a function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Working with arrays
&lt;/h3&gt;

&lt;p&gt;Arrays defined in &lt;code&gt;.ts&lt;/code&gt; (TypeScript) files that are not strongly-typed function the same as arrays in &lt;code&gt;.js&lt;/code&gt; (JavaScript) files. Elements within arrays without strong-typing will accept elements of any data-type, which could result in each element adhering to the same rules (i.e. be of the same type), or being of varying types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* declaring an array without a type will essentially "opt out" of 
 * the safe-gaurds provided by TypeScript */&lt;/span&gt; 
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; 

&lt;span class="cm"&gt;/* So we can add elements to the array of any type */&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Susan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By declaring adding typing to arrays, the compiler will throw an error anytime an element failing to adhere to the type/interface outlined in the array's type definition will throw an error.&lt;/p&gt;

&lt;p&gt;Typing is added to arrays similarly to adding typing to variables and function definitions. First declare the type of variable (&lt;code&gt;const&lt;/code&gt;/&lt;code&gt;let&lt;/code&gt;), followed by the name of the array, followed by a colon (&lt;code&gt;:&lt;/code&gt;) and the &lt;code&gt;type&lt;/code&gt; (e.g. &lt;code&gt;:number&lt;/code&gt;) or &lt;code&gt;interface&lt;/code&gt; (e.g. &lt;code&gt;Person&lt;/code&gt;), then with opening and closing brackets (&lt;code&gt;[]&lt;/code&gt;) to indicate that it is an array of that type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* strongly typed array of numbers */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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="s2"&gt;`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be useful when working with complex or irregular objects as well as increasing performance through optimization (in some cases).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* declare an interface */&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Person&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="kr"&gt;string&lt;/span&gt; 
  &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; 
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* every element within the array must adhere to 
 * the rules defined in the interface or type annotated, 
 * in this case: the person interface */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt; 

&lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&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="s1"&gt;Barack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Obama&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="c1"&gt;// valid &lt;/span&gt;
&lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&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="s1"&gt;Steve&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jobs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="c1"&gt;// throws an error &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Tuples
&lt;/h4&gt;

&lt;p&gt;TypeScript builds on this strong typing of arrays by enabling definition of a "tuple", which (in TypeScript) is a strongly typed, fixed length array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* declare a tuple that has 3 elements, 
 * the first being a number, 
 * the second being a string
 * and the thirds being a boolean */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Contestant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create a tuple of this type we annotate the variable with the type &lt;code&gt;:Contestant&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Custom Type */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Contestant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="cm"&gt;/* Create Tuple from Type */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;competitors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Contestant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Tony Robbins&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: with the &lt;code&gt;type&lt;/code&gt; definition above, we cannot initialize a variable of type &lt;code&gt;Contestant&lt;/code&gt; to an empty array (&lt;code&gt;[]&lt;/code&gt;), rather it must be declared with elements matching the types/interfaces declared by the tuple's &lt;code&gt;type&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Generics
&lt;/h3&gt;

&lt;p&gt;In order to implement functionality where behavior has been abstracted so that the logic implemented can be repeated with different varaible types, TypeScript offers "generics". &lt;/p&gt;

&lt;p&gt;This abstraction of behavior with generics is pervasive in Framework's like &lt;a href="https://angular.io/"&gt;Angular&lt;/a&gt;. Generics are also common in a variety of Software Engineering design principles and patterns like the &lt;a href="https://sourcemaking.com/design_patterns/observer"&gt;"observer" pattern&lt;/a&gt;. In the observer pattern, a one-to-many relationship is defined between an object and all it's "obsevers" (other objects), such that when the state of the "subject"&lt;br&gt;
 being &lt;em&gt;observed&lt;/em&gt; changes, all the observers of the subject are automatically updated. &lt;/p&gt;
&lt;h4&gt;
  
  
  Generic Syntax
&lt;/h4&gt;

&lt;p&gt;To declare a generic in TypeScript we using angle brackets (&lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;) enclosed with an alias (often "T": &lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt;) representing an abstraction of the object that is being added the "generic" logic or functionality defined by in the generic type definition.  &lt;/p&gt;

&lt;p&gt;In TypeScript this might look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* declare generic type of "Observable" 
 * with the variable `T` representing 
 * any object that where "Observable" 
 * functionality is needed */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* define that any observable will have a public property 
   * named `value` */&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&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="cm"&gt;/* explicitly declare an observable number */&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;importantNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="cm"&gt;/* explicitly declare an observable person */&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Person&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&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;importantPerson&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

&lt;span class="cm"&gt;/* implicitly declare an observable number */&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;secondPassed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With generics, logic and functionality can be created without knowing the type of data (primitive or structured) that will implement the abstracted ("generic") logic. &lt;/p&gt;

&lt;h2&gt;
  
  
  And that's the basics
&lt;/h2&gt;

&lt;p&gt;Hopefully by this point you've got a basic idea of what TypeScript is, what benefits and drawbacks TypeScript offers when compared to JavaScript and the basics of defining, implementing and using strongly typed variables, interfaces, arrays, and the abstraction of typing using Generics. &lt;/p&gt;

</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Getting Started with Vue.js </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Fri, 21 Aug 2020 22:49:11 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/getting-started-with-vue-js-3nl2</link>
      <guid>https://dev.to/jasonnordheim/getting-started-with-vue-js-3nl2</guid>
      <description>&lt;h1&gt;
  
  
  Getting Started with View
&lt;/h1&gt;

&lt;p&gt;View is a declarative and progresive web framework that is designed from the ground-up to be incrementally adoptable by focusing solely on the view layer. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Basics
&lt;/h2&gt;

&lt;p&gt;To create a &lt;code&gt;Vue&lt;/code&gt; instance connecting the Vue JavaScript framework with the HTML document, you must either have the Vue framework installed and referenced in your project, or link to a CDN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- index.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheeet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"index.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- Import the view framework via CDN --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/vue/dist/vue.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- we will use this ID to set the root element for Vue.js --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- We use double curly brace syntax to reference information contained in the 'data' element of the Vue , this is called a "template literal" --&amp;gt;&lt;/span&gt;
            {{ message }}
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="c"&gt;&amp;lt;!-- Link to associated View/Vue --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"index.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// index.js &lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; 
        &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/* the sets up the connection to the root element of the application */&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Vue!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="cm"&gt;/* an property defined in the `root` of data will be immediately accessible between double {{}}curly braces in a HTML document */&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;Now any change this &lt;code&gt;data&lt;/code&gt; object within the associated &lt;code&gt;Vue&lt;/code&gt; changes, the associated data in the HTML template will also be re-rendered. &lt;/p&gt;

&lt;p&gt;If you don't believe me, open up the JavaScript console and change the value of &lt;code&gt;app.message&lt;/code&gt; and take a look at the HTML page in your browser? (it should be displaying the new value) &lt;/p&gt;

&lt;p&gt;Now that &lt;code&gt;Vue&lt;/code&gt; is connected to our HTML, we no longer need to interact with it directly, but rather through our instance of &lt;code&gt;Vue({...})&lt;/code&gt; which is connected directly to our &lt;code&gt;&amp;lt;div id="app"&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Directives
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Directives&lt;/strong&gt; in &lt;code&gt;Vue&lt;/code&gt; are HTML elements that are prefixed with &lt;code&gt;v-&lt;/code&gt; which indicates that they will have special properties provided by Vue. So if we have something like &lt;code&gt;v-bind&lt;/code&gt;, we are telling view that we are about to "bind" some piece of data as an attribute to an HTML element, effectively; we can bind JavaScript code to attributes of an HTML elements to add functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;v-bind:title=&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        Hover your mouse over this text for a few seconds to see when you loaded the page. 
    &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`This page was loaded at &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="nx"&gt;toLocaleString&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="p"&gt;})&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be effectively equivilant to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;title=&lt;/span&gt;&lt;span class="s"&gt;"This page was loaded at CURRENT DATE/TIME"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        Hover your mouse over this text for a few seconds to see when you loaded the page. 
    &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... except that native HTML doesn't allow us to dynamically generate text so we would have to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;
            Hover your mouse over this text for a few seconds to see when you loaded the page. 
        &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;span&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="s1"&gt;#app &amp;gt; span&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`This page was loaded at &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="nx"&gt;toLocaleString&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="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Redering Collections
&lt;/h3&gt;

&lt;p&gt;As is common in programming, we often need to iterate ("loop") through data sets and display information from the objects being iterated ("looped") through, view does this by attaching a "for" (&lt;code&gt;v-for&lt;/code&gt;) to the element to be rendered for each object being iterated over. &lt;/p&gt;

&lt;p&gt;So if we had a collection of todo's, we could display each of them using the &lt;code&gt;v-for&lt;/code&gt; directive, which will take a string argument that includes an alias which will represent each object as it is iterated through, and the name of the collection within the data set containing the objects to loop through. &lt;/p&gt;

&lt;p&gt;That sounds complicated, but in practice it looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="c"&gt;&amp;lt;!-- 
        Target Root Element 
    --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; 
        &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
            &lt;span class="c"&gt;&amp;lt;!--    
                Element to display each iterated object
                along with an alias ("todo") of what each item will be called from the speciefied collection ("todos")
            --&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"todos in todos"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                {{ todo.text }}
            &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
            &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;todos&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="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Learn Java&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="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Learn Kotlin&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="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deploy android application to the google store&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;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or if we wanted to expand on that check list we could add additional properties and loop through them like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Leveling up the todo list&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"todo in todos"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;{{todo.text}}&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="na"&gt;v-bind:checked=&lt;/span&gt;&lt;span class="s"&gt;"todo.checked"&lt;/span&gt; &lt;span class="na"&gt;aria-label=&lt;/span&gt;&lt;span class="s"&gt;"todo.text"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;todos&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="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Learn Java&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;checked&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Learn Kotlin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;checked&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deploy android application to the google store&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;checked&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="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conditional Rendering
&lt;/h3&gt;

&lt;p&gt;Well that's all fine and dandy, that's very little of what the Vue.js framework enables; one of the more common functionalities of any JavaScript framework (React, Vue, Angular) has to do with reactively displaying different pieces of information depending on various conditions. &lt;/p&gt;

&lt;p&gt;To enact conditions we use the "if" &lt;code&gt;v-if&lt;/code&gt; directive, and supply that directive with the name of the property we want to perform the "if" evaluation upon. &lt;/p&gt;

&lt;p&gt;That could look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- 
        `v-if` is checking the 
        "seen" property of `data` within the `Vue` 
        It should evaluate to true or false 
    --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;v-if=&lt;/span&gt;&lt;span class="s"&gt;"seen"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Now you see me&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;v-if=&lt;/span&gt;&lt;span class="s"&gt;"unseen"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Now you don't&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;seen&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="cm"&gt;/* element should render */&lt;/span&gt;
    &lt;span class="na"&gt;unseen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="cm"&gt;/* element should not render */&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Events
&lt;/h3&gt;

&lt;p&gt;In order to handle events with view, we utilize the "on" (&lt;code&gt;v-on&lt;/code&gt;) directive, which attaches event listeners to HTML elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;{{ message }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- 
      Attaching a click event listener to a method called 'reverseMessage'
    --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;v-on:click=&lt;/span&gt;&lt;span class="s"&gt;"reverseMessage"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Reverse Message&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app-5&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Vue.js!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;reverseMessage&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="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;v-on&lt;/code&gt; we can attach any event listeners that we would use outside of a framework simply by specifying what event after the colon (&lt;code&gt;v-on:click&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Alright so, that's the basics to get you started. There is so much more to be discovered. Look out for follow-up post soon!&lt;/p&gt;

&lt;h2&gt;
  
  
  Acknowlegements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Inspired by &lt;a href="https://vuejs.org/v2/guide/"&gt;Vue JS Guide&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>frameworks</category>
    </item>
    <item>
      <title>An Intro into React Hooks with `useState` </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Wed, 15 Jul 2020 19:50:21 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/an-intro-into-react-hooks-with-usestate-dnf</link>
      <guid>https://dev.to/jasonnordheim/an-intro-into-react-hooks-with-usestate-dnf</guid>
      <description>&lt;h1&gt;
  
  
  An Intro into React Hooks with &lt;code&gt;useState&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;First, lets get a baseline. &lt;/p&gt;

&lt;p&gt;React hooks allow developers to encapsulate abstract logic that can be re-used in some, a few or many components within a react application. Since state is one of the most cumbersome items to manage within a react application; most react hooks help manage state. In fact, one of the most-used react hooks is called &lt;code&gt;useState&lt;/code&gt; and (unsurprisingly) it abstracts the code surrounding using state, allowing the developer to focus more on application functionality and less on the technicalities surrounding creating, modifying, and retrieving state. &lt;/p&gt;

&lt;h2&gt;
  
  
  Old vs. New
&lt;/h2&gt;

&lt;p&gt;To better understand this, let's take a look at a traditional component built &lt;strong&gt;without&lt;/strong&gt; hooks.&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;span class="c1"&gt;// a Counter component that allows an end-user to increment or decrement a counter &lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&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="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// increment the value of the counter, defaulting to increase of 1&lt;/span&gt;
  &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;amount&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="c1"&gt;// decrement the value of the counter, defaulting to a decrement of 1&lt;/span&gt;
  &lt;span class="nx"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;amount&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="c1"&gt;// reset the counter to the initial state &lt;/span&gt;
  &lt;span class="nx"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&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="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&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="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;increment&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;+&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;decrement&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;-&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;reset&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;Reset&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;The code above demonstrates a traditional react component built with class-based inheritance. With hooks, we can transition this code to a more &lt;em&gt;functional&lt;/em&gt; approach, with less "boiler-plate" code.&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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;Counter&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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;function&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;setValue&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="nx"&gt;amount&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;decrement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;setValue&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="nx"&gt;amount&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;reset&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nx"&gt;setValue&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="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;increment&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;+&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;decrement&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;-&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;reset&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;Reset&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;The first thing that changes, is that inherently, functions are &lt;strong&gt;stateless&lt;/strong&gt;. In order to circumvent this limitation hooks offload that task to a seperate function: &lt;code&gt;useState&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;So first we must import &lt;code&gt;useState&lt;/code&gt; from the React library:&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;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now instead of setting the initial state in the constructor of the component (functional components do &lt;strong&gt;not&lt;/strong&gt; have constructors), we set state in the constructor of the &lt;code&gt;useState&lt;/code&gt; hook: &lt;code&gt;useState(initialValue)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// old &lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&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="c1"&gt;// providing initial value &lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// new &lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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;Counter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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="c1"&gt;// providing initial value&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike class-based state, &lt;code&gt;state&lt;/code&gt; using hooks can be of any data type (recall that class-based components are hashes). &lt;/p&gt;

&lt;h3&gt;
  
  
  Mutating State
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; hook returns two variables to the caller: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a variable that can be used to access the current state. &lt;/li&gt;
&lt;li&gt;a function that can be used to mutate state. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is up to the developer to name the returned values accordingly, but by convention it is common to name the returned values as 'value' and 'setValue' as &lt;code&gt;[value, setValue]&lt;/code&gt; or in the case of our &lt;code&gt;Counter&lt;/code&gt; component: &lt;code&gt;[count, setCount]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When we want to change state, we have a couple options: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We can directly modify the state, in which we use the &lt;code&gt;setValue&lt;/code&gt; function returned by &lt;code&gt;useState&lt;/code&gt; to mutate the value. &lt;/li&gt;
&lt;li&gt;We can provide a function that mutates the state, in which the function recieves (as an argument) the previous state and the output of that function is the new state. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This would be contrasted with the component based approach, where we must use a function that recieves the previous state as the argument and returns the new state:&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;// Component based approach &lt;/span&gt;
&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;amount&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;So with hooks, we could do the same thing:&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;// functional approach mirroring the component based approach &lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;previousCount&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;previousCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="c1"&gt;// or (shorthand)&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;previousCount&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;previousCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But with functional components we could simplify this even more:&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;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we already have the current count in memory, all we have to do is access that and implement the same logic, since whatever is placed in the &lt;code&gt;setCount&lt;/code&gt; function will be the new value for state. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Gotchas
&lt;/h2&gt;

&lt;p&gt;With class-based state, we typically store data in a hash, we then copy and modify the state in order to mutate it. While we could do this with the &lt;code&gt;useState&lt;/code&gt; hook, it is not recomended. Instead, it is more efficient (and likely more readable) to place each piece of state in seperate state variables:&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;//incorrect: &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;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&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="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="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// correct &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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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;Why? &lt;/p&gt;

&lt;p&gt;This would be best answered by reviewing an example; imaginge we had a component that needed to manage states including &lt;code&gt;count&lt;/code&gt; and &lt;code&gt;color&lt;/code&gt;... To change the color of the component with class-based components we would:&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;changeColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newColor&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;previousState&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;// copy previous state and overwrite color attribute &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;previousState&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="nx"&gt;newColor&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;In functional components, leveraging the &lt;code&gt;useState&lt;/code&gt; hook, it would look like:&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;changeColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newColor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;setColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newColor&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;So why would we use hooks instead of classes? &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When you update state, it is more clear what is changing&lt;/li&gt;
&lt;li&gt;The syntax is more readable and concise&lt;/li&gt;
&lt;li&gt;It is more effecient ( we are not repeatably copying and overwritting data) &lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Woah, I know that could be a lot to grasp at once, so here are the key take aways: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React hooks abstract functionality that would typically require a class-based component into a function that can be used in &lt;strong&gt;any&lt;/strong&gt; component within a React application. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;useState&lt;/code&gt; hook allows us to more simply manage state than the traditional class-based approach. &lt;/li&gt;
&lt;li&gt;If there are multiple pieces of state to be modified, those should be seperated into seperate pieces of state with their own accessor and setter. &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>jsx</category>
      <category>intro</category>
    </item>
    <item>
      <title>The JavaScript Stack Explained</title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Thu, 21 May 2020 22:20:45 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/the-javascript-stack-explained-2pp2</link>
      <guid>https://dev.to/jasonnordheim/the-javascript-stack-explained-2pp2</guid>
      <description>&lt;p&gt;JavaScript code is executed through leveraging two key data structures: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the stack&lt;/li&gt;
&lt;li&gt;the heap &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal in this blog post is to explain and clarify what the JavaScript call stack is. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Stack
&lt;/h2&gt;

&lt;p&gt;A stack is an ordered data structure that keeps track of functions invocations. It is important to note, the JavaScript call stack is part of the JavaScript runtime, you do not directly modify the stack. &lt;/p&gt;

&lt;p&gt;Whenever you invoke a function (in JavaScript) the details of the invocation of that function is saved ("pushed") to the top of the stack. When the function &lt;code&gt;return&lt;/code&gt;s a value, the information regarding the invocation is removed ("popped") from the top of the stack. &lt;/p&gt;

&lt;h3&gt;
  
  
  A Simple Stack Example
&lt;/h3&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;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;    &lt;span class="c1"&gt;// line 1 &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// line 2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;                           &lt;span class="c1"&gt;// line 3 &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multiply&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;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// line 4 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in practice; when a script is executed by the browser, the JavaScript runtime first loads the &lt;code&gt;main&lt;/code&gt; function to the top of the stack; in the example above this would be line 5 (&lt;code&gt;let result = multiply(3,5);&lt;/code&gt;), where the stack records the name of the function and the  location (i.e. line-number) at which the function was invoked. &lt;/p&gt;

&lt;p&gt;At this point the stack would have 1 invocation: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;4 function: main&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As the JavaScript runtime parses the first function call from the top of the JavaScript file (the &lt;code&gt;multiply&lt;/code&gt; function), it then loads ("pushes") the &lt;code&gt;multiply&lt;/code&gt; function to the stack. &lt;/p&gt;

&lt;p&gt;At this points the stack would have 2 invocations: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;4 function: main&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2 funtion: multiply&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This indicates that the call stack, with the first function of &lt;code&gt;main&lt;/code&gt; is waiting on the function of &lt;code&gt;multiply&lt;/code&gt; to resolve, before it can continue. Once the result of &lt;code&gt;multiply(3,5)&lt;/code&gt; has been evaluated, &lt;code&gt;multiply&lt;/code&gt; is removed ("popped") from the call stack. &lt;/p&gt;

&lt;p&gt;Since there is nothing else in the file, the execution of the &lt;code&gt;main&lt;/code&gt; function is considered complete; which means the call stack will now remove the &lt;code&gt;main&lt;/code&gt; function from the stack, resulting in an empty stack/call stack.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Stack Frame
&lt;/h3&gt;

&lt;p&gt;The "Stack Frame" refers to a single element in the JavaScript runtime call stack. Each "Stack Frame" contains the following information: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the function that was invoked. &lt;/li&gt;
&lt;li&gt;the parameters that were passed to the function &lt;/li&gt;
&lt;li&gt;the line-number from which execution of the function started. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, piecing that information together, we can understand that the JavaScript call stack is: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An ordered set of stack frames &lt;/li&gt;
&lt;li&gt;the most recently invoked function would be at the top of the stack&lt;/li&gt;
&lt;li&gt;the bottom of the stack is the first function invoked &lt;/li&gt;
&lt;li&gt;the stack is &lt;strong&gt;always&lt;/strong&gt; processed from top to bottom. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why should you care?
&lt;/h2&gt;

&lt;p&gt;Understanding how the call stack works is fundamental to writing web applications. Not only is it helpful to understand how the JavaScript runtime executes the logic in your application, but if you have ever seen a JavaScript error (or an error in pretty much any language), you've seen a text representation of the call stack (typically called a "stacktrace"), which tells you where your code threw an error, and what events lead up to that errors occurrence. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>An Introduction to JavaScript Classes &amp; Methods</title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Thu, 21 May 2020 17:01:37 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/an-introduction-to-javascript-classes-methods-1pdp</link>
      <guid>https://dev.to/jasonnordheim/an-introduction-to-javascript-classes-methods-1pdp</guid>
      <description>&lt;h1&gt;
  
  
  JavaScript Classes
&lt;/h1&gt;

&lt;p&gt;In programming, classes are used a "blue print" for modeling real-world items in code. &lt;/p&gt;

&lt;p&gt;In JavaScript, the syntax for creating a class could be as simple as:&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;class&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sideLength&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sideLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;area&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sideLength&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;h2&gt;
  
  
  Breaking this down:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First we declared a &lt;code&gt;class&lt;/code&gt; called &lt;code&gt;Square&lt;/code&gt; to represent the idea of a square. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we declared that in order to create a square, we need to know the length of one side of the square. We specified this by defining a &lt;code&gt;constructor&lt;/code&gt; method, which is a special method that "builds" our class when we instantiate it. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;constructor&lt;/code&gt; we took the provided &lt;code&gt;sideLength&lt;/code&gt; and saved it to an instance variable called: &lt;code&gt;sideLength&lt;/code&gt; &lt;br&gt;
a. Variables specified with the keyword &lt;code&gt;this&lt;/code&gt; indicate instance variables &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We also specified a method called &lt;code&gt;area&lt;/code&gt; which takes no parameters/arguments, as denoted by the empty parenthesis &lt;code&gt;area()&lt;/code&gt;. In this method, we specified that the area of the &lt;code&gt;Square&lt;/code&gt; is equal to &lt;code&gt;this.sideLength * this.sideLength&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Instantiating
&lt;/h2&gt;

&lt;p&gt;Once the blue print (&lt;code&gt;class&lt;/code&gt;) has been defined, we create "instances" or specific occurences of that class. In order to instantiate the class we defined above we would use the following syntax:&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;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Square&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This says, store an instance of a square, with a &lt;code&gt;sideLength&lt;/code&gt; equal to &lt;code&gt;5&lt;/code&gt; in a variable we named &lt;code&gt;square&lt;/code&gt;. Now that we have an instance we can use the instance method associated with the &lt;code&gt;Square&lt;/code&gt; class:&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;square&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 25 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Methods Types
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Standard Methods
&lt;/h3&gt;

&lt;p&gt;Recall the &lt;code&gt;Square&lt;/code&gt; class 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;class&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sideLength&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sideLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;area&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sideLength&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sideLength&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;... &lt;code&gt;area()&lt;/code&gt; is a standard class method. Standard methods in JavaScript are available to any instance of the class they to which they are defined. &lt;/p&gt;

&lt;h3&gt;
  
  
  Static Methods
&lt;/h3&gt;

&lt;p&gt;Static methods are class-level methods. In other words, they cannot be called on an instance of a class, but rather they are called on the class itself. Typically, static methods are generic utility methods that have functionality relating to the class, but not an instance of a class. &lt;/p&gt;

&lt;p&gt;To illustrate this, we will use a different 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;class&lt;/span&gt; &lt;span class="nx"&gt;MyMath&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number&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;In the example above, we are defining what it means to &lt;code&gt;square&lt;/code&gt; a number, and what it means to &lt;code&gt;cube&lt;/code&gt; a number. It is important to note, that with static methods, we have no dependence on an instance - or in more technical terms; static methods are stateless. &lt;/p&gt;

&lt;p&gt;To use a static method, we must call upon the method at the class level, &lt;strong&gt;not&lt;/strong&gt; the instance level:&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;squaredNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;MyMath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;square&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;// =&amp;gt; 25 &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cubedNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;MyMath&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cube&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="c1"&gt;// =&amp;gt; 9 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Getters and Setters
&lt;/h3&gt;

&lt;p&gt;In most object-oriented programming (OOP) languages, objects/classes have "getter" and "setter" methods that provide access to instance variables. In most native OOP languages, you have "private variables" that are only accessible within the class themselves. This is designed to protect the state of that object by controlling when and how instance variables are set ("setters") and retrieved ("getters"). &lt;/p&gt;

&lt;p&gt;JavaScript is a functional programming language at its core, which means that it is designed to be written in a procedural style. This is in comparison to OOP languages which model "state" (variables) and "behavior" (methods) within objects after real life. Diving into procedural vs. OOP is out of scope for this topic, but a fundamental programming idea that every modern developer should have a firm grasp around.&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;class&lt;/span&gt; &lt;span class="nx"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&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="k"&gt;this&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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_activityLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// setter method &lt;/span&gt;
    &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nx"&gt;activityLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;level&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;level&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;level&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; 
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_activityLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// getter method &lt;/span&gt;
    &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;return&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; runs &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_activityLevel&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; miles per day`&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;Inherently, "getter" methods (in JavaScript) provide the ability to access an objects internal state, without invoking the method:&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;skipTheDog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Skip&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// =&amp;gt; null &lt;/span&gt;
&lt;span class="nx"&gt;skipTheDog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;activityLevel&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;// =&amp;gt; null &lt;/span&gt;
&lt;span class="nx"&gt;skipTheDog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                     &lt;span class="c1"&gt;// =&amp;gt; `Skip runs 6 miles per day`  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice with the setter (&lt;code&gt;skipTheDog.activityLevel(5);&lt;/code&gt;), we pass in the value we want to use to modify the internal state of the object. Conversely, with getter we do not need to use parenthesis (&lt;code&gt;()&lt;/code&gt;) or "invoke" the method as it is defined as a "getter" method in the class definition. In other words; "getter" methods operate a lot like the properties of an object, except that they do not allow you to change the objects internal state:&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;skipTheDog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Skip&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                &lt;span class="c1"&gt;// =&amp;gt; null &lt;/span&gt;
&lt;span class="nx"&gt;skipTheDog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;activityLevel&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;// =&amp;gt; null &lt;/span&gt;
&lt;span class="nx"&gt;skipTheDog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Skip runs six miles per day`&lt;/span&gt;   &lt;span class="c1"&gt;// =&amp;gt; ERROR &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Per the example above, we can not use "getter" methods to set internal state, so this would throw an error. &lt;/p&gt;

&lt;h1&gt;
  
  
  Glossary of Terms
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Class&lt;/strong&gt; - a blue-print that defines a type of object. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constructor&lt;/strong&gt; - a special method that defines the required parameters to instantiate that class. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instance&lt;/strong&gt; - A specific occurance of an instantiated class. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instantiation&lt;/strong&gt; - The process of creating an "instance" of a class &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Invoke&lt;/strong&gt; - to call execution of a method, function, subroutine or procedure &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method&lt;/strong&gt; - a procedure associated with a class that defines behavior of an object &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parameter&lt;/strong&gt; - an arguement for a method &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>classes</category>
      <category>objects</category>
      <category>methods</category>
    </item>
    <item>
      <title>Key Concepts to get Started with Rails APIs </title>
      <dc:creator>Jason</dc:creator>
      <pubDate>Tue, 21 Apr 2020 15:52:55 +0000</pubDate>
      <link>https://dev.to/jasonnordheim/key-concepts-to-get-started-with-rails-apis-22df</link>
      <guid>https://dev.to/jasonnordheim/key-concepts-to-get-started-with-rails-apis-22df</guid>
      <description>&lt;h1&gt;
  
  
  What is an API?
&lt;/h1&gt;

&lt;p&gt;Well first, an API is an “Application Programming Interface”, and it is a way for one application to “talk” to another. Of course, you have to speak the same language ( &lt;code&gt;.json&lt;/code&gt; , &lt;code&gt;.csv&lt;/code&gt; , &lt;code&gt;.txt&lt;/code&gt; ). And  it is through API’s that most web applications are built. So if you want to do full-stack engineering, you need to know API’s. &lt;/p&gt;

&lt;p&gt;My goal here is to share the concepts that I believe are key to understanding API’s in Ruby. I am sure that there are many tutorials that will show you exactly how to do it, so I will not re-hash that here; here my goal is to help you understand how an API works conceptually. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The trick to forgetting the big picture is to look at everything close-up. - Chuck Palahniuk&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Programming Paradigms&lt;/li&gt;
&lt;li&gt;Model View Controller &lt;/li&gt;
&lt;li&gt;Web Concepts&lt;/li&gt;
&lt;li&gt;HTTP Requests &lt;/li&gt;
&lt;li&gt;Routing &lt;/li&gt;
&lt;li&gt;Rendering &lt;/li&gt;
&lt;li&gt;Static&lt;/li&gt;
&lt;li&gt;Dynamic &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Model View Controller
&lt;/h2&gt;

&lt;p&gt;Starting if with a definition; Model-View-Controller or MVC is an architectural software engineering design pattern in which we decouple different functions of an application or in programmer talk - separate our concerns. In this case; we are separating the concerns of modeling data into three broad categories; &lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Model&lt;/strong&gt; - as a verb it would be “modeling”, this functional group is responsible for “modeling” or data. These are the objects that hold and organize our data. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Controller&lt;/strong&gt; - as the name implies, this functional layer serves as a sort of router, designated the responsibility of communicating with the models to work with the application data. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;View&lt;/strong&gt; - the view assumes responsibility of displaying content to a user by interpreting the information received from the controller. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is key to understand the distinctions here; a view is only responsible for display functions, a model is responsible for representing our data, and a controller contains the logic for interaction between the two other layers. &lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP and the Internet
&lt;/h2&gt;

&lt;p&gt;HTTP stands for Hyper-text transport protocol (a web communication protocol) and is the standard along with HTTPS or Hyper-text transport protocol secure. In programming lingo; think of HTTP as an object that has methods and functions that allow us to send messages and read messages over the internet. &lt;/p&gt;

&lt;p&gt;Like most things in the world of systems, there are pieces to an HTTP command; a &lt;strong&gt;header&lt;/strong&gt;, a &lt;strong&gt;body&lt;/strong&gt;, and a &lt;strong&gt;footer&lt;/strong&gt;. The header will have things like who sent the request, what type of request it is (more on that later), the path of the resource they wish to issue the command to, and more, but those are they key things. Next there’s the body, which will have the “payload” of the command, and lastly there is a footer that denotes that that is the end of the HTTP command.&lt;/p&gt;

&lt;p&gt;Starting with the types of HTTP; there are a few core types that mirror CRUD (create, read, update and delete) functions. &lt;/p&gt;

&lt;p&gt;In HTTP lingo;&lt;br&gt;
• "create" would correspond to a &lt;code&gt;PUT&lt;/code&gt; HTTP command.&lt;br&gt;&lt;br&gt;
• "read" would correspond to a &lt;code&gt;GET&lt;/code&gt; HTTP command. &lt;br&gt;
• "update" would correspond to a &lt;code&gt;PATCH&lt;/code&gt; HTTP command. &lt;br&gt;
• "delete" would correspond to the &lt;code&gt;DELETE&lt;/code&gt; HTTP command. &lt;/p&gt;

&lt;p&gt;There are additional (less common) types of HTTP commands like; &lt;br&gt;
• &lt;code&gt;TRACE&lt;/code&gt; , which will echo back the last received request &lt;br&gt;
• &lt;code&gt;OPTIONS&lt;/code&gt; , which will return the HTTP methods supported by the server &lt;br&gt;
• &lt;code&gt;CONNECT&lt;/code&gt; , which will convert the command to a TCP/IP tunnel (generally for SSL)&lt;br&gt;
• &lt;code&gt;HEAD&lt;/code&gt; , which asks for a response (like a &lt;code&gt;GET&lt;/code&gt; but without the body) &lt;/p&gt;

&lt;p&gt;These allow servers to send and retrieve information and are the key building blocks of API’s. &lt;/p&gt;

&lt;h2&gt;
  
  
  Routing
&lt;/h2&gt;

&lt;p&gt;The next fundamental concepts of Ruby API’s is the concept of routing. Personally, I like to think of Routing just like a network router that most of us have in our home. It takes a HTTP command (or request) and directs it to the appropriate resource. However, unlike network switches/routers, Ruby routes, referring to directing traffic from an endpoint which is how most websites work. &lt;/p&gt;

&lt;p&gt;When you go to &lt;code&gt;http://www.cnn.com/&lt;/code&gt;, your browser is sending a &lt;code&gt;GET&lt;/code&gt; request to that DNS (Domain Naming Service) resolves that to an IP address, which your browser then sends an HTTP &lt;code&gt;GET&lt;/code&gt; command. In this case, no authentication is required, and the website responds with and HTTP response (request --&amp;gt; response) whose body (think "payload") carries the HTML from the requested page. &lt;/p&gt;

&lt;p&gt;In the example of cnn.com, routing was implied: since nothing followed the &lt;code&gt;.com&lt;/code&gt;, we simply wanted the index page. When you want to go to a more specific resource at that IP address like &lt;code&gt;https://www.cnn.com/us&lt;/code&gt;... the traffic will be directed by DNS to the same server, however that server will respond different as the &lt;code&gt;GET&lt;/code&gt; command has refined the information it is requesting . This is the fundamental concept of routing. It involves interpreting an HTTP request and directing it to a controller which can process that request. &lt;/p&gt;

&lt;h2&gt;
  
  
  Rendering
&lt;/h2&gt;

&lt;p&gt;Let’s start with a definition; [technopedia’s(&lt;a href="https://www.techopedia.com/definition/9163/rendering"&gt;https://www.techopedia.com/definition/9163/rendering&lt;/a&gt;) definition was the first one I found that I felt concisely describes rendering in the context of the web:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rendering is the process involved in the generation of a two-dimensional or three-dimensional image from a model by means of application programs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here, what I am referring to by "rendering" is the act of interpreting web code (most commonly in the form of HTML, CSS and JavaScript) and generating or retrieving the requested information.&lt;/p&gt;

&lt;p&gt;In the case of web applications, it can be as simple as delivering an HTML page. This is typically referred to as &lt;strong&gt;static rendering&lt;/strong&gt;. Well this works fine for “about us” pages, more elaborate sites will display different depending on a variety of factors, this we call dynamic rendering. &lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it all together
&lt;/h2&gt;

&lt;p&gt;So now that we understand how the internet works (HTTP commands, requests and responses) and how those requests are routed and rendered. Let’s put all those pieces together and talk about how it fits into the MVC  software design pattern and more specifically Ruby (on Rails). &lt;/p&gt;

&lt;p&gt;At a high level, an HTTP &lt;code&gt;GET&lt;/code&gt; command is sent to a web resource, requesting to read data. That resources processes HTTP command/request and routes it to the appropriate resource. Assuming the requestor has adequate permissions, the resource will then pull up (think “read”) the requested information with an HTTP response and the body of the HTTP response will have the payload or information requested by the user. &lt;/p&gt;

&lt;p&gt;Now let’s put that in context of MVC and Ruby on Rails with an example: &lt;/p&gt;

&lt;p&gt;A resources makes a HTTP &lt;code&gt;GET&lt;/code&gt; command to a resource we will call myapp.com . That resources receives the requests and routes the traffic based upon the directions supplied in the routing file (&lt;code&gt;routes.rb&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BS07lDZI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mke6xc5l0qbkfmyz15ii.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BS07lDZI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mke6xc5l0qbkfmyz15ii.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The routes file describes what to do depending on the request. Here we are requesting (&lt;code&gt;GET&lt;/code&gt;) a document called &lt;code&gt;’myapp.com/about’&lt;/code&gt; which in Rails means that I want to &lt;code&gt;GET&lt;/code&gt; or “read” data from the &lt;code&gt;“about”&lt;/code&gt; page. In this example, I am simply going to pass this to a &lt;code&gt;StaticContoller&lt;/code&gt; by directing the traffic &lt;code&gt;to: static#about&lt;/code&gt; , which says &lt;br&gt;
pass this traffic “to the &lt;code&gt;StaticContoller&lt;/code&gt; and call the &lt;code&gt;about&lt;/code&gt; method" (or “action” in Rails lingo). and passes the HTTP command onto the relevant controller (the “C” in MVC). &lt;/p&gt;

&lt;p&gt;The controllers job is to process that request by using the &lt;strong&gt;Models&lt;/strong&gt; to represent application data and then call upon a &lt;strong&gt;View&lt;/strong&gt; to display that data to the user complete with the information requested with the initial HTTP command. &lt;/p&gt;

&lt;p&gt;There’s a lot more to understanding how this works, as Rails is a framework that is built leveraging conventions (marketed as “convention over configuration”), which simply means that these pieces are all “wired” together based on naming conventions. &lt;/p&gt;

&lt;h1&gt;
  
  
  How does this all fit in with APIs?
&lt;/h1&gt;

&lt;p&gt;In the case of Rails applications, all of the above process holds true, but we exclude the “View” part (mostly). In the case of an API, another application is requesting information, and the job of the API is to return that information in a mutually agreed upon language; which in most cases in json. &lt;/p&gt;

&lt;p&gt;Here if we are making a HTTP &lt;code&gt;GET&lt;/code&gt; request, we want to “read” information; so the Rails application would still be routing that request to a controller, and the controller would still be interacting with one or more of the application models, but the last part differs. Unlike a standard web page, an API request would be expecting different content in the body of the HTTP response, which is often text formatted as JSON or (JavaScript Object Notation). In other words, the “View” here would be simply JSON data, no HTML or CSS. &lt;/p&gt;

</description>
      <category>rails</category>
      <category>api</category>
      <category>ruby</category>
      <category>mvc</category>
    </item>
  </channel>
</rss>
