<?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>Forem: Chuck Choi</title>
    <description>The latest articles on Forem by Chuck Choi (@chuckchoiboi).</description>
    <link>https://forem.com/chuckchoiboi</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%2F615794%2Faeebe807-24a2-4f29-ae0d-cfd45bcae517.jpg</url>
      <title>Forem: Chuck Choi</title>
      <link>https://forem.com/chuckchoiboi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/chuckchoiboi"/>
    <language>en</language>
    <item>
      <title>Data Structure Series: Hash Table</title>
      <dc:creator>Chuck Choi</dc:creator>
      <pubDate>Mon, 16 Aug 2021 22:28:31 +0000</pubDate>
      <link>https://forem.com/chuckchoiboi/data-structure-series-hash-table-5h16</link>
      <guid>https://forem.com/chuckchoiboi/data-structure-series-hash-table-5h16</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xpt6in1hml7m89611hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xpt6in1hml7m89611hj.png" alt="data-structure-series-intro"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use forks to eat pasta, spoons to eat soup, and chopsticks to eat dumplings. Each silverwares have its advantages/disadvantages, hence working better than the other for the food that it interacts well with. &lt;strong&gt;Just like that, different data structures are better suited and perform better than the others based on the situations/use cases.&lt;/strong&gt; They each have their pros and cons. Understanding these pros and cons can help you be a better programmer, as it will allow you to choose an appropriate data structure(s) based on the circumstances/goals you have, and it helps to drastically improve the performance of the algorithm being applied. I will be putting these blog series together on well known programming data structures in JavaScript, and link them all in one blog post in the future. Feel free to leave a comment if you have any questions!&lt;/p&gt;




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

&lt;p&gt;&lt;strong&gt;1. What Is Hash Table?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Hash Function&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Implementation in JavaScript&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Helper Functions&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;5. Handling Collisions&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;6. Big O&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;7. Helpful Resources&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  1. What is Hash Table? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Hash Table&lt;/strong&gt; is a data structure of associative array that stores key/value paired data into buckets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Considered being one of the most important data structures in computing, &lt;strong&gt;Hash Table&lt;/strong&gt; is used in many areas of applications: &lt;em&gt;password verifications&lt;/em&gt;, &lt;em&gt;cryptography&lt;/em&gt;, &lt;em&gt;compilers&lt;/em&gt;, and the list goes on. Due to its efficiency and speed in &lt;strong&gt;searching&lt;/strong&gt;, &lt;strong&gt;insertion&lt;/strong&gt;, and &lt;strong&gt;removal&lt;/strong&gt; of data, it is a widely applicable and preferred data structure in many cases. &lt;strong&gt;A Hash Table is a data structure of associative array that stores data as a key/value pair into a bucket&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8hxv3euxnoqmdeqv0puc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8hxv3euxnoqmdeqv0puc.png" alt="hash-table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How &lt;strong&gt;Hash Table&lt;/strong&gt; works is that it takes a &lt;strong&gt;key&lt;/strong&gt; and a &lt;strong&gt;value&lt;/strong&gt; as inputs, then runs the key through a &lt;strong&gt;hash function&lt;/strong&gt; that turns it into an &lt;strong&gt;index&lt;/strong&gt;. This process is called &lt;strong&gt;hashing&lt;/strong&gt;. The index is used to map the value of the input into the table's bucket. &lt;strong&gt;The hash function is irreversible, which makes it secure and reliable.&lt;/strong&gt; It is possible for two different keys to result in the same index though, and this is called a &lt;strong&gt;collision.&lt;/strong&gt; A collision can override the previous key's placeholder if not handled. There are various ways to handle hash collisions though -- &lt;strong&gt;Separate Chaining&lt;/strong&gt; being one of them which often uses Linked Lists inside the bucket to store multiple data in the same index. We will get into it later in this post. But first, let's discuss how &lt;strong&gt;hash function&lt;/strong&gt; works in a nutshell.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. Hash Function &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The hashing process in computing is like hashing a potato to make hashed brown. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57u46j9c44c4lcy37aa8.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57u46j9c44c4lcy37aa8.jpg" alt="potato"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash functions, or hashing algorithms generate a fixed-length result from a given input.&lt;/strong&gt; This process is called &lt;strong&gt;hashing&lt;/strong&gt;. The fixed-length result is used in Hash Tables as an index to map the input into a hash bucket. The hashing process in computing is like hashing a potato to make hashed brown. You could think of potato as key input, grater as hash function, and shredded potato as index that is hashed out as a result of the hash function. Just like how you can't turn shredded potato back into a whole potato, &lt;strong&gt;hash functions are irreversible -- it's a one way algorithm.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is an example of a hash function in JavaScript:&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;hash&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;size&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;hashedKey&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="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;key&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="nx"&gt;hashedKey&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;hashedKey&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This function accepts two arguments: string &lt;code&gt;key&lt;/code&gt; to hash, and &lt;code&gt;size&lt;/code&gt; of hash buckets&lt;/li&gt;
&lt;li&gt;Initialize a variable named &lt;code&gt;hashedKey&lt;/code&gt; as 0 to return at the end&lt;/li&gt;
&lt;li&gt;Iterate each of the string's characters to sum up their character codes&lt;/li&gt;
&lt;li&gt;After the iteration, use &lt;strong&gt;modulo operation (%)&lt;/strong&gt; to find the remainder of the &lt;code&gt;hashedKey / size&lt;/code&gt; and set it as new &lt;code&gt;hashedKey&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;hashedKey&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;&lt;br&gt;
In the above algorithm, we are initializing a variable &lt;code&gt;hashedKey&lt;/code&gt; as &lt;code&gt;0&lt;/code&gt;. The value of this variable will change based on the string and be returned as a result of this function. We need a way to represent each of the letters into numbers, this way the matching string key that goes through the function will always convert to the same integer. JavaScript's string method &lt;code&gt;charCodeAt()&lt;/code&gt; allows us to convert a string character into an integer representing the UTF-16 code unit.&lt;/p&gt;

&lt;p&gt;With that being said, we are using a for loop to iterate every character of the key input. For each character being iterated, we are using the &lt;code&gt;charCodeAt()&lt;/code&gt; method to convert the character and add it to &lt;code&gt;hashedKey&lt;/code&gt; variable we defined at the beginning. Once we sum up all the integers that represents each characters, we execute an modulo operation &lt;code&gt;%&lt;/code&gt; using the &lt;code&gt;size&lt;/code&gt; of the bucket (second argument of the function) as a divisor. Modulo operation not only guarantees that the resulting integer is in range between 0 to the size of the bucket, but also makes the result irreversible.&lt;/p&gt;

&lt;p&gt;This is a very simple and basic hash function that can be improved better. I recommend you to check out &lt;a href="https://www.freecodecamp.org/news/md5-vs-sha-1-vs-sha-2-which-is-the-most-secure-encryption-hash-and-how-to-check-them/" rel="noopener noreferrer"&gt;this blog post&lt;/a&gt; if you are interested in learning about different hash functions designed by mathematicians and computer scientists around the world. Now it is time to implement the hash table in JavaScript!&lt;/p&gt;


&lt;h2&gt;
  
  
  3. Implementation in JavaScript &lt;a&gt;&lt;/a&gt;
&lt;/h2&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="nc"&gt;HashTable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;53&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;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;size&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;buckets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;_hash &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;hashedKey&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="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;key&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="nx"&gt;hashedKey&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;hashedKey&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;size&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;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashTable&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;table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// HashTable {size: 53, buckets: Array(53)}&lt;/span&gt;

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

&lt;/div&gt;


&lt;p&gt;The above &lt;strong&gt;Hash Table&lt;/strong&gt; class has two properties:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;size&lt;/code&gt;: the number representing the &lt;code&gt;size&lt;/code&gt; of the buckets, and we are using prime number 53 as a default value (choosing a prime number for the hash table's size reduces the chances of collisions)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;buckets&lt;/code&gt;: &lt;code&gt;buckets&lt;/code&gt; are the placeholders for each data (key/value pair), and we are using &lt;code&gt;Array&lt;/code&gt; class to create an empty array with size of 53 indices&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And we have the &lt;code&gt;_hash&lt;/code&gt; method similar to what we created earlier, but only difference is that it is not taking in the &lt;code&gt;size&lt;/code&gt; as second argument since we are using the &lt;code&gt;size&lt;/code&gt; of the object created from the &lt;code&gt;Hash Table&lt;/code&gt; class. With this, we can create an object with buckets array that contains default &lt;code&gt;size&lt;/code&gt; of 53 indices or a specified &lt;code&gt;size&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's go ahead and add some methods to this Hash Table!&lt;/p&gt;


&lt;h2&gt;
  
  
  4. Helper Functions &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;set()&lt;/strong&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;// adds key-value pair into hash table's bucket&lt;/span&gt;
&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&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="nf"&gt;_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts a &lt;code&gt;key&lt;/code&gt; and a &lt;code&gt;value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Hashes the &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Stores the key-value pair in the hash bucket&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;get()&lt;/strong&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;// retrieves the value of the key from its respective bucket&lt;/span&gt;
&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&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="nf"&gt;_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="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;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// returns value of the key&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts a &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Hashes the &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Retrieves the key-value pair in the hash bucket&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;remove()&lt;/strong&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;// removes the key-value pair from the hash table's bucket&lt;/span&gt;
&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&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="nf"&gt;_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;deleted&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;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;delete&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;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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;deleted&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts a &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Hashes the &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Retrieves the key-value pair in the hash bucket and stores it&lt;/li&gt;
&lt;li&gt;Delete the key-value pair in the hash bucket (use &lt;code&gt;delete&lt;/code&gt; operator to empty the element, doesn't affect the array size)&lt;/li&gt;
&lt;li&gt;Returns the stored key-value pair&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;All the helper functions in this data structure is fairly simple -- they all utilize the hash function we defined earlier to retrieve the &lt;code&gt;index&lt;/code&gt; that is associated with the &lt;code&gt;key&lt;/code&gt; passed, and access the array's element in that &lt;code&gt;index&lt;/code&gt;. There's a problem with these methods though. What happens if the hash function returns the same index for two different inputs? Our hash function is fairly simple so this will happen for sure. If so, it will override the bucket that is already occupied or &lt;code&gt;get&lt;/code&gt; method will retrieve a wrong value that we aren't looking for. How can we improve these helper methods to handle the collisions?&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Handling Collisions &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7sqjh5yeaaolafcdf9k5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7sqjh5yeaaolafcdf9k5.png" alt="collision"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we discussed earlier, it is possible for a hash function to produce collisions: returning the same index for multiple different keys. &lt;strong&gt;Unfortunately, even under the best of circumstances, collisions are nearly unavoidable.&lt;/strong&gt; Any hash function with more inputs than outputs will necessarily have such collisions; the harder they are to find, the more secure the hash function is.&lt;/p&gt;

&lt;p&gt;There are multiple ways to handle collisions though, and the two common techniques are &lt;strong&gt;Separate Chaining&lt;/strong&gt; and &lt;strong&gt;Linear Probing&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Separate Chaining:&lt;/strong&gt; If there is only one hash code pointing to an index of array then the value is directly stored in that index. If hash code of second value also points to the same index though, then we replace that index value with a linked list or array and all values pointing to that index are stored in the list. Same logic is applied while retrieving the values, we will have to iterate all the elements inside a bucket if the bucket stores multiple key-value pairs. &lt;strong&gt;In short, separate chaining creates a list-like object inside a bucket to store multiple data with collisions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linear Probing:&lt;/strong&gt; &lt;strong&gt;Linear Probing technique works on the concept of keep incrementing the hashed index until you find an empty bucket.&lt;/strong&gt; Thus, Linear Probing takes less space than Separate Chaining , and perform significantly faster than Separate Chaining (since we don't have to loop through lists inside buckets).&lt;/p&gt;

&lt;p&gt;Although &lt;strong&gt;Separate Chaining&lt;/strong&gt; is significantly less efficient than &lt;strong&gt;Linear Probing&lt;/strong&gt;, it is easier to implement. Here's how we can improve the helper methods we defined by utilizing &lt;strong&gt;Separate Chaining&lt;/strong&gt; (we will use &lt;strong&gt;Array&lt;/strong&gt; instead of &lt;strong&gt;Linked List&lt;/strong&gt; for simplicity):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;set()&lt;/strong&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;// adds key-value pair into hash table's bucket&lt;/span&gt;
&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&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="nf"&gt;_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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="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;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts a &lt;code&gt;key&lt;/code&gt; and a &lt;code&gt;value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Hashes the &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the hash bucket is empty, set it as an empty array&lt;/li&gt;
&lt;li&gt;Push the key-value pair in the array inside the bucket&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;get()&lt;/strong&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;// retrieves the value of the key from its respective bucket&lt;/span&gt;
&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&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="nf"&gt;_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&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;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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="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="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;undefined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts a &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Hashes the &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the bucket is truthy, iterate each key-value pair inside the bucket&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;key&lt;/code&gt; matches the pair, return the &lt;code&gt;value&lt;/code&gt; of the pair&lt;/li&gt;
&lt;li&gt;return &lt;code&gt;undefined&lt;/code&gt; if the bucket is empty&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;remove()&lt;/strong&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;// removes the key-value pair from the hash table's bucket&lt;/span&gt;
&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&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="nf"&gt;_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&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;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buckets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&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;i&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="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;strong&gt;Pseudocode:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accepts a &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Hashes the &lt;code&gt;key&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the bucket is truthy, iterate each key-value pair inside the bucket&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;key&lt;/code&gt; matches the pair, remove the pair and return it&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Big O &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpoenpbx11rdd9ly5wd8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpoenpbx11rdd9ly5wd8w.png" alt="big-o"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Space complexity of this data structure is linear: as the size of the buckets increase, so does the space&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Set/Get/Remove:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Average: &lt;em&gt;O(1) Time Complexity&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Worst Case: &lt;em&gt;O(n) Time Complexity&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;All these helper methods use hash function to look up the indices. Hash function takes constant time, but the time complexity can get linear with buckets with multiple elements due to collisions. More items will mean more time to look inside the bucket, hence taking linear time (&lt;em&gt;O(n)&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Helpful Resources &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/" rel="noopener noreferrer"&gt;&lt;strong&gt;Online Course&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(Udemy Course)&lt;/strong&gt;&lt;br&gt;
Check out this Udemy course named JavaScript Algorithms and Data Structures Masterclass! It is created by &lt;a href="https://www.youtube.com/c/ColtSteeleCode" rel="noopener noreferrer"&gt;Colt Steele&lt;/a&gt;, and I referenced his code for the data structure implementation part of this blog post. Personally, I didn't know where to start with algorithms and data structures especially coming from a non-tech background. This course is very well structured for beginners to build a foundation on these topics.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://visualgo.net/en" rel="noopener noreferrer"&gt;&lt;strong&gt;Visual Animation&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(VisuAlgo)&lt;/strong&gt;&lt;br&gt;
Data structures can be difficult to comprehend for some people just by looking at the code/text. The instructor in the course above uses a website named VisuAlgo that has visual representation of algorithms and data structures through animation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.interviewcake.com/data-structures-reference" rel="noopener noreferrer"&gt;&lt;strong&gt;Data Structure Cheat Sheet&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(Interview Cake)&lt;/strong&gt;&lt;br&gt;
Also, here's a really well-summarized cheat sheet/visualizations on data structures.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=h2d9b_nEzoA" rel="noopener noreferrer"&gt;&lt;strong&gt;CS50's Hash Tables Lesson&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(YouTube Video)&lt;/strong&gt;&lt;br&gt;
I came across this YouTube video thanks to one of the DEV Community users Alex @tinydev! It's part of Harvard's CS50 course, and they do a great job explaining Hash Tables.&lt;/p&gt;

</description>
      <category>datastructure</category>
      <category>hashtable</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Data Structure Series: Stack &amp; Queue</title>
      <dc:creator>Chuck Choi</dc:creator>
      <pubDate>Wed, 11 Aug 2021 14:39:26 +0000</pubDate>
      <link>https://forem.com/chuckchoiboi/data-structure-series-stack-queue-1jop</link>
      <guid>https://forem.com/chuckchoiboi/data-structure-series-stack-queue-1jop</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r3s65hg5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xpt6in1hml7m89611hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r3s65hg5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xpt6in1hml7m89611hj.png" alt="data-structure-series-intro"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use forks to eat pasta, spoons to eat soup, and chopsticks to eat dumplings. Each silverwares have its advantages/disadvantages, hence working better than the other for the food that it interacts well with. &lt;strong&gt;Just like that, different data structures are better suited and perform better than the others based on the situations/use cases.&lt;/strong&gt; They each have their pros and cons. Understanding these pros and cons can help you be a better programmer, as it will allow you to choose an appropriate data structure(s) based on the circumstances/goals you have, and it helps to drastically improve the performance of the algorithm being applied. Feel free to leave a comment if you have any questions!&lt;/p&gt;




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

&lt;p&gt;&lt;strong&gt;1. What are Stacks and Queues?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Implementation in JavaScript&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Implementation Using Linked List&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Big O&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;5. Helpful Resources&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  1. What are Stacks and Queues? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Stacks and Queues are two types of linear data structures with a main difference in their data management principle: LIFO (Last-In, First-Out) for Stacks, and FIFO (First-In, First-Out) for Queues.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Stack
&lt;/h3&gt;

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

&lt;p&gt;&lt;strong&gt;A Stack is a linear data structure that follows LIFO (Last-In, First-Out) principle.&lt;/strong&gt; With &lt;strong&gt;LIFO&lt;/strong&gt; principle, whatever the data came in last would be the first to be taken out. An example you'd be familiar with would be the &lt;strong&gt;Undo functionality&lt;/strong&gt; in a text editor like Word Processor. In a Word document, the Undo command undoes any last action you do, which includes formatting text, moving blocks, typing and deleting text, formatting, and etc. Using the Undo command until the end will eventually take you to a blank page where you started like a &lt;strong&gt;Stack&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S9cZud2h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqxo0tn2txybwehpujkp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S9cZud2h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqxo0tn2txybwehpujkp.jpg" alt="rock-a-stack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I remember I used to play with this plastic toy named "Rock-A-Stack". This toy comes with with a base with a center cone on top, and multiple colorful plastic rings in different sizes. Your goal is to stack the rings on top of the base in the order of the size from biggest to smallest to form a pyramid shape. The ring cannot be taken out from the bottom because of the base, you will have to take out the whatever the ring is at the topmost position of the stack in order to re-arrange the order. A &lt;strong&gt;Stack&lt;/strong&gt; in the programming world is fundamentally no different than the toy Rock-A-Stack. &lt;/p&gt;
&lt;h2&gt;
  
  
  Queue
&lt;/h2&gt;

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

&lt;p&gt;&lt;strong&gt;A Queue is also a linear data structure, but follows FIFO (First-In, First-Out) principle.&lt;/strong&gt; With &lt;strong&gt;FIFO&lt;/strong&gt; principle, whatever the data came in first would be the first to be taken out. A &lt;strong&gt;printer queue&lt;/strong&gt; is a good example of a &lt;strong&gt;Queue&lt;/strong&gt; data structure. In an office setting where one or few printers are shared by multiple people, queue makes sure that the printing tasks are executed in the chronological order they came in. Even if you were to use a printer at home and print multiple instances of document pages, it pushes the tasks in a queue. Let's say you forgot to turn on the printer, the queue will make sure that the printing tasks are not lost, but will execute each tasks as a queue so that first printing task will be executed first once the printer is turned on.&lt;/p&gt;

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

&lt;p&gt;A real life example would be a &lt;strong&gt;security scanning line at the TSA&lt;/strong&gt;, or any other lines like at an amusement park, restaurant, and etc. Nobody likes it when someone cuts the line. You have to wait until your turn comes. If you are the first one to arrive at the TSA line, you will go through the security scanning first. That's a Queue right there, First-In, First-Out.&lt;/p&gt;

&lt;p&gt;In Summary, Stacks and Queues are two types of linear data structures with a main difference in their data management principle: LIFO (Last-In, First-Out) for Stacks, and FIFO (First-In, First-Out) for Queues.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. Implementation Using Array &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Stacks and Queues can simply be implemented using a built in &lt;em&gt;Array&lt;/em&gt; in JavaScript. For &lt;strong&gt;Stacks&lt;/strong&gt;, you just need to use Array's &lt;code&gt;push()&lt;/code&gt; and &lt;code&gt;pop()&lt;/code&gt; methods to add and element at the end of an array, and remove the element at the end. For &lt;strong&gt;Queues&lt;/strong&gt;, you will have to use &lt;code&gt;push()&lt;/code&gt; method to add an element at the end, but use &lt;code&gt;shift()&lt;/code&gt; to take out the first element that was pushed in. They will look something like this:&lt;/p&gt;
&lt;h3&gt;
  
  
  Stack
&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;const&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;stack&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;Baseball&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;stack&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;Soccer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;stack&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;Football&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;stack&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;Basketball&lt;/span&gt;&lt;span class="dl"&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;stack&lt;/span&gt; &lt;span class="c1"&gt;// ["Baseball", "Soccer", "Football", "Basketball"]&lt;/span&gt;

&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns "Basketball"&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;stack&lt;/span&gt; &lt;span class="c1"&gt;// ["Baseball", "Soccer", "Football"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Queue
&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;const&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;queue&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;Peanut Butter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;queue&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;Milk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;queue&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;Apple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;queue&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;Cheese&lt;/span&gt;&lt;span class="dl"&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;queue&lt;/span&gt; &lt;span class="c1"&gt;// ["Peanut Butter", "Milk", "Apple", "Cheese"]&lt;/span&gt;

&lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns "Peanut Butter"&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt; &lt;span class="c1"&gt;// ["Milk", "Apple", "Cheese"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is totally easy and convenient for &lt;strong&gt;Stacks&lt;/strong&gt;. &lt;strong&gt;But there is a downside of implementing a Queue using Array&lt;/strong&gt;. Can you guess what it is? &lt;code&gt;push()&lt;/code&gt; and &lt;code&gt;pop()&lt;/code&gt; methods have time complexity of O(1) while &lt;code&gt;shift()&lt;/code&gt; and &lt;code&gt;unshift()&lt;/code&gt; methods have time complexity of O(N). This is because when you are adding or removing an element of an array, all the elements to the right side of that element has to rearrange their position, thus indices of those get reassigned. &lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;shift()&lt;/code&gt; and &lt;code&gt;unshift()&lt;/code&gt; are pretty costly in &lt;strong&gt;Array&lt;/strong&gt;, let's see if there's a way to optimize &lt;strong&gt;Stacks&lt;/strong&gt; and &lt;strong&gt;Queues&lt;/strong&gt;. Aha! &lt;strong&gt;Linked Lists&lt;/strong&gt; are great at inserting/deleting the first and the last element! If you remember how &lt;strong&gt;Linked List&lt;/strong&gt; works, a Linked List is a collection of data in a sequence, with each of the data referencing its next node (or previous node if it is a Doubly Linked List) from its &lt;code&gt;head&lt;/code&gt; to the &lt;code&gt;tail&lt;/code&gt;. For us to visualize it better using Stacks &amp;amp; Queues, we will call the pointers &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;last&lt;/code&gt; instead of &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Singly Linked Lists'&lt;/strong&gt; nodes reference their next node but not their previous. Adding a new &lt;code&gt;first&lt;/code&gt; node to a &lt;strong&gt;Singly Linked List&lt;/strong&gt; is quick, we just need to replace the new &lt;code&gt;first&lt;/code&gt;, and set its &lt;code&gt;next&lt;/code&gt; node to the old &lt;code&gt;first&lt;/code&gt; node. Removing the current &lt;code&gt;first&lt;/code&gt; node is quick as well, we just need to remove the current &lt;code&gt;first&lt;/code&gt; node, and set its next node as the new &lt;code&gt;first&lt;/code&gt; node. &lt;strong&gt;This makes Singly Linked List a perfect candidate for Stacks to follow LIFO (Last-In, First-Out) principle.&lt;/strong&gt; However, if we were to add a new node to a queue (&lt;em&gt;enqueue&lt;/em&gt;) and remove the last node (&lt;em&gt;dequeue&lt;/em&gt;) using &lt;strong&gt;Singly Linked List&lt;/strong&gt;, it will not be efficient to &lt;em&gt;dequeue&lt;/em&gt; the last node. This is because a &lt;strong&gt;Singly Linked List's&lt;/strong&gt; node does not reference its previous node, so we will have to traverse the entire list to find out what the previous node of &lt;code&gt;last&lt;/code&gt; node is. The previous node of &lt;code&gt;last&lt;/code&gt; node will need to be reassigned as the new &lt;code&gt;last&lt;/code&gt; node. &lt;strong&gt;Thus, a Queue will be more optimized to utilize Doubly Linked List over Singly Linked List.&lt;/strong&gt; Check out the code below:&lt;/p&gt;


&lt;h2&gt;
  
  
  3. Implementation Using Linked List &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Stack&lt;/strong&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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Node&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;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&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;value&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;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Stack&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&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="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// push() method adds a new node at the top (first)&lt;/span&gt;
    &lt;span class="nx"&gt;push&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;Node&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;first&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;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;oldNode&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;first&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;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// pop() method removes a node at the top (first)&lt;/span&gt;
    &lt;span class="nx"&gt;pop&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="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;first&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removedNode&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;first&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&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;last&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;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="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;first&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;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&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;size&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;removedNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode for &lt;code&gt;push()&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function should accept a value&lt;/li&gt;
&lt;li&gt;Create a new node with that value&lt;/li&gt;
&lt;li&gt;If there are no nodes in the stack, set the first and last property to be the newly created node&lt;/li&gt;
&lt;li&gt;If there is at least one node, create a variable that stores the current first property on the stack&lt;/li&gt;
&lt;li&gt;Reset the first property to be the newly created node&lt;/li&gt;
&lt;li&gt;Set the next property on the node to be the previously created variable&lt;/li&gt;
&lt;li&gt;Increment the size of the stack by 1, and return it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pseudocode for &lt;code&gt;pop()&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there are no nodes in the stack, return null&lt;/li&gt;
&lt;li&gt;Create a temporary variable to store the first property on the stack&lt;/li&gt;
&lt;li&gt;If there is only 1 node, set the first and last property to be null&lt;/li&gt;
&lt;li&gt;If there is more than one node, set the first property to be the next property on the current first&lt;/li&gt;
&lt;li&gt;Decrement the size by 1&lt;/li&gt;
&lt;li&gt;Return the value of the node removed&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Queue&lt;/strong&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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Queue&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&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="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// enqueue() method adds a new node at the end (last)&lt;/span&gt;
    &lt;span class="nx"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;Node&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;first&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;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// dequeue() method removes a node at the beginning (first)&lt;/span&gt;
    &lt;span class="nx"&gt;dequeue&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="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;first&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;removedNode&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;first&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&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;last&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;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="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;first&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;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&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;size&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;removedNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pseudocode for &lt;code&gt;enqueue()&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This function accepts a value&lt;/li&gt;
&lt;li&gt;Create a new node using the value passed to the function&lt;/li&gt;
&lt;li&gt;If there are no nodes in the queue, set this node to be the first and last property of the queue&lt;/li&gt;
&lt;li&gt;Otherwise, set the next property on the current last to be that node, and then set the last property of the queue to be that node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pseudocode for &lt;code&gt;dequeue()&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there is no first property, just return null&lt;/li&gt;
&lt;li&gt;Store the first property in a variable&lt;/li&gt;
&lt;li&gt;See if the first is the same as the last (check if there is only 1 node). If so, set the first and last to be null&lt;/li&gt;
&lt;li&gt;If there is more than 1 node, set the first property to be the next property of first&lt;/li&gt;
&lt;li&gt;Decrement the size by 1&lt;/li&gt;
&lt;li&gt;Return the value of the node dequeued&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;A bit more hassle to implement than just using Array, but this will make the data structure more optimized. I definitely recommend you to go check out the &lt;a href="https://dev.to/chuckchoiboi/data-structure-series-linked-list-3eb2"&gt;Data Structure Series blog post&lt;/a&gt; I wrote on Linked List to learn about it if you need a refresher or have a problem comprehending the code above.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Big O &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Space complexity of this data structure is linear, as the size of the list increase, so does the space&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;strong&gt;Push/Pop&lt;/strong&gt; and &lt;strong&gt;Enqueue/Dequeue&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;O(1)&lt;/em&gt; Time Complexity&lt;/li&gt;
&lt;li&gt;If we were to utilize &lt;strong&gt;Linked List&lt;/strong&gt; over &lt;strong&gt;Array&lt;/strong&gt;, both &lt;strong&gt;Push/Pop&lt;/strong&gt; and &lt;strong&gt;Enqueue/Dequeue's&lt;/strong&gt; time complexity can be optimized to &lt;em&gt;O(1)&lt;/em&gt;. Furthermore, &lt;strong&gt;Linked List is not the only optimized way to implement Stacks and Queues&lt;/strong&gt;, for example, you can create those classes using an object as its storage. &lt;a href="https://youtu.be/1AJ4ldcH2t4?t=236"&gt;Here's&lt;/a&gt; video on this implementation if you are interested, but as you can see, there are many ways to create Stack/Queue.&lt;/li&gt;
&lt;/ul&gt;


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




&lt;h2&gt;
  
  
  5. Helpful Resources &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/"&gt;&lt;strong&gt;Online Course&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(Udemy Course)&lt;/strong&gt;&lt;br&gt;
Check out this Udemy course named JavaScript Algorithms and Data Structures Masterclass! It is created by &lt;a href="https://www.youtube.com/c/ColtSteeleCode"&gt;Colt Steele&lt;/a&gt;, and I referenced his code for the data structure implementation part of this blog post. Personally, I didn't know where to start with algorithms and data structures especially coming from a non-tech background. This course is very well structured for beginners to build a foundation on these topics.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://visualgo.net/en"&gt;&lt;strong&gt;Visual Animation&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(VisuAlgo)&lt;/strong&gt;&lt;br&gt;
Data structures can be difficult to comprehend for some people just by looking at the code/text. The instructor in the course above uses a website named VisuAlgo that has visual representation of algorithms and data structures through animation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.interviewcake.com/data-structures-reference"&gt;&lt;strong&gt;Data Structure Cheat Sheet&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(Interview Cake)&lt;/strong&gt;&lt;br&gt;
Also, here's a really well-summarized cheat sheet/visualizations on data structures.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>stack</category>
      <category>queue</category>
    </item>
    <item>
      <title>Data Structure Series: Linked List</title>
      <dc:creator>Chuck Choi</dc:creator>
      <pubDate>Mon, 09 Aug 2021 16:15:48 +0000</pubDate>
      <link>https://forem.com/chuckchoiboi/data-structure-series-linked-list-3eb2</link>
      <guid>https://forem.com/chuckchoiboi/data-structure-series-linked-list-3eb2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xpt6in1hml7m89611hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xpt6in1hml7m89611hj.png" alt="data-structure-series-intro"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We use forks to eat pasta, spoons to eat soup, and chopsticks to eat dumplings. Each silverwares have its advantages/disadvantages, hence working better than the other for the food that it interacts well with. &lt;strong&gt;Just like that, different data structures are better suited and perform better than the others based on the situations/use cases.&lt;/strong&gt; They each have their pros and cons. Understanding these pros and cons can help you be a better programmer, as it will allow you to choose an appropriate data structure(s) based on the circumstances/goals you have, and it helps to drastically improve the performance of the algorithm being applied. Feel free to leave a comment if you have any questions!&lt;/p&gt;




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

&lt;p&gt;&lt;strong&gt;1. What is Linked List?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Implementation in JavaScript&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Helper Methods&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Big O&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;5. Helpful Resources&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  1. What is Linked List? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A Linked List is a collection of data in a sequence, with each of the data referencing its next node (or previous node if it is a Doubly Linked List) from its &lt;em&gt;'head'&lt;/em&gt; to the &lt;em&gt;'tail'&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0n3m9367xhmbbntpaaxs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0n3m9367xhmbbntpaaxs.png" alt="linked-list1"&gt;&lt;/a&gt;&lt;br&gt;
A &lt;strong&gt;Linked List&lt;/strong&gt; is a type of data that is represented in a sequential collection. Each piece of data in that collection is called the &lt;em&gt;node&lt;/em&gt;, which references its adjacent node in the sequence. The first node of a linked list is called the &lt;strong&gt;'head'&lt;/strong&gt;, and the last node is called the &lt;strong&gt;'tail'&lt;/strong&gt;. There are two types of linked lists: &lt;strong&gt;Singly Linked List&lt;/strong&gt; and &lt;strong&gt;Doubly Linked List&lt;/strong&gt;. As the names suggest, Singly Linked Lists’ nodes are linked in only single direction, so each nodes references its next node. On the other hand, Doubly Linked Lists’ nodes reference both its previous and the next node. &lt;strong&gt;In summary, a Linked List is a collection of data in a sequence, with each of the data referencing its next node (or previous node if it is a Doubly Linked List) from its &lt;em&gt;'head'&lt;/em&gt; to the &lt;em&gt;'tail'&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It sounds a bit similar to a built-in data structure &lt;em&gt;Array&lt;/em&gt;, doesn't it? The difference is that &lt;strong&gt;Arrays store each data in a consecutive manner in the memory&lt;/strong&gt; meaning that the elements are stored next to each other. And each elements is indexed based on the position, and each element is directly accessible using those indices. Meanwhile, &lt;em&gt;Linked Lists&lt;/em&gt; store each data anywhere in the memory, but the nodes &lt;strong&gt;reference&lt;/strong&gt; their next and previous node. So in order to access a specific node in a Linked List, you need to traverse the list sequentially from its head or tail to the other end until you get to the node you are looking for.&lt;/p&gt;

&lt;p&gt;Because of these differences, there are things that linked lists can do better than arrays, and vice versa: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  Arrays can search faster
&lt;/h3&gt;

&lt;p&gt;As we discussed, &lt;strong&gt;Arrays&lt;/strong&gt; support random access, so we can access any elements in the (n)th index very quickly while &lt;strong&gt;Linked Lists&lt;/strong&gt; support sequential access, so we have to start from the head or tail to the (n)th node or value of the node we are looking for, thus taking longer time to search an element. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  Linked Lists can insert/delete faster
&lt;/h3&gt;

&lt;p&gt;In order to insert or delete an element in the beginning or middle of an &lt;strong&gt;Array&lt;/strong&gt;, you have to shift all of the elements on the right since its &lt;em&gt;consecutive&lt;/em&gt; index positions will change. So inserting and deleting an element in an array can be costly unless you are inserting or removing the last element of the array (since there's no elements after the last element). With &lt;strong&gt;Linked Lists&lt;/strong&gt;, inserting/deleting the first and the last element takes constant time since we just have to update the head/tail. Inserting/deleting an element in the middle can take linear time as well though, since you'd have to find the position to insert/delete by traversing the list one element at a time. However, there's no need to update all the elements that come afterwards, you just have to rearrange its adjacent nodes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  2. Implementation in JavaScript &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Singly Linked List&lt;/strong&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;// each node references its NEXT node&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&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;value&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;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SinglyLinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&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;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&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;0&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;SLL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SinglyLinkedList&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;firstNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;secondNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;thirdNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;46&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// set the first new node as the SLL's head&lt;/span&gt;
&lt;span class="nx"&gt;SLL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;SLL&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="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// second as its next&lt;/span&gt;
&lt;span class="nx"&gt;firstNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;secondNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;SLL&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="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// the third as the second's next &lt;/span&gt;
&lt;span class="c1"&gt;// while also setting it as a tail since it's the last one.&lt;/span&gt;
&lt;span class="nx"&gt;secondNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;SLL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thirdNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;SLL&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="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// This SLL will look something like this:&lt;/span&gt;
&lt;span class="c1"&gt;// (16) =&amp;gt; (2) =&amp;gt; (46)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Doubly Linked List&lt;/strong&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;// each node references both its NEXT and PREVIOUS node&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&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;value&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;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DoublyLinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;constructor&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;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&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;0&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;DLL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DoublyLinkedList&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;firstNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;361&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;secondnode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&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;thirdNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// set the first new node as the DLL's head&lt;/span&gt;
&lt;span class="nx"&gt;DLL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;DLL&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="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// second as its next, and head as its prev&lt;/span&gt;
&lt;span class="nx"&gt;firstNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;secondNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;secondNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;DLL&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="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// the third as the second's next &lt;/span&gt;
&lt;span class="c1"&gt;// while also setting it as a tail since it's the last one.&lt;/span&gt;
&lt;span class="nx"&gt;secondNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DLL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thirdNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;thirdNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;secondNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;DLL&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="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// This SLL will look something like this:&lt;/span&gt;
&lt;span class="c1"&gt;// (361) &amp;lt;=&amp;gt; (99) &amp;lt;=&amp;gt; (4)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will set up a &lt;code&gt;Node&lt;/code&gt; class which accepts a value and set it to its value, with its next property (and prev if Doubly Linked List) initialized to null. Linked List class will be a sequential collection of these nodes, which will have its head and tail. We will want to keep track of the list's length, and increment/decrement it every time a new node is added or removed. Since &lt;strong&gt;Singly Linked Lists's&lt;/strong&gt; nodes only reference the &lt;code&gt;next&lt;/code&gt; node and &lt;strong&gt;Doubly Linked Lists'&lt;/strong&gt; nodes reference both their &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;previous&lt;/code&gt; nodes, Singly Linked Lists are simpler but less powerful than Doubly Linked Lists. &lt;/p&gt;

&lt;p&gt;If you were to implement a helper method to pop the last element of the list, it's easier to do that with &lt;strong&gt;Doubly Linked Lists&lt;/strong&gt; as you simply have to remove the tail of the list, and set the new tail to be the previous node of the tail being removed. On the other hand, we can access the tail of the list, but will have to traverse the entire list and remember the previous node until you hit the tail so you can remove the tail and set the remembered previous node to be the new tail.&lt;/p&gt;

&lt;p&gt;The main drawback of using &lt;strong&gt;Doubly Linked List&lt;/strong&gt; vs &lt;strong&gt;Singly Linked List&lt;/strong&gt; is that &lt;strong&gt;Doubly Linked List&lt;/strong&gt; takes up more space than the &lt;strong&gt;Singly Linked List&lt;/strong&gt; since you have to set each nodes' next and previous node. But in return, it opens up more doors to make your data and its algorithms efficient. With that being said, here are couple helper methods to utilize &lt;strong&gt;Linked Lists&lt;/strong&gt; better. However, we will only focus on &lt;strong&gt;Doubly Linked Lists&lt;/strong&gt; for this blog post.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Helper Methods (Doubly Linked List only) &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;push()&lt;/strong&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;// accepts a value as an argument&lt;/span&gt;
&lt;span class="c1"&gt;// appends a new node with the value passed at the end of the list&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;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&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;head&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;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&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;tail&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;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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;strong&gt;Pseudo code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new node with the value passed to the function&lt;/li&gt;
&lt;li&gt;If the head property is &lt;code&gt;null&lt;/code&gt;, set the &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt; to be the newly created node&lt;/li&gt;
&lt;li&gt;If the head is not &lt;code&gt;null&lt;/code&gt;, set the next property on the &lt;code&gt;tail&lt;/code&gt; to be that node&lt;/li&gt;
&lt;li&gt;Set the &lt;code&gt;prev&lt;/code&gt; property on the newly created node to be the &lt;code&gt;tail&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set the &lt;code&gt;tail&lt;/code&gt; to be the newly created node&lt;/li&gt;
&lt;li&gt;Increment the &lt;code&gt;length&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return the Linked List&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;pop()&lt;/strong&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;// removes the last node (tail) of the list&lt;/span&gt;
&lt;span class="nf"&gt;pop&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="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;head&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;undefined&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;removedNode&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;tail&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="k"&gt;this&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="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;head&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;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removedNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&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;tail&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;removedNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="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;length&lt;/span&gt;&lt;span class="o"&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;removedNode&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;strong&gt;Pseudo code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there is no &lt;code&gt;head&lt;/code&gt;, return &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Store the current &lt;code&gt;tail&lt;/code&gt; in a variable to return later&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;length&lt;/code&gt; is 1, set the &lt;code&gt;head&lt;/code&gt; or &lt;code&gt;tail&lt;/code&gt; to be &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Update the &lt;code&gt;tail&lt;/code&gt; to be the previous Node&lt;/li&gt;
&lt;li&gt;Set the new &lt;code&gt;tail&lt;/code&gt;'s &lt;code&gt;next&lt;/code&gt; to &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Decrement the &lt;code&gt;length&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return the node removed&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;unshift()&lt;/strong&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;// accepts a value as an argument&lt;/span&gt;
&lt;span class="c1"&gt;// prepends a new node with the value passed at the beginning of the list&lt;/span&gt;
&lt;span class="nf"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Node&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="k"&gt;if&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;length&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="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;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;tail&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;head&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&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;head&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;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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;strong&gt;Pseudo code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new node with the &lt;code&gt;value&lt;/code&gt; passed to the function&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;length&lt;/code&gt; is 0, set the &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt; to be the new node&lt;/li&gt;
&lt;li&gt;Otherwise

&lt;ul&gt;
&lt;li&gt;Set the &lt;code&gt;prev&lt;/code&gt; property on the &lt;code&gt;head&lt;/code&gt; to be the new node&lt;/li&gt;
&lt;li&gt;Set the &lt;code&gt;next&lt;/code&gt; property on the new node to be the &lt;code&gt;head&lt;/code&gt; property&lt;/li&gt;
&lt;li&gt;Update the &lt;code&gt;head&lt;/code&gt; to be the new node&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Increment the &lt;code&gt;length&lt;/code&gt;
&lt;/li&gt;

&lt;li&gt;Return the Linked List&lt;/li&gt;

&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;shift()&lt;/strong&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;// removes the first node (head) of the list&lt;/span&gt;
&lt;span class="nf"&gt;shift&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="k"&gt;this&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;0&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;undefined&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;oldHead&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;head&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="k"&gt;this&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="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;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldHead&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&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;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;oldHead&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="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;length&lt;/span&gt;&lt;span class="o"&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;oldHead&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;strong&gt;Pseudo code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;length&lt;/code&gt; is 0, return &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Store the current &lt;code&gt;head&lt;/code&gt; property in a variable&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;length&lt;/code&gt; is one, set the &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt; to be &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Update the &lt;code&gt;head&lt;/code&gt; to be the &lt;code&gt;next&lt;/code&gt; of the old &lt;code&gt;head&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set the &lt;code&gt;head&lt;/code&gt;'s &lt;code&gt;prev&lt;/code&gt; property to &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set the old &lt;code&gt;head&lt;/code&gt;'s &lt;code&gt;next&lt;/code&gt; to &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Decrement the &lt;code&gt;length&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return old &lt;code&gt;head&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;get()&lt;/strong&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;// accepts an index as an argument&lt;/span&gt;
&lt;span class="c1"&gt;// returns the node at the index passed&lt;/span&gt;
&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idx&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;idx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;&amp;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;length&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="kd"&gt;let&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;current&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;idx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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;length&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="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;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;current&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;head&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;count&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;
            &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;count&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;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;count&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;tail&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;count&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt;
            &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;current&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;strong&gt;Pseudo code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the index is less than 0 or greater or equal to the &lt;code&gt;length&lt;/code&gt;, return &lt;code&gt;null&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the index is less than or equal to half the length of the list

&lt;ul&gt;
&lt;li&gt;Loop through the list starting from the &lt;code&gt;head&lt;/code&gt; and loop towards the middle&lt;/li&gt;
&lt;li&gt;Return the node once it is found&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If the index is greater than half the length of the list

&lt;ul&gt;
&lt;li&gt;Loop through the list starting from the &lt;code&gt;tail&lt;/code&gt; and loop towards the middle&lt;/li&gt;
&lt;li&gt;Return the node once it is found&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;set()&lt;/strong&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;// accepts an index and value as arguments&lt;/span&gt;
&lt;span class="c1"&gt;// finds the node at the index, and updates the node's value to the value passed&lt;/span&gt;
&lt;span class="c1"&gt;// returns false if the node is not found, true if the value is updated&lt;/span&gt;
&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foundNode&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="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;idx&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;foundNode&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;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;foundNode&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;value&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;true&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;strong&gt;Pseudo code:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a variable which is the result of the &lt;code&gt;get&lt;/code&gt; method at the index passed to the function&lt;/li&gt;
&lt;li&gt;If the &lt;code&gt;get&lt;/code&gt; method does not return a valid node, return &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set the &lt;code&gt;value&lt;/code&gt; of the node found from &lt;code&gt;get&lt;/code&gt; method to the &lt;code&gt;value&lt;/code&gt; passed to the function&lt;/li&gt;
&lt;li&gt;return &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Big O &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38wer67h7y7yvvf0b92d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38wer67h7y7yvvf0b92d.png" alt="linked-list-big-o"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;O(n)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Space complexity of this data structure is linear, as the size of the list increase, so does the space&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Push/Pop&lt;/strong&gt; and &lt;strong&gt;Shift/Unshift&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;O(1)&lt;/em&gt; Time Complexity&lt;/li&gt;
&lt;li&gt;It will take constant time to add/remove the node at the head and tail of a Linked List, since we just have to add a new node to the either end, and update the newly added node as its head/tail, or its previous/next element as head or tail if the node is being removed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Get/Set&lt;/strong&gt; and &lt;strong&gt;Insert/Delete&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;O(n)&lt;/em&gt; Time Complexity&lt;/li&gt;
&lt;li&gt;In order for us to find an element in a Linked List, we will need to traverse the list to find the index or value of the index. Due to this nature of the Linked List, modifying the node in the middle of the list will take linear time (the time complexity changes based on the list size). Although Insert/Delete methods are not listed in the helper method above, you get the idea that we will have to traverse the list to find an index of the list to insert/delete the element.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Helpful Resources &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/" rel="noopener noreferrer"&gt;&lt;strong&gt;Online Course&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(Udemy Course)&lt;/strong&gt;&lt;br&gt;
Check out this Udemy course named JavaScript Algorithms and Data Structures Masterclass! It is created by &lt;a href="https://www.youtube.com/c/ColtSteeleCode" rel="noopener noreferrer"&gt;Colt Steele&lt;/a&gt;, and I referenced his code for the data structure implementation part of this blog post. Personally, I didn't know where to start with algorithms and data structures especially coming from a non-tech background. This course is very well structured for beginners to build a foundation on these topics.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://visualgo.net/en" rel="noopener noreferrer"&gt;&lt;strong&gt;Visual Animation&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(VisuAlgo)&lt;/strong&gt;&lt;br&gt;
Data structures can be difficult to comprehend for some people just by looking at the code/text. The instructor in the course above uses a website named VisuAlgo that has visual representation of algorithms and data structures through animation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.interviewcake.com/data-structures-reference" rel="noopener noreferrer"&gt;&lt;strong&gt;Data Structure Cheat Sheet&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;(Interview Cake)&lt;/strong&gt;&lt;br&gt;
Also, here's a really well-summarized cheat sheet/visualizations on data structures.&lt;/p&gt;

</description>
      <category>datastructure</category>
      <category>linkedlist</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Let’s Talk About Cross-Origin Resource Sharing (CORS)</title>
      <dc:creator>Chuck Choi</dc:creator>
      <pubDate>Sat, 22 May 2021 13:07:50 +0000</pubDate>
      <link>https://forem.com/chuckchoiboi/demystifying-cross-origin-resource-sharing-cors-4lpj</link>
      <guid>https://forem.com/chuckchoiboi/demystifying-cross-origin-resource-sharing-cors-4lpj</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Access to fetch at ‘&lt;a href="https://coolserver.com%E2%80%99" rel="noopener noreferrer"&gt;https://coolserver.com’&lt;/a&gt; from origin ‘&lt;a href="http://localhost:3000%E2%80%99" rel="noopener noreferrer"&gt;http://localhost:3000’&lt;/a&gt; has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every web developer may have come across this &lt;strong&gt;CORS policy violation&lt;/strong&gt; (Cross-Origin Resource Sharing) error message at least once in their career. I faced this issue for the first time when I was developing a full stack application for a group project at a coding bootcamp. We were simply building a client application that was fetching data from the server we developed, and we panicked as this error popped up.&lt;/p&gt;

&lt;p&gt;The error itself is actually pretty informative. It basically tells you that the client-side is not one of the "whitelisted" origins to access the data being fetched. In this blog post, lets learn the basics of Cross-Origin Resource Sharing, three scenarios and the common errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Cross-Origin Resource Sharing?
&lt;/h2&gt;

&lt;p&gt;Let's first go over what &lt;strong&gt;CORS&lt;/strong&gt; is and why it is important. &lt;strong&gt;CORS&lt;/strong&gt; is an acronym for &lt;strong&gt;Cross-Origin Resource Sharing&lt;/strong&gt;, which is a cyber security mechanism that allows/prevents one origin to access a resource from a different origin. This is something that the server has control over to restrict who has access to the resource, how they can access the data (which HTTP methods are allowed), if cookie information should be included or not, and etc.&lt;/p&gt;

&lt;p&gt;Client-side applications are generally very vulnerable to cyber attacks from the malicious users. If you think about it, users can easily open the browser dev tool to check how the DOM is structured, which server it is communicating with, and where the resource is coming from without much restrictions. &lt;strong&gt;CORS is not the perfect security measure, but it provides minimum guarantee that the resource we fetch from the other origin is safe.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Same-Origin Policy vs Cross-Origin Resource Sharing
&lt;/h2&gt;

&lt;p&gt;There are two policies that helps the browsers to protect the users from potential cyber attacks via dynamically loaded code. These are Same-Origin Policy (SOP) and Cross-Origin Resource Sharing. Generally, it is forbidden to read data from another origin. SOP allows browsers to only request resources from the same origin. You'd be violating SOP if you request a resource from a different origin. For example, requesting data from &lt;code&gt;https://chuckchoi.me&lt;/code&gt; to &lt;code&gt;https://dev.to&lt;/code&gt; would be violating SOP normally since these are not the same origin.&lt;/p&gt;

&lt;p&gt;This would defeat the purpose and the power of the web if you are not able to fetch data from another origin. Thankfully, &lt;strong&gt;Cross-Origin Resource Sharing (CORS)&lt;/strong&gt; allows exceptions to SOP and permits cross-origin requests to be made. There are three main requests used in cross-origin requests, and let's dive into the common errors you would see for each of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before We Begin...
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ii881xa6qoq8uzfggz6.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ii881xa6qoq8uzfggz6.JPG" alt="cors-tutorial"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I built a simple client-side React app and an Express server to help us visualize what's going on. There's three different Cross-Origin requests you can test and see common errors you may face based on the server's setting. You can see each scenario's server and request structure, and click &lt;strong&gt;"Send Request"&lt;/strong&gt; button to see what response you would get. You can also open your browser console to check the network tab to see the network behavior. Feel free to use the app on the side to supplement the understanding and check out the repository if you'd like!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://chuckchoiboi.github.io/cors-tutorial/" rel="noopener noreferrer"&gt;CORS Tutorial App Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/chuckchoiboi/cors-tutorial" rel="noopener noreferrer"&gt;Git Repository&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Simple Request
&lt;/h2&gt;

&lt;p&gt;There isn't an official terminology for the request we are about to discuss, but MDN's CORS documentation call it Simple Request. Simple Request is a cross origin request that is simply sent without any preflight request (which we will go over next) directly to the server. Server would respond back with a response that contains Access-Control-Allow-Origin in the header which then the browser checks CORS policy violations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1lbkalno6veu2vgprm3t.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1lbkalno6veu2vgprm3t.JPG" alt="simple-request1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simple Requests are only allowed if certain conditions are met which is not the case for most of the modern web development. Here are the list of conditions found in MDN:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One of the allowed methods:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GET&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HEAD&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;POST&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Apart from the headers automatically set by the user agent (for example, Connection, User-Agent, or the other headers defined in the Fetch spec as a “forbidden header name”), the only headers which are allowed to be manually set are those which the Fetch spec defines as a “CORS-safelisted request-header”, which are:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Accept&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Accept-Language&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Content-Language&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Content-Type&lt;/code&gt; (but note the additional requirements below)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The only allowed values for the Content-Type header are:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;multipart/form-data&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/plain&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If the request is made using an &lt;code&gt;XMLHttpRequest&lt;/code&gt; object, no event listeners are registered on the object returned by the &lt;code&gt;XMLHttpRequest.upload&lt;/code&gt; property used in the request; that is, given an &lt;code&gt;XMLHttpRequest&lt;/code&gt; instance &lt;code&gt;xhr&lt;/code&gt;, no code has called &lt;code&gt;xhr.upload.addEventListener()&lt;/code&gt; to add an event listener to monitor the upload.&lt;/li&gt;

&lt;li&gt;No &lt;code&gt;ReadableStream&lt;/code&gt; object is used in the request.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Wow, that was a pretty long list of requirements. As we discussed, it is pretty rare to meet all the requirements above in modern web development, so you may be dealing with preflight or credentialed request most of the time. But for Simple Request to work without violating CORS error, the response's header needs to have &lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt; with the request's origin listed or use an asterisk (* sign) as a wildcard to allow all origins.&lt;/p&gt;

&lt;h3&gt;
  
  
  Simple Request Exercise -- &lt;a href="https://chuckchoiboi.github.io/cors-tutorial/" rel="noopener noreferrer"&gt;CORS Tutorial App&lt;/a&gt;
&lt;/h3&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Error #1: No Access-Control-Allow-Origin Header&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's go ahead and open up the &lt;a href="https://chuckchoiboi.github.io/cors-tutorial/" rel="noopener noreferrer"&gt;CORS-Tutorial App&lt;/a&gt;. Under the &lt;strong&gt;Simple Request&lt;/strong&gt; tab -&amp;gt; &lt;strong&gt;Error 1&lt;/strong&gt; tab, this is how the server is structured:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz637b16pi06ubxqo7wyp.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz637b16pi06ubxqo7wyp.JPG" alt="simple-request-error1-server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fetch method we are invoking is &lt;code&gt;fetch('https://cors-tutorial-server.herokuapp.com/api/simple/no-origin')&lt;/code&gt;. By default, &lt;code&gt;fetch()&lt;/code&gt; would make a &lt;code&gt;GET&lt;/code&gt; request to the URL passed as an argument if the method is not specified. And since the request is very basic, it is sending it as a simple request as it meets the simple request's requirements. Let's go ahead and click the button to see what would response we would get if we make a simple fetch request to that route:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfssesb01fqy5vqj3158.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfssesb01fqy5vqj3158.JPG" alt="simple-request-error1-response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Based on the error message above, the request we made from the app's origin &lt;code&gt;https://chuckchoiboi.github.io/cors-tutorial/&lt;/code&gt; has been blocked due to the violation of the CORS policy. It shows that &lt;strong&gt;"No 'Access-Control-Allow-Origin' header is present on the requested resource."&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Solution 1: Wildcard Origin&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the first steps to comply with the CORS policy is adding &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; to the response's header. You could either specify the origin, or use asterisk as a wildcard to allow all origins. From the server-side, you could add a wildcard origin like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8w1kz49gdp2gfhfr24bx.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8w1kz49gdp2gfhfr24bx.JPG" alt="simple-request-wildcard-server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go ahead and try sending the request. You would see the server responding with a valid response like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52tqevtt66ku7j9a85mo.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52tqevtt66ku7j9a85mo.JPG" alt="simple-request-wildcard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Error #2 - Unmatching Origin&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Allowing all origins is probably not the best practice and it would not be secure. It would be better if you "whitelist" the origins by specifying which ones you are expecting. Here's an example of a server with origin specified (&lt;strong&gt;Simple Request&lt;/strong&gt; tab -&amp;gt; &lt;strong&gt;Error 2&lt;/strong&gt; tab):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fst1zm62y4pbt8nqamyeq.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fst1zm62y4pbt8nqamyeq.JPG" alt="simple-request-error2-server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The origin that this route is expecting is &lt;code&gt;https://www.website.notcool&lt;/code&gt; though. Making a fetch request from &lt;code&gt;https://chuckchoiboi.github.io/cors-tutorial/&lt;/code&gt; show a bit different error message this time:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65i2yqf42kpkb94pfqrl.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65i2yqf42kpkb94pfqrl.JPG" alt="simple-request-error2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, the error shows that the origin the server is expecting for this route is &lt;code&gt;https://www.website.notcool&lt;/code&gt;. Let's say we are making a request from &lt;code&gt;www.website.notcool&lt;/code&gt;, but the protocol we are making the request from is &lt;code&gt;http://&lt;/code&gt; and not &lt;code&gt;https://&lt;/code&gt;. This will throw the same error since origin consists of protocol and host both. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Solution #2: Matching Origin&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that being said, the server's response header would need to have the origin that matches the request's origin. A valid simple request can be sent to a server with the origin specified like this (&lt;strong&gt;Simple Request&lt;/strong&gt; tab -&amp;gt; &lt;strong&gt;Valid Condition&lt;/strong&gt; tab):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbamzi35lhia2uofxjnkp.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbamzi35lhia2uofxjnkp.JPG" alt="simple-request-solution2"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Preflight Request
&lt;/h2&gt;

&lt;p&gt;You are going to come across &lt;strong&gt;preflight requests&lt;/strong&gt; more than simple requests in modern web applications. For this scenario, the browser makes a preflight request to ask for permissions before the actual request is made. If the browser approves the response from the server through the preflight request, then the actual request is made. If the preflight request is not approved, then the actual request is not made.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54f9p1ba3ta9vj25w9ad.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54f9p1ba3ta9vj25w9ad.JPG" alt="preflight-flowchart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;During this preflight process, the preflight request uses OPTIONS method. The preflight response needs to allow the origin of the request in the header, and the actual request's method needs to be allowed as well. Once these conditions are satisfied, that's when the actual request is made.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preflight Request Exercise -- &lt;a href="https://chuckchoiboi.github.io/cors-tutorial/" rel="noopener noreferrer"&gt;CORS Tutorial App&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Error #1: Preflight Response with Unmatching Origin&lt;/strong&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1cxjkb8y4cbicqtgf4w.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1cxjkb8y4cbicqtgf4w.JPG" alt="preflight-request-error1-server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a look at this example. The request is trying to make a &lt;code&gt;DELETE&lt;/code&gt; request to the server. Since the request is using &lt;code&gt;DELETE&lt;/code&gt; method, it will make this request a preflight request, thus the browser will first send a preflight request using &lt;code&gt;OPTIONS&lt;/code&gt; method to check its permission. However, since the origin of the request and the response's &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; value is not matching, this preflight request will fail and not even go to the actual request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3w7k8nh5degqlzwv9m47.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3w7k8nh5degqlzwv9m47.JPG" alt="preflight-request-error1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Error #2: Preflight Response with Method Unspecified&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's try again. Let's try sending a DELETE request this time to a route with preflight response that contains header with the request's origin allowed like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5von63f9k8f1jcgeam9j.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5von63f9k8f1jcgeam9j.JPG" alt="preflight-request-error2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Does it feel like we may be missing something? Here's a little spoiler. This one again will not even go to the actual request because the server's preflight response does not have DELETE method specified. Here's the error response you will get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp533tta8v5nezmzfd9q7.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp533tta8v5nezmzfd9q7.JPG" alt="preflight-request-error2-response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Error #3: Preflight Passes, Actual Request Fails&lt;/strong&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbwhw3efc71h6tccv9q8a.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbwhw3efc71h6tccv9q8a.JPG" alt="preflight-request-error3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that the preflight response has matching origin allowed, and &lt;code&gt;DELETE&lt;/code&gt; method allowed as well, this will send the actual &lt;code&gt;DELETE&lt;/code&gt; request. Did you notice anything wrong from the response header though?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fla3prirop4bxgmhuesdv.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fla3prirop4bxgmhuesdv.JPG" alt="preflight-request-error3-response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You got it right! As the error shows, the server is only allowing &lt;code&gt;https://www.website.notcool&lt;/code&gt; origin. Even if the preflight passes, if the actual request fails, you will still be violating CORS policy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rssrnz6cxc91jt97286.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rssrnz6cxc91jt97286.JPG" alt="preflight-request-solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to make a valid Preflight Request, the server needs to handle preflight request with valid origin, and valid method in the response header as we discussed. Once the preflight request passes, the actual request is sent. The actual request would need to have the request origin allowed to comply with the CORS policy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotpefcpg81i9fjzfyaw2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotpefcpg81i9fjzfyaw2.JPG" alt="preflight-request-solution-response"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Credentialed Request
&lt;/h2&gt;

&lt;p&gt;Last but not least, there's a 3rd scenario to cross-origin request that strengthens the security. When sending &lt;code&gt;XMLHttpRequest&lt;/code&gt; or &lt;code&gt;fetch&lt;/code&gt;, you should not include the browser cookie or authentication-related headers without any options. Sending a request with credentials option would allow us to send sensitive information like cookies in cross-origin requests.&lt;/p&gt;

&lt;p&gt;You can send a credentialed request by adding &lt;code&gt;{"credentials": "include"}&lt;/code&gt; option to the request in JavaScript. This will add some strict rules to CORS policy conditions. When the browser is sending a credentialed request, the response's Access-Control-Allow-Origin should not be using the wildcard "*". It needs to specify the request's origin, and also the server needs to have additional header &lt;code&gt;Access-Control-Allow-Credentials&lt;/code&gt; set to &lt;code&gt;true&lt;/code&gt; to allow valid credentialed request to be made.&lt;/p&gt;

&lt;h3&gt;
  
  
  Credentialed Request Exercise -- &lt;a href="https://chuckchoiboi.github.io/cors-tutorial/" rel="noopener noreferrer"&gt;CORS Tutorial App&lt;/a&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Error 1: Wildcard Origin&lt;/strong&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tiybf49h90bk707r2lo.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tiybf49h90bk707r2lo.JPG" alt="credentialed-request-server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time, we are sending a GET request using fetch() method that includes &lt;code&gt;{"credentials":"include"}&lt;/code&gt; as an option. The server's response header is using a wildcard for Access-Control-Allow-Origin. Let's go ahead and send the request by clicking the button in the app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnq6r295spmy5cicfidgm.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnq6r295spmy5cicfidgm.JPG" alt="credentialed-request-error1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the error message, credentialed request does not allow &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; to be the wildcard. In order for us to be able to make a credentialed request to the server, we will need the server's route to allow &lt;code&gt;https://chuckchoiboi.github.io&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Error 2: Access-Control-Allow-Credentialed&lt;/strong&gt;&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxo8du6jy1cvpnpysxuad.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxo8du6jy1cvpnpysxuad.JPG" alt="credentialed-request-error2-server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay we have the request origin &lt;code&gt;https://chuckchoiboi.github.io&lt;/code&gt; specified in the server this time. Without further ado, let's go ahead and click the "Send Request" button!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenykpsfug9jp8fomwa6v.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenykpsfug9jp8fomwa6v.JPG" alt="credentialed-request-error2-response"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Trust me, this is the last error you are going to see today. As we discussed earlier, credentialed request adds more strict conditions to CORS policy rules. What the error message is suggesting is that the response header needs to include an additional header &lt;code&gt;Access-Control-Allow-Credentials&lt;/code&gt; with its value being set to &lt;code&gt;true&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To summarize, credentialed request can be made by adding &lt;code&gt;{"credentials":"include"}&lt;/code&gt; in the request, response header specifying the request origin (wildcard not allowed), and &lt;code&gt;Access-Control-Allow-Credentials&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt; in the response header as well. A successful credentialed request should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9nvthg9llaueeyrpw5g.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9nvthg9llaueeyrpw5g.JPG" alt="credentialed-request-solution"&gt;&lt;/a&gt;&lt;/p&gt;




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

&lt;p&gt;When I think of Cross-Origin Resource Sharing, it reminds me of guest list/access to a gated community. I've been to couple of my friends' gated community houses, where the home owners have to give names to the security guard at the gate to let them know who is invited to enter the gate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiek0hx2x0pa5go5wi27.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiek0hx2x0pa5go5wi27.JPG" alt="gate-security"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What's interesting about Cross-Origin Resource Sharing is that the front-end developers are the ones who actually have problems with CORS policy violations, while the backend developers have the control to resolve these issues in the response header. Resolving CORS errors is not too difficult to handle, you just need to communicate with the backend developer to make sure all the CORS policy conditions are met to fetch the resource.&lt;/p&gt;

&lt;p&gt;Thank you so much for taking time to read this blog post! If you want to learn about the React app or the Express server I built, or give me feedback on the app/blog post feel free to message me on &lt;a href="https://www.linkedin.com/in/chuckchoi/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A Reflection of a Coding Bootcamp Grad</title>
      <dc:creator>Chuck Choi</dc:creator>
      <pubDate>Mon, 17 May 2021 17:04:27 +0000</pubDate>
      <link>https://forem.com/chuckchoiboi/reflection-of-a-coding-bootcamp-grad-4ibe</link>
      <guid>https://forem.com/chuckchoiboi/reflection-of-a-coding-bootcamp-grad-4ibe</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrdlul0h98a0ij3npo4u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrdlul0h98a0ij3npo4u.png" alt="grad-picture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After 6 months of the coding bootcamp journey, a cohort of 18 brilliant programmers has graduated on &lt;strong&gt;May 15th, 2021&lt;/strong&gt;, and we all got one step closer to our goals. It was definitely not an easy road especially with the COVID-19 Pandemic forcing us to be in a virtual setting, but we all powered through! I wanted to take a moment to give a shoutout to my fellow cohort members and share what it was like to be a part of it for those who are debating whether to commit to a coding bootcamp or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Personal Background
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwur6z8him8vuwf260ifk.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwur6z8him8vuwf260ifk.JPG" alt="sommelier-exam"&gt;&lt;/a&gt;&lt;br&gt;
I came from a restaurant management background and switched to the tech industry in 2018. For the past 3 years, I've been working at a Boston-based tech company named &lt;strong&gt;HubSpot&lt;/strong&gt;. The company has been very supportive of my interest in front-end development, so I had the pleasure to take various front-end courses and training under their financial support. I really enjoyed front-end development and wanted to become a software engineer eventually. Coming from a non-tech background, I wasn't sure where to start though.&lt;/p&gt;

&lt;p&gt;I heard about &lt;strong&gt;General Assembly's&lt;/strong&gt; Software Engineering Immersive course from a colleague but wasn't quite sure if I wanted to commit to such a course that is time-consuming and expensive yet. I broke down my income to see if I could cover the cost but I barely could. I knew my wallet would be very slim for a while, So I did research on &lt;strong&gt;Lambda School&lt;/strong&gt;, &lt;strong&gt;Hack Reactor&lt;/strong&gt;, &lt;strong&gt;Flatiron School&lt;/strong&gt;, and a local university's Bootcamp program to make a careful decision. I checked out each of their cost/financial options they had, and how the class structure looked like. After going through months of emails, calls, and Google searches, I decided to go with &lt;strong&gt;General Assembly.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Course Breakdown
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Institution

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;General Assembly&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Course Name

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Software Engineering Immersive&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Dates

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;November 17th, 2021 - May 15th, 2021&lt;/strong&gt; (6 months)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Setting

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Part-time, Virtual&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Tuition

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;$14,950 USD&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Schedule&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tuesday&lt;/th&gt;
&lt;th&gt;Wednesday&lt;/th&gt;
&lt;th&gt;(Every other) &lt;br&gt;&lt;em&gt;Thursday&lt;/em&gt;
&lt;/th&gt;
&lt;th&gt;Saturday&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;5:30 PM - 9:45 PM&lt;/td&gt;
&lt;td&gt;5:30 PM - 9:45 PM&lt;/td&gt;
&lt;td&gt;&lt;em&gt;5:30 PM - 7:30 PM&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;9:00 AM - 5:00 PM&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;General Assembly had a fairly positive reputation and was the best for me because I have taken a couple of courses at that institution, so I was already satisfied with them. Most importantly, they offered a part-time class that fit my schedule and an alumni discount...!&lt;/p&gt;

&lt;h2&gt;
  
  
  Financial Detail
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgr9u5qhuel5joeobmnhn.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgr9u5qhuel5joeobmnhn.JPG" alt="tuition"&gt;&lt;/a&gt;&lt;br&gt;
Tuition was one of the most important factors for me when deciding whether to enroll in the program or not, and I bet it was for many of the Bootcamp grads/prospects. General Assembly's tuition was &lt;strong&gt;$14,950&lt;/strong&gt; which was kind of in the middle of the other bootcamp's price range. GA had a couple of financing options like loans and income share agreement to make it workout for the students. They offered a 15% alumni discount for me which brought down the cost by a significant amount (went from &lt;strong&gt;$14,950&lt;/strong&gt; to &lt;strong&gt;$12,707.50&lt;/strong&gt;). And thankfully, my employer provided $5,000 of financial support to take this course, so I ultimately had to pay &lt;strong&gt;$7,707.50 out of my pocket.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Typical Day to Day
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2756qpzmqnpjxexhm8c7.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2756qpzmqnpjxexhm8c7.JPG" alt="day-to-day"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I took this course while working full-time &lt;strong&gt;7:00 AM to 4:00 PM&lt;/strong&gt;. On the days I had the class, I had to work out, cook, and eat from &lt;strong&gt;4:00 PM - 5:30 PM&lt;/strong&gt;, then I was off to class for about &lt;strong&gt;4 hours&lt;/strong&gt;. Thanks to the course being virtual and not in-person, it saved me tons of time. I initially thought about taking the in-person course at GA but COVID-19 forced everything to be virtual. I do not know how I would've done it if it was in-person, driving through the LA traffic every class.&lt;/p&gt;

&lt;p&gt;I sacrificed my free time so I wouldn't lose much sleep. I missed out on Friendsgiving dinner this year which I was sad about. There were less golf time, video games, and Netflix. But it felt really good to be learning with organized time management and seeing my progress. &lt;/p&gt;

&lt;p&gt;This is how my typical day-to-day looked like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8a4vy86a6aurp59hg97.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8a4vy86a6aurp59hg97.JPG" alt="schedule"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Course Experience
&lt;/h2&gt;

&lt;p&gt;First of all, my cohort was pretty awesome. There were total 18 students who came from various backgrounds like &lt;strong&gt;music&lt;/strong&gt;, &lt;strong&gt;media&lt;/strong&gt;, &lt;strong&gt;aerospace&lt;/strong&gt;, &lt;strong&gt;technical support&lt;/strong&gt;, &lt;strong&gt;customer success&lt;/strong&gt;, &lt;strong&gt;consulting&lt;/strong&gt;, &lt;strong&gt;construction&lt;/strong&gt;, &lt;strong&gt;fresh out of high school&lt;/strong&gt; and etc. Some of the students had no prior coding experience, and some students had prior knowledge/experience that supplemented the course material. But we all had moments we struggled and triumphed. But we all powered through, it was fascinating to see everyone's growth through the journey.&lt;/p&gt;

&lt;p&gt;The course covered &lt;strong&gt;Front-End&lt;/strong&gt;, &lt;strong&gt;Full-Stack&lt;/strong&gt;, and &lt;strong&gt;client/server-side frameworks&lt;/strong&gt;. The course was taught via &lt;em&gt;Zoom&lt;/em&gt; and &lt;em&gt;GitHub&lt;/em&gt;, so we got very familiar with GitHub thanks to that. The course was structured like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lecture&lt;/li&gt;
&lt;li&gt;In-class exercise&lt;/li&gt;
&lt;li&gt;Lab work&lt;/li&gt;
&lt;li&gt;Homework&lt;/li&gt;
&lt;li&gt;Projects&lt;/li&gt;
&lt;li&gt;Career support/consulting (Every other Thursday)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The course work covered enough for you to learn the basics of each topic, but you really needed to put time outside of the class to get the best out of the class. I spent a lot of time after class to review, write codes from scratch to practice what I learned, and some of us even had peer-review sessions to help each other learn. Although the class was virtual, we had a lot of chances to get close to each other. There were two solo projects and two group projects, and the group projects really gave us chance to see each others' work styles and get close.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fea6420xoc5yra93k7a10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fea6420xoc5yra93k7a10.png" alt="cohort"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, the instructors were the best part of the course! There were two instructors and an assistant instructor teaching the course, and all of them were very patient. If there were students who were behind, they took time to set up breakout rooms and office hours to make sure that no one was left behind. I really appreciated their dedication and effort.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There was room for growth for this program though.&lt;/strong&gt; The course was led using the GitHub repository that I believe the General Assembly HQ put together to align it with the industry standards. &lt;strong&gt;There were many moments that some of us felt that the repository was pretty outdated with documentation on the tech stacks we were learning.&lt;/strong&gt; That definitely caused so much confusion figuring it out and debugging the version issues. Although it is understandable that it can't be perfect due to the tech stacks changing so much during a short period of time, we thought it would help the students focus more on comprehending the fundamentals of the material. I have sent this feedback to GA team, so hopefully this will be reflected in the future by making sure that the instructions are up to date more frequently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Personal Advice
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4zhovcdicijoxmzzba2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4zhovcdicijoxmzzba2.jpg" alt="journey"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I got lucky to get discounts and financial supports, but when I say this is a big financial and time commitment, I mean it. Some people would have to quit their job to be able to take this course and not having an income for 3 months/6 months would give you a lot of stress. So if you are considering enrolling in a coding bootcamp, really do some research on if you are able to financially support yourself, if you have time for it, and if this course would really be beneficial for you. There are many other ways to gain programming knowledge -- online classes and documentations to teach yourself how to code! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coding bootcamp is not a golden ticket to a software engineering career.&lt;/strong&gt; You really need to invest a lot of time and dedication to learn, never stop learning, work on projects, and prep for interviews/coding challenges to land a job. It all depends on the individual's dedication and capacity.&lt;/p&gt;

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

&lt;p&gt;If you do not think you have what it takes to be a programmer, I want to tell you that many of us felt that way too. I had moments I doubted myself and wanted to cry. It was manageable but not easy working full-time and being enrolled in a coding bootcamp part-time for 6 months. I had many nights I was digging deeper into the course material, homework, projects, and extra stuff I was curious about which made me messed up my sleeping schedule pretty often. I actually lost a lot of hair, but no big deal -- I have &lt;em&gt;Kirkland's Minoxidil&lt;/em&gt; by my side (hair growth treatment). &lt;strong&gt;My hair may or may not grow but one thing I was sure of: my technical knowledge will.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3be6jvhnha5nizqcu4ia.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3be6jvhnha5nizqcu4ia.jpg" alt="minoxidil"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for taking time to read this article.&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/chuckchoi/" rel="noopener noreferrer"&gt;Connect with me on LinkedIn&lt;/a&gt; and let me know if you have any questions!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>career</category>
    </item>
    <item>
      <title>How Does The Web Work?</title>
      <dc:creator>Chuck Choi</dc:creator>
      <pubDate>Mon, 26 Apr 2021 13:38:41 +0000</pubDate>
      <link>https://forem.com/chuckchoiboi/how-does-the-web-work-543</link>
      <guid>https://forem.com/chuckchoiboi/how-does-the-web-work-543</guid>
      <description>&lt;p&gt;Car, television, stove, and refrigerator are the machines we use daily that are fairly simple to use. These are essential tools in our lives yet most of us do not fully comprehend how they work but still, it is not the end of the world. We don't have to understand their mechanisms to be able to use them. Computers and mobile devices are also other machines that most people know how to use, but they are pretty complicated machines that are capable of doing many things. &lt;/p&gt;

&lt;p&gt;Using these devices, we connect to the &lt;strong&gt;World Wide Web (www)&lt;/strong&gt; commonly known as &lt;strong&gt;the Web&lt;/strong&gt; where we can access various resources such as documents, audios, pictures, and videos. The web became very simple to use despite its complexity over time, and even a 7-year-old Larry can open his mom's iPad to watch his favorite cartoon on YouTube.&lt;/p&gt;

&lt;p&gt;Have you ever wondered what's happening behind the scenes for someone like young Larry to go on YouTube to play the video, or what the moving pieces are for you to get to a website? You came to the right place. Let us go over the key players of the web to do its work.&lt;/p&gt;




&lt;h2&gt;
  
  
  The history of the World Wide Web
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2xekhdz3i8nqw2upgk5.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2xekhdz3i8nqw2upgk5.jpg" alt="sir-tim-berners-lee"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;World Wide Web&lt;/strong&gt;, also known as the web was invented by a British computer scientist named &lt;em&gt;Sir Tim Berners-Lee&lt;/em&gt; back in 1989. His parents were computer scientists, but Tim was more interested in trains growing up. He got into electronics as he had to build electronic gadgets to control the trains. Eventually, he got more interested in electronics than trains which is how he started working on computers and software.&lt;/p&gt;

&lt;p&gt;Tim became a software engineer at a physics laboratory in Switzerland named &lt;a href="https://home.cern/" rel="noopener noreferrer"&gt;&lt;strong&gt;CERN&lt;/strong&gt;&lt;/a&gt; after graduating from &lt;em&gt;Oxford University&lt;/em&gt;. Back then, things were very manual and offline which you had to log on to different computers to get the information stored. Some computers used different programs, so you had two choices: &lt;em&gt;learn a different program on each computer to access the information&lt;/em&gt;, or &lt;em&gt;go have a coffee chat with your co-worker to ask how they work.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With millions of computers being connected together through the internet, Tim envisioned a technology to connect the world with information and proposed his idea to his supervisor at &lt;em&gt;CERN&lt;/em&gt;. Although it was never an official project, his supervisor gave him time to work on the big task.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2zvoyagtahka8hxgxsgv.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2zvoyagtahka8hxgxsgv.jpg" alt="www-proposal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tim has developed the three fundamental technologies by 1990 that are the foundation of the web today: &lt;strong&gt;HTML&lt;/strong&gt;, &lt;strong&gt;URI&lt;/strong&gt;, and &lt;strong&gt;HTTP&lt;/strong&gt;. Over time, the web has grown and Tim realized that the web's true potential would only prevail if it was accessible by anyone, anywhere without any cost or permission. &lt;em&gt;CERN&lt;/em&gt; agreed to make the web free forever and announced the decision in April 1993. The web has grown ever since with the number of websites totaling &lt;strong&gt;1.8 Billion&lt;/strong&gt; as of April 2021.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“Had the technology been proprietary, and in my total control, it would probably not have taken off. You can’t propose that something be a universal space and at the same time keep control of it.”&lt;/em&gt; - Sir Tim Berners-Lee&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Modern Days of the Web
&lt;/h2&gt;

&lt;p&gt;Enough of the history lessons on the web, it's time to talk about how the offspring of Tim's noble task look like today. To summarize the flow of the web, a browser sends an HTTP request to a server to access specific content, and the server returns an HTTP response of that requested data back to the browser. We will get into more detail but it looks something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkf501vdvia0odcuihzp3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkf501vdvia0odcuihzp3.png" alt="modern-web"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's go through each of the key players of the web.&lt;/p&gt;

&lt;h3&gt;
  
  
  Client, Server, and HTTP
&lt;/h3&gt;

&lt;p&gt;I spend a lot of free time watching YouTube. It is probably the website I spent the most time in my life. In order for me to get to the website to watch a video, I go through these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open my computer&lt;/li&gt;
&lt;li&gt;Open Google Chrome&lt;/li&gt;
&lt;li&gt;In the browser, I type &lt;a href="http://www.youtube.com" rel="noopener noreferrer"&gt;www.youtube.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;I get to the website&lt;/li&gt;
&lt;li&gt;Watch a video&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this example, I am the &lt;em&gt;client&lt;/em&gt; who requests access to a video that is in YouTube's database. YouTube is serving me by providing the web service to access the video, which makes YouTube the &lt;em&gt;server&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;client&lt;/strong&gt; is internet-connected computer hardware that uses client software like a web browser. Your computer and mobile device are clients that use browsers like Chrome, Firefox, or Safari. We often refer to the device, browser, and user using the device as clients. Clients can request access to the content that servers store.&lt;/p&gt;

&lt;p&gt;On the other hand, a &lt;strong&gt;server&lt;/strong&gt; is computer software and its hardware that &lt;em&gt;serves&lt;/em&gt; clients by receiving their requests and returning responses accordingly. Servers can show web pages, send/receive emails, store files and share them, or identify and authorize user accounts. &lt;/p&gt;

&lt;p&gt;I like to compare a client and a server relationship as a patron and a librarian at a public library. The patron can ask the librarian a book he is looking for, and the librarian will respond with the location of the book if they have a copy of it. Just like that, a client can send a request to a server to view a web document.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fegkbeud4qjbfrjmblrdp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fegkbeud4qjbfrjmblrdp.png" alt="librarian-cartoon"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When humans speak to each other, we use a shared language and follow its grammar structure to deliver our messages across. Clients and servers do the same by using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP" rel="noopener noreferrer"&gt;&lt;strong&gt;Hypertext Transfer Protocol (HTTP)&lt;/strong&gt;&lt;/a&gt; which is a request-response protocol they expect from each other when exchanging data. &lt;/p&gt;

&lt;p&gt;A client communicates with a server by sending an &lt;strong&gt;HTTP request&lt;/strong&gt; containing information on what the client is looking for, and the server responds to the client by returning an &lt;strong&gt;HTTP response&lt;/strong&gt; as a result of the request. HTTP requests and responses both have &lt;strong&gt;HTTP Header&lt;/strong&gt;, which allows clients and servers to understand each other better. HTTP Headers contain information like the client's setup (browser, operating system), browser cookie, and domain name the client wants to reach. &lt;/p&gt;

&lt;p&gt;HTTP response often contains the resource data that was requested, and the status of the requested action to indicate if it was successful or not. As the name "Hypertext" suggests, HTTP requests and responses transfer content that is beyond just text. The content could be &lt;strong&gt;code files&lt;/strong&gt; like HTML, CSS, JavaScript, or &lt;strong&gt;assets&lt;/strong&gt; like images, audio, video, documents, and etc. &lt;/p&gt;

&lt;p&gt;It is possible for hackers to intercept the data in the middle and see the data being exchanged though. This could result in horrible outcomes logging into a bank account, email, or health insurance. That is why &lt;strong&gt;Hypertext Transfer Protocol Secure (HTTPS)&lt;/strong&gt; was introduced to encrypt the data. With &lt;em&gt;HTTPS&lt;/em&gt;, hackers will see encrypted meaningless characters even if they were to intercept the data. The data can be decrypted by using the shared secret key between the client and the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qh86byb84hrtxqoty2k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qh86byb84hrtxqoty2k.png" alt="web-workflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Internet, TCP/IP, and DNS
&lt;/h3&gt;

&lt;p&gt;For clients and servers to be able to communicate, they connect to the global system of computer networks called the &lt;strong&gt;Internet.&lt;/strong&gt; We pay a monthly fee to the &lt;strong&gt;internet service providers (ISP)&lt;/strong&gt; to be able to connect to the network. The internet uses the internet protocol suite (TCP/IP) to exchange data packets between computers. &lt;/p&gt;

&lt;p&gt;These &lt;strong&gt;packets&lt;/strong&gt; are fragments of data that allow data to be transferred reliably and efficiently. Transferring a large file instead of packets would be inefficient as the speed of the data transfer varies based on how you are sending them (optical cable, copper wire, or satellite). It can result in an unexpected loss of data or a change in the order of the packets. this is where the &lt;em&gt;internet protocol suite&lt;/em&gt; comes into play. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88w3v552ae4n6q1bw3mk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88w3v552ae4n6q1bw3mk.png" alt="packet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;internet protocol suite&lt;/strong&gt; is a communication protocol that ensures the successful exchange of data to an intended destination. It consists of two protocols: &lt;em&gt;TCP&lt;/em&gt; and &lt;em&gt;IP&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TCP&lt;/strong&gt; stands for &lt;strong&gt;Transmission Control Protocol&lt;/strong&gt; which defines the model of the data and assigns numbers to each data packet being transferred. With the numbers assigned to each packet, it can detect loss of data during the transfer to fix them and reassemble them in the right order as one file again. Due to its complexity, it makes TCP very reliable.&lt;/p&gt;

&lt;p&gt;In order for data to get to the right place between computers, it requires the addresses of each computer. &lt;strong&gt;IP&lt;/strong&gt; stands for &lt;strong&gt;Internet Protocol&lt;/strong&gt; which routes data to the right location. IPs are numbers of unique computer addresses with a mix of digits and periods like &lt;code&gt;192.158. 1.38&lt;/code&gt; (IPv4). With the web growing its size every day, a new version of IP, IPv6 was deployed to fulfill the need for more internet addresses. Compared to its previous version with a 32-bit binary IP address, IPv6 uses a 128-bit binary IP address which allows 340 undecillion unique address space! Here's an example of an IPv6 address: &lt;code&gt;2001:0db8:85a3:0000:0000:8a2e:0370:7334&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeu5z70mxrbmjpotnyax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeu5z70mxrbmjpotnyax.png" alt="network-packet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These IP addresses are not that human-readable though, and we'd need address books to keep all the websites' IP addresses. And it'd be very inconvenient if we had to look up Google's IP address and type &lt;code&gt;http://142.250.188.238/&lt;/code&gt; in the browser to get there every time. To solve this problem, the &lt;strong&gt;Domain Name System (DNS)&lt;/strong&gt; was introduced. The DNS is like the address book of the internet. We purchase domains from DNS providers, website addresses that are more human-readable like &lt;em&gt;google.com&lt;/em&gt;, &lt;em&gt;youtube.com&lt;/em&gt;, or &lt;em&gt;facebook.com&lt;/em&gt;. With the domains purchased for the websites, the DNS provider is responsible for exchanging domain URLs to IP addresses to the clients.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmj47og7s4qtyfph6qdw.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmj47og7s4qtyfph6qdw.jpg" alt="client-dns"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Typical Flow of the Web
&lt;/h3&gt;

&lt;p&gt;With that being said, let's take a look at an example of a user accessing YouTube's home page and break down what's happening behind the scenes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxh4pa7y21xnratdi9pbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxh4pa7y21xnratdi9pbg.png" alt="web-works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;User opens his laptop (&lt;em&gt;client&lt;/em&gt;) that is connected to the internet and opens Google Chrome (&lt;em&gt;browser&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;User types in the web address &lt;a href="http://www.youtube.com" rel="noopener noreferrer"&gt;www.youtube.com&lt;/a&gt; to the browser address bar&lt;/li&gt;
&lt;li&gt;The browser goes to the &lt;em&gt;DNS server&lt;/em&gt; and exchange the web address into an &lt;em&gt;IP address&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;The browser uses the &lt;em&gt;IP address&lt;/em&gt; to make an &lt;em&gt;HTTP request&lt;/em&gt; to YouTube's &lt;em&gt;server&lt;/em&gt; to access the website page&lt;/li&gt;
&lt;li&gt;YouTube server looks at the &lt;em&gt;HTTP request&lt;/em&gt;, prepares the data into &lt;em&gt;packets&lt;/em&gt; and &lt;em&gt;TCP&lt;/em&gt; numbers each &lt;em&gt;packet&lt;/em&gt; (many companies have their data stored in services like Oracle Cloud or AWS)&lt;/li&gt;
&lt;li&gt;YouTube &lt;em&gt;server&lt;/em&gt; responds with an &lt;em&gt;HTTP response&lt;/em&gt; with a "200 OK" status code (means the request was processed successfully) to the user's browser&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;TCP&lt;/em&gt; assembles the &lt;em&gt;packets&lt;/em&gt; back to the data as a whole, and the web page is displayed by parsing &lt;em&gt;HTML&lt;/em&gt;, &lt;em&gt;CSS&lt;/em&gt;, &lt;em&gt;JavaScript&lt;/em&gt; and its assets like images and videos&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgidlfw8fe90z0n4gsgz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffgidlfw8fe90z0n4gsgz.png" alt="wfh"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine the world without the web. Imagine doing your school projects without any access to Google, just like back a couple of decades ago when things were simple without any power of the internet. You would have to access offline documents like books, newspapers, or magazines. Researchers had to fly across the country to interview the right personnel to collect data.&lt;/p&gt;

&lt;p&gt;The web has made many things possible by connecting humanity all over the world to exchange information. You can now watch Netflix anywhere with the internet instead of going to a Blockbuster store to rent a DVD, have video chats with friends and family across the globe, or simply Google any information you are looking for. Especially the COVID-19 pandemic really showed the power of the web by connecting the world regardless of the location. It minimized the damage to our society by allowing remote work, food delivery service, and quick/easy access to COVID-19 guidelines for anyone.&lt;/p&gt;

&lt;p&gt;Although you do not have to fully understand how the web works to be able to use them, I hoped to provide a little bit of history and knowledge on how the web operates. Feel free to comment below with additional information! Thank you so much for taking time to read this blog post.&lt;/p&gt;

&lt;p&gt;Follow my blog account or let's connect on &lt;a href="https://www.linkedin.com/in/chuckchoi/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; to keep up with more tech content!&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Is "this" really that complicated in JavaScript?</title>
      <dc:creator>Chuck Choi</dc:creator>
      <pubDate>Mon, 19 Apr 2021 15:03:31 +0000</pubDate>
      <link>https://forem.com/chuckchoiboi/is-this-really-that-complicated-in-javascript-4o3h</link>
      <guid>https://forem.com/chuckchoiboi/is-this-really-that-complicated-in-javascript-4o3h</guid>
      <description>&lt;p&gt;&lt;code&gt;this&lt;/code&gt; keyword can be one of the most confusing monsters in JavaScript. Especially those who didn't learn JavaScript as their first programming language find it pretty confusing as it behaves differently in JS compared to the other languages. And many programmers rely on libraries like jQuery, so they learn how to use it but don't get to fully comprehend its fundamentals. Well, let me show you how &lt;code&gt;this&lt;/code&gt; isn't as confusing as you think.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is "this" in JavaScript?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt;MDN Web Doc&lt;/a&gt; explains that &lt;code&gt;this&lt;/code&gt; is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A property of an execution context&lt;/strong&gt; (global, function or eval) that, in non–strict mode, is always a reference to an object and in strict mode can be any value. In most cases, &lt;strong&gt;the value of &lt;code&gt;this&lt;/code&gt; is determined by how a function is called&lt;/strong&gt; (runtime binding). It can't be set by assignment during execution, and it may be different each time the function is called.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To put it simply, &lt;strong&gt;&lt;code&gt;this&lt;/code&gt; is a keyword used to reference the execution context.&lt;/strong&gt; We could think of it as "whoever it is that calls the function." So &lt;code&gt;this&lt;/code&gt; typically refers to the object that is invoking its method. In JavaScript, any value can be determined when it is defined or when the function is called. The latter is the case for &lt;code&gt;this&lt;/code&gt; keyword usually in the case of &lt;strong&gt;Implicit binding&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;Implicit binding&lt;/strong&gt;, the value of &lt;code&gt;this&lt;/code&gt; is determined based on the execution context. But &lt;code&gt;this&lt;/code&gt; behaves differently if the function being invoked as an &lt;strong&gt;arrow function&lt;/strong&gt; or if you are using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode"&gt;strict mode&lt;/a&gt; introduced in ES5.  And there's &lt;strong&gt;Explicit binding&lt;/strong&gt; which you can explicitly bind &lt;code&gt;this&lt;/code&gt; keyword to an object you pass as an argument for &lt;code&gt;call()&lt;/code&gt;, &lt;code&gt;bind()&lt;/code&gt;, and &lt;code&gt;apply()&lt;/code&gt; methods. Let's dive in deeper to each of them.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. Object Method
&lt;/h3&gt;

&lt;p&gt;Take a look at the following 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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Slim Shady&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;myNameIs&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myName&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;hi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myNameIs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// prints 'Slim Shady'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code's &lt;code&gt;hi&lt;/code&gt; object has a method &lt;code&gt;myNameIs&lt;/code&gt; which logs &lt;code&gt;myName&lt;/code&gt; of &lt;code&gt;this&lt;/code&gt; in the console. When &lt;code&gt;hi&lt;/code&gt; object invokes its method like the following &lt;code&gt;hi.myNameIs()&lt;/code&gt;, the object who called the function is &lt;code&gt;hi&lt;/code&gt; which makes &lt;code&gt;this&lt;/code&gt; to be implicitly bound to &lt;code&gt;hi&lt;/code&gt; object. Hence, the console will log &lt;code&gt;myName&lt;/code&gt; of the object &lt;code&gt;'Slim Shady'&lt;/code&gt;. A simple way to look at it is that &lt;strong&gt;whatever is on the left side of a dot notation of a method being invoked is the object that &lt;code&gt;this&lt;/code&gt; will be referring to.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;How about this example though:&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;whatIsThis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;whatIsThis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints Window {...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hmm... we just discussed that an easy way to understand &lt;code&gt;this&lt;/code&gt; keyword in object method invocation is paying attention to the left side of dot notation. But this time, &lt;code&gt;whatIsThis&lt;/code&gt; is a function defined using function declaration, and we can execute it without an object which logs &lt;code&gt;this&lt;/code&gt; as the &lt;code&gt;window&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Sounds pretty confusing right? Well, when we declare a function, we are making it a global function available to the global object, so the function's containing scope is the global object &lt;code&gt;Window&lt;/code&gt;. Another way to execute &lt;code&gt;whatIsThis&lt;/code&gt; is: &lt;code&gt;window.whatIsThis()&lt;/code&gt;. Look at that, &lt;code&gt;window&lt;/code&gt; is on the left side of &lt;code&gt;whatIsThis()&lt;/code&gt;! This brings me to the next point -- &lt;code&gt;this&lt;/code&gt; in global context.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Global Context
&lt;/h3&gt;

&lt;p&gt;As we discussed, when a method inside an object is executed by the object, &lt;code&gt;this&lt;/code&gt; refers to the object. But what happens if I try to simply log &lt;code&gt;this&lt;/code&gt; into console? Give it a try in your browser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// prints Window {...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like &lt;code&gt;this&lt;/code&gt; refers to &lt;code&gt;window&lt;/code&gt; object. By default, &lt;code&gt;this&lt;/code&gt; refers to the global object (Window in browser environment). If we want to understand why it is, ask yourself (or Google) what the &lt;code&gt;window&lt;/code&gt; object is. If we take a look at &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Global_object"&gt;MDN Web Docs&lt;/a&gt; again, it explains:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The window object is the Global Object in the Browser. &lt;strong&gt;Any Global Variables or Functions can be accessed as properties of the window object.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we add &lt;code&gt;this&lt;/code&gt; to your browser console, your global environment Window is executing the expression &lt;code&gt;this&lt;/code&gt;, so the window object is being referred to in this global context. &lt;/p&gt;

&lt;p&gt;Back to the Slim Shady example, here's a function created using the &lt;code&gt;myNameIs&lt;/code&gt; method in &lt;code&gt;hi&lt;/code&gt; object earlier:&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;var&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Slim Shady&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;myNameIs&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myName&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;hisNameIs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myNameIs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;hisNameIs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interesting. &lt;code&gt;hisNameIs&lt;/code&gt; function logged &lt;code&gt;undefined&lt;/code&gt; in the console. Let's try to understand what happened at the line &lt;code&gt;var hisNameIs = hi.myNameIs&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;hi.myNameIs&lt;/code&gt; is a method -- a property containing a function definition. We simply declared a &lt;strong&gt;global function&lt;/strong&gt; named &lt;code&gt;hisNameIs&lt;/code&gt; by using &lt;code&gt;var&lt;/code&gt; and initialized it with the function definition from &lt;code&gt;hi&lt;/code&gt; object passed.&lt;/p&gt;

&lt;p&gt;Second, global functions are stored as properties in the &lt;code&gt;window&lt;/code&gt; object. When we invoke the global function &lt;code&gt;hisNameIs()&lt;/code&gt;, it is the same as &lt;code&gt;window.hisNameIs()&lt;/code&gt;. The window is the object that is executing its method &lt;code&gt;hisNameIs&lt;/code&gt;, so &lt;code&gt;this&lt;/code&gt; is now referring to the &lt;code&gt;window&lt;/code&gt; object. window object does not have a property named &lt;code&gt;myName&lt;/code&gt;, so it will return undefined.&lt;/p&gt;

&lt;p&gt;In conclusion, &lt;strong&gt;&lt;code&gt;this&lt;/code&gt; will refer to the global object in global context.&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Strict Mode
&lt;/h3&gt;

&lt;p&gt;JavaScript was first introduced in 1995 as &lt;em&gt;Mocha&lt;/em&gt; which took 10 days to develop by a Netscape programmer named &lt;strong&gt;Brandon Eich&lt;/strong&gt;. It would be surprising if the language came out to be perfect in 10 days of development right? The language has evolved to today's version 6 (ES6), with the language designers' attempt to correct the flaws in the past versions. Its legacy features were not possible to be removed in order to maintain the backward compatibility, which is why &lt;em&gt;strict mode&lt;/em&gt; was introduced in ES5 to opt in to correct the early language flaws. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; keyword is one of them. It behaves differently when you opt into &lt;em&gt;strict mode&lt;/em&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;whatIsThis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;whatIsThis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;em&gt;strict mode&lt;/em&gt;, &lt;code&gt;this&lt;/code&gt; keyword will default to undefined in function invocation. It is likely that &lt;code&gt;this&lt;/code&gt; keyword was not meant to point to the &lt;code&gt;window&lt;/code&gt; object, as you can simply use &lt;code&gt;window&lt;/code&gt; keyword to do so.&lt;/p&gt;

&lt;p&gt;In ES5, &lt;code&gt;bind()&lt;/code&gt; method was introduced to explicitly set the function's &lt;code&gt;this&lt;/code&gt; regardless of how it is called. You can pass an object as an argument when using &lt;code&gt;bind()&lt;/code&gt; method, and the function's &lt;code&gt;this&lt;/code&gt; keyword will refer to the object no matter how the function is invoked. Bringing back the code from earlier using &lt;code&gt;bind()&lt;/code&gt; method this time, we can now create a new function with object passed explicitly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Slim Shady&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;myNameIs&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myName&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;hisNameIs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myNameIs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;hisNameIs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// prints Slim Shady&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom! Even with the strict mode, &lt;code&gt;hisNameIs&lt;/code&gt; function's &lt;code&gt;this&lt;/code&gt; will refer to the &lt;code&gt;hi&lt;/code&gt; object passed no matter what. &lt;code&gt;call()&lt;/code&gt; and &lt;code&gt;apply()&lt;/code&gt; are basically the same which you can pass additional arguments to the function. The three methods are slightly different which you can read more about in this &lt;a href="https://medium.com/@omergoldberg/javascript-call-apply-and-bind-e5c27301f7bb"&gt;blog post&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Arrow function
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; inside an &lt;strong&gt;arrow function&lt;/strong&gt; behaves a bit differently compared to the one inside a function declaration or a function expression. Arrow function was introduced in ES6 as an alternative to a traditional way of defining function. Let's compare these two objects using the different versions of 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;// using regular function as callback inside forEach()&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;oldPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chuck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;apps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Facebook&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;YouTube&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;Uber&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;useApps&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;apps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; is using &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="c1"&gt;// this refers to the window object&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;oldPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useApps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// prints undefined is using Facebook&lt;/span&gt;
&lt;span class="c1"&gt;// prints undefined is using YouTube&lt;/span&gt;
&lt;span class="c1"&gt;// prints undefined is using Uber&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;oldphone.useApps&lt;/code&gt; function iterates each of the &lt;code&gt;apps&lt;/code&gt; using &lt;code&gt;forEach&lt;/code&gt; with a regular function passed as a callback function. However, the callback function inside &lt;code&gt;forEach&lt;/code&gt; method does not bind to the original object. Instead, it will bind to the global &lt;code&gt;window&lt;/code&gt; object thus &lt;code&gt;this.owner&lt;/code&gt; returns &lt;code&gt;undefined&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This could be very inconvenient if we were doing something similar as a &lt;code&gt;class&lt;/code&gt;. There are two ways to fix it though, &lt;code&gt;forEach()&lt;/code&gt; method takes an optional argument &lt;code&gt;thisArg&lt;/code&gt; in addition to the callback function like this: &lt;code&gt;arr.forEach(callback[, thisArg])&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Or we can use an &lt;strong&gt;arrow function&lt;/strong&gt; as a callback to utilize its &lt;em&gt;lexical scoping&lt;/em&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;// using arrow function as callback inside forEach()&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newPhone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chuck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;apps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Facebook&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;YouTube&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;Uber&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;useApps&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;apps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; is using &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;newPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useApps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// prints Chuck is using Facebook&lt;/span&gt;
&lt;span class="c1"&gt;// prints Chuck is using YouTube&lt;/span&gt;
&lt;span class="c1"&gt;// prints Chuck is using Uber&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila! This time the callback function's &lt;code&gt;this&lt;/code&gt; referred to &lt;code&gt;newPhone&lt;/code&gt;, and logged &lt;code&gt;this.owner&lt;/code&gt; as &lt;code&gt;'Chuck'&lt;/code&gt;. Arrow function allows you to write functions in a cleaner way, and they have &lt;strong&gt;lexical scope&lt;/strong&gt; I mentioned earlier which means that they will inherit the scope from its parent. &lt;/p&gt;

&lt;p&gt;The callback function nested inside the &lt;code&gt;forEach&lt;/code&gt; method above inherited the scope from its parent &lt;code&gt;useApps&lt;/code&gt; which is &lt;code&gt;newPhone&lt;/code&gt; object. Because of this nature, &lt;strong&gt;The value of &lt;code&gt;this&lt;/code&gt; inside an arrow function is determined when that arrow function is defined&lt;/strong&gt; unlike the typical situations from earlier. I personally think that &lt;code&gt;this&lt;/code&gt; inside an arrow function is the most confusing part of &lt;code&gt;this&lt;/code&gt; keyword, but it simply inherits the scope from its parent.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;bind()&lt;/code&gt;, &lt;code&gt;call()&lt;/code&gt;, and &lt;code&gt;apply()&lt;/code&gt; are not compatible with arrow functions. Arrow functions will inherit the scope from its parent regardless.&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;p&gt;To conclude, let's summarize how &lt;code&gt;this&lt;/code&gt; works in JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; is a keyword used to reference the execution context&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;method invocation&lt;/strong&gt;, the object that is invoking the method would be the execution context &lt;code&gt;this&lt;/code&gt; will refer to&lt;/li&gt;
&lt;li&gt;In global context like regular function invocation, &lt;code&gt;this&lt;/code&gt; will default to the global object&lt;/li&gt;
&lt;li&gt;In &lt;em&gt;strict mode&lt;/em&gt;, &lt;code&gt;this&lt;/code&gt; keyword will default to undefined in regular function invocation.&lt;/li&gt;
&lt;li&gt;You can use &lt;code&gt;bind()&lt;/code&gt;, &lt;code&gt;call()&lt;/code&gt;, or &lt;code&gt;apply()&lt;/code&gt; to explicitly bind an object to a function&lt;/li&gt;
&lt;li&gt;An arrow function will inherit the scope from its parent, so &lt;code&gt;this&lt;/code&gt; inside an arrow function will follow its parent's &lt;code&gt;this&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bind()&lt;/code&gt;, &lt;code&gt;call()&lt;/code&gt;, and &lt;code&gt;apply()&lt;/code&gt; do not work for arrow functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope this was a helpful resource for you to understand how &lt;code&gt;this&lt;/code&gt; works in JavaScript. Feel free to comment below if you have any questions or notice any inaccurate information and I will respond as soon as possible :)&lt;/p&gt;

&lt;p&gt;Follow me on Clubhouse &lt;a class="mentioned-user" href="https://dev.to/chuckchoiboi"&gt;@chuckchoiboi&lt;/a&gt;
&lt;/p&gt;

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