<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Waverley Leung</title>
    <description>The latest articles on DEV Community by Waverley Leung (@wlcreate).</description>
    <link>https://dev.to/wlcreate</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%2F534163%2Fcc4a8e16-f708-4f0f-bb60-0c413b4fac01.jpeg</url>
      <title>DEV Community: Waverley Leung</title>
      <link>https://dev.to/wlcreate</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wlcreate"/>
    <language>en</language>
    <item>
      <title>Big-O Notation from a Non-CS Perspective</title>
      <dc:creator>Waverley Leung</dc:creator>
      <pubDate>Tue, 01 Jun 2021 21:30:52 +0000</pubDate>
      <link>https://dev.to/wlcreate/big-o-notation-from-a-non-cs-perspective-2edj</link>
      <guid>https://dev.to/wlcreate/big-o-notation-from-a-non-cs-perspective-2edj</guid>
      <description>&lt;p&gt;Hi everyone!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/dzaUX7CAG0Ihi/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/dzaUX7CAG0Ihi/giphy.gif" alt="bear waving hello"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Welcome to the second post in our Data Structures &amp;amp; Algorithm series! Last time we reviewed the crossovers in &lt;a href="https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-4ek4"&gt;JavaScript arrays and strings&lt;/a&gt;. This time we will cover Big O notation, diving into time and space complexity. &lt;/p&gt;

&lt;p&gt;Both of us (&lt;a href="https://dev.to/mehmehmehlol"&gt;Megan&lt;/a&gt; and I) graduated from bootcamp, after learning Ruby on Rails, JavaScript, React, etc., we had to spend a lot of our time learning Big-O Notation through many online resources. We hope this will be the place for you if you are looking for a “plain English” explanation of Big-O Notation!&lt;/p&gt;

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

&lt;p&gt;In computer science, Big O notation is used to classify the run time or space requirements of an algorithm as their input size grows. For CS students at college, they have to learn different types of big- notation (Big O, Big Theta, Big Omega). But for the sake of software engineering technical interviews, all we care about is the best and worst case scenarios, which Big O provides the tightest description of the run time and space. &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%2F3sahbrfd6besc18arjsr.jpeg" 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%2F3sahbrfd6besc18arjsr.jpeg" alt="Big O Notation Graph"&gt;&lt;/a&gt;&lt;br&gt;
This graph demonstrates clearly on how the run time and space changes depending on the input of a Big-O Notation. &lt;code&gt;O(1)&lt;/code&gt; and &lt;code&gt;O(log n)&lt;/code&gt; have the best run time and space complexity while &lt;code&gt;O(n!)&lt;/code&gt;, &lt;code&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/code&gt; and &lt;code&gt;O(2&lt;sup&gt;n&lt;/sup&gt;)&lt;/code&gt; have the worst run time and space complexity.&lt;/p&gt;

&lt;p&gt;In this article, we will break down all these notations with provided examples and Leetcode questions at the end of each part. &lt;/p&gt;
&lt;h2&gt;
  
  
  What does it mean by brute force and optimized solution?
&lt;/h2&gt;

&lt;p&gt;Before we start, we would like to explain what brute force and optimized solution mean, as you might see these keywords later in the article. &lt;/p&gt;

&lt;p&gt;The easiest way to understand what &lt;strong&gt;brute force solution&lt;/strong&gt; is whatever solution comes to your head first. On the other hand, for &lt;strong&gt;optimized solution&lt;/strong&gt;, after you have the brute force solution, you would think of an optimized solution to either simplify the code or minimize the time and space complexity if possible. &lt;/p&gt;

&lt;p&gt;For instance, your brute force solution has a &lt;code&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/code&gt; time complexity and with optimized solution, you are able to reduce it to the time complexity of &lt;code&gt;O(n)&lt;/code&gt;. &lt;br&gt;
Understanding this concept is important since this is something you would discuss with your interviewer on how you would make your solution from brute force to more optimized.&lt;/p&gt;
&lt;h2&gt;
  
  
  Complexity Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Big O Notations&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Constant Time&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Logarithmic Time&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linear Time&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linearithmic Time&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quadratic Time&lt;/td&gt;
&lt;td&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exponential Time&lt;/td&gt;
&lt;td&gt;O(2&lt;sup&gt;n&lt;/sup&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Factorial Time&lt;/td&gt;
&lt;td&gt;O(n!)&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h3&gt;
  
  
  Constant Time: O(1)
&lt;/h3&gt;

&lt;p&gt;Often referred to as “constant time”, &lt;code&gt;O(1)&lt;/code&gt; has the least complexity. I like to think of this as no matter how large or small the input, you can always expect the same number of steps to execute inside the function.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHelloToFirstFriend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;friends&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="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;friend&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="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;sayHelloToFirstFriend&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;spongebob&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;patrick&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;sandy&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;squidward&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;gary&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;// “Hello spongebob”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;th&gt;Typical use cases&lt;/th&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://dev.to/lukocastillo/time-complexity-big-0-for-javascript-array-methods-and-examples-mlg%E2%80%9D"&gt;Accessing an Array through its Index&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/implementation-stack-javascript/%E2%80%9D"&gt;Inserting (push) or deleting (pop) from a Stack&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/implementation-linkedlist-javascript/%E2%80%9D"&gt;Inserting or deleting a node in a Linked List&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/implementation-queue-javascript/%E2%80%9D"&gt;Insertion or deleting from a Queue&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.freecodecamp.org/news/how-to-implement-a-simple-hash-table-in-javascript-cb3b9c1f2997/%E2%80%9D"&gt;Searching, inserting, or deleting from a Hash Table&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Logarithmic Time: O(log n)
&lt;/h3&gt;

&lt;p&gt;Don’t be afraid of the math! When you see a logarithm it’s asking you, “What power must we raise this base to, in order to get this answer?" In other words, we use logarithms to solve for a variable when that variable is an exponent. &lt;/p&gt;

&lt;p&gt;In terms of computer science this translates to: “How many times must we divide n in half in order to get back down to 1?” Therefore, solutions with &lt;code&gt;O(log n)&lt;/code&gt; essentially divide the problem in half, determine which half it needs to continue, divide that section in half, repeating this same idea until it finds what it needs or ruling out the set. As a result, while these solutions do grow more than constant time, it nonetheless grows slowly compared to other time complexities.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Typical use cases&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search%E2%80%9D"&gt;Binary Search&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/linear-time-merging%E2%80%9D"&gt;Certain Divide and Conquer Algorithms based on Linear functionality&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/count-fibonacci-numbers-given-range-log-time/%E2%80%9D"&gt;Calculating Fibonacci Numbers&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Note: Notice that for all of these use cases the input is sorted and searching for something!&lt;/p&gt;

&lt;h3&gt;
  
  
  Linear Time: O(n)
&lt;/h3&gt;

&lt;p&gt;Probably the most familiar is &lt;code&gt;O(n)&lt;/code&gt;, or “linear time”. This is because as the size of the input grows, the time the number of operations takes to execute also grows. In other words, if an array has 10 items a for loop will be executed 10 times whereas if the array has 10,000 items the same for loop would execute 10,000 times as well.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;binarySearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;middle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;middle&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;middle&lt;/span&gt;
    &lt;span class="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;guess&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// search the right side of the list&lt;/span&gt;
      &lt;span class="nx"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;middle&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="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="c1"&gt;// search the left side of the list&lt;/span&gt;
      &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;middle&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// if target is not found&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 2:&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;sayHelloToFriends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;friends&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="nx"&gt;friends&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;friends&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;sayHelloToFriends&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;spongebob&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;patrick&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;sandy&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;squidward&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;gary&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;// “Hello spongebob”&lt;/span&gt;
&lt;span class="c1"&gt;// “Hello patrick”&lt;/span&gt;
&lt;span class="c1"&gt;// “Hello sandy”&lt;/span&gt;
&lt;span class="c1"&gt;// “Hello squidward”&lt;/span&gt;
&lt;span class="c1"&gt;// “Hello gary”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Typical use cases&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Traversing an &lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/ways-iterating-array-javascript/%E2%80%9D"&gt;Array&lt;/a&gt; or &lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/implementation-linkedlist-javascript/%E2%80%9D"&gt;Linked List&lt;/a&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/linear-search/%E2%80%9D"&gt;Linear Search&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/implementation-linkedlist-javascript/%E2%80%9D"&gt;Deletion of a specific element in a Linked List (Not sorted)&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.freecodecamp.org/news/two-ways-to-check-for-palindromes-in-javascript-64fea8191fd7/%E2%80%9D"&gt;Comparing two strings&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.freecodecamp.org/news/two-ways-to-check-for-palindromes-in-javascript-64fea8191fd7/%E2%80%9D"&gt;Checking for Palindrome&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.freecodecamp.org/news/javascript-loops-explained-for-loop-for/%E2%80%9D"&gt;Anytime using a `for` loop or iterating&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Linearithmic Time: O(n log n)
&lt;/h3&gt;

&lt;p&gt;Building off of typical solutions for &lt;code&gt;O(log n)&lt;/code&gt;, the extra “n” comes from the extra time cost of sorting. Therefore, a lot of sorting algorithms have the complexity of &lt;code&gt;O(n log n)&lt;/code&gt;. On the other hand, while it does take more time than &lt;code&gt;O(log n)&lt;/code&gt;, it’s also important to remember that logarithms grow very slowly. As a result, its path is similar to that of linear time. To explain a bit more of the role &lt;code&gt;n&lt;/code&gt; plays, let’s take a look at merge sort. &lt;/p&gt;

&lt;p&gt;Starting the same as &lt;code&gt;O(log n)&lt;/code&gt;, in merge sort you start by dividing the array in half. Next you sort the two halves and then merge the two sorted halves into one sorted whole. However, in order to sort the two halves you repeat the same idea of dividing them, sorting them, merging the sorted halves until you’ve sorted everything. &lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;right&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;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="c1"&gt;// Break out of loop if any one of the array gets empty&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;left&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;right&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="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Pick the smaller among the smallest element of left and right sub arrays &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;left&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;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;arr&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;left&lt;/span&gt;&lt;span class="p"&gt;.&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;.&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Concatenating the leftover elements&lt;/span&gt;
    &lt;span class="c1"&gt;// (in case we didn't go through the entire left or right array)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;half&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

  &lt;span class="c1"&gt;// Base case or terminating case&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&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;array&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;half&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="nf"&gt;mergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Typical use cases&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/merge-sort//%22"&gt;Merge Sort&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/heap-sort/%E2%80%9D"&gt;Heap Sort&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.geeksforgeeks.org/quick-sort/%E2%80%9D"&gt;Quick Sort&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="%E2%80%9Dhttps://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/divide-and-conquer-algorithms%E2%80%9D"&gt;Certain Divide and Conquer Algorithms based on optimizing O(n&lt;sup&gt;2&lt;/sup&gt;) algorithms&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Quadratic Time: O(n&lt;sup&gt;2&lt;/sup&gt;)
&lt;/h3&gt;

&lt;p&gt;A function with quadratic time complexity has a growth rate of n&lt;sup&gt;2&lt;/sup&gt;. Meaning? If the input size is 2, then the function has to do 4 times. If the input size is 3, then the function has to be done 9 times. If the input size is 1000, then the function has to do 1,000,000 (1 million) times.&lt;br&gt;
In other words, &lt;code&gt;O(n&lt;sup&gt;2&lt;/sup&gt;)&lt;/code&gt; is going to run really slow, especially since the input size is really large. &lt;/p&gt;

&lt;p&gt;Most of the time, we would describe an algorithm that has quadratic time when we have to iterate an object at least twice, examples like nested for loops.&lt;/p&gt;

&lt;p&gt;Find duplicates and bubble sort are two of the quadratic algorithms examples that you would run into. Bubble sort (as well as insertion sort and selection sort) is like the naive version of merge sort and quick sort. It is slow, but it is always the first concept you would first learn when learning sorting algorithms. It builds a great foundation for the rest of the more complicated sorting algorithms.&lt;/p&gt;

&lt;p&gt;What bubble sort does is repeatedly swapping adjacent elements if they are in the wrong order. Let's say we are sorting an unordered array of numbers from smallest to largest. Bubble sort would examine the numbers if they are in the right order by swapping them &lt;strong&gt;one by one&lt;/strong&gt; .&lt;/p&gt;

&lt;p&gt;Example of Bubble Sort:&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;bubbleSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&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="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;j&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&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="nf"&gt;swap &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&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="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// swap helper method&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;swap&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;second&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;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparing to Merge Sort, which the array would be cut in half , Bubble Sort would go through each element of the array &lt;strong&gt;one by one&lt;/strong&gt; until everything is sorted in the right place (and then it will go through again one more time even though it is already sorted.)&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Typical use cases&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://www.geeksforgeeks.org/bubble-sort/" rel="noopener noreferrer"&gt;Bubble Sort&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://www.geeksforgeeks.org/insertion-sort/" rel="noopener noreferrer"&gt;Insertion Sort&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://www.geeksforgeeks.org/insertion-sort/" rel="noopener noreferrer"&gt;Selection Sort&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://leetcode.com/problems/find-the-duplicate-number/" rel="noopener noreferrer"&gt;Find Duplicates (Brute Force)&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://www.geeksforgeeks.org/find-all-pairs-possible-from-the-given-array/" rel="noopener noreferrer"&gt;Find All Possible Ordered Pairs in An Array&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Exponential Time: O(2&lt;sup&gt;n&lt;/sup&gt;)
&lt;/h3&gt;

&lt;p&gt;Base-2 Exponential running time means the calculations will go double with each input size grows.&lt;br&gt;
2&lt;sup&gt;2&lt;/sup&gt; =&amp;gt; 4&lt;br&gt;
2&lt;sup&gt;3&lt;/sup&gt; =&amp;gt; 8&lt;br&gt;
2&lt;sup&gt;4&lt;/sup&gt; =&amp;gt; 16&lt;br&gt;
...&lt;br&gt;
2&lt;sup&gt;10&lt;/sup&gt; =&amp;gt; 1,267,650,600,228,229,401,496,703,205,376&lt;/p&gt;

&lt;p&gt;As you can see whenever &lt;code&gt;n&lt;/code&gt; is increased by 1, the result is doubled. Essentially, the number starts very low and to the end, the number will be very large.&lt;/p&gt;

&lt;p&gt;In most cases, please avoid the use of exponential time since the run time is going to get slower. Not that it's the worst one, but obviously it's not great. &lt;/p&gt;

&lt;p&gt;Fibonacci Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fib &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Typical use cases&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://www.educative.io/m/find-all-subsets" rel="noopener noreferrer"&gt;Power Set: Finding all the subsets on a set&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://leetcode.com/problems/fibonacci-number/" rel="noopener noreferrer"&gt;Fibonacci Number&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Factorial Time: O(n!)
&lt;/h3&gt;

&lt;p&gt;If you understood how factorial works, this is how it's like:&lt;br&gt;
5! = 5 x 4 x 3 x 2 x 1, in other words,&lt;br&gt;
n! = n x (n - 1) x (n - 2) x (n - 3)... x 1&lt;/p&gt;

&lt;p&gt;As the input size increases, the run time gets bigger and bigger and BIGGER! I personally have not encountered a factorial problem, therefore I would attach an example below with the link as reference.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Typical use cases&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href="https://adrianmejia.com/most-popular-algorithms-time-complexity-every-programmer-should-know-free-online-tutorial-course/#Permutations" rel="noopener noreferrer"&gt;Permutations&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;




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

&lt;p&gt;We hope this article gives you a better understanding of Big-O Notation! This concept is important since often during interviews, you will need to analyze the Big-O Notation of your solution. Furthermore, knowing this can help you understand which solution has better or worse runtime as you come up with approaches. If you are still having trouble understanding, we have provided more resources down below for you to reference! &lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://stackoverflow.com/questions/1592649/examples-of-algorithms-which-has-o1-on-log-n-and-olog-n-complexities" rel="noopener noreferrer"&gt;Examples of Algorithms which has O(1), O(n log n) and O(log n) complexities 👀&lt;/a&gt; (Stack Overflow)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.bigocheatsheet.com" rel="noopener noreferrer"&gt;Big-O Cheat Sheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.freecodecamp.org/news/big-o-notation-why-it-matters-and-why-it-doesnt-1674cfa8a23c/" rel="noopener noreferrer"&gt;What is Big O Notation Explained: Space and Time Complexity&lt;/a&gt; (FreeCodeCamp)&lt;/li&gt; 
&lt;li&gt;
&lt;a href="https://en.wikipedia.org/wiki/Big_O_notation" rel="noopener noreferrer"&gt;Big-O notation&lt;/a&gt; (Wikipedia)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://adrianmejia.com/most-popular-algorithms-time-complexity-every-programmer-should-know-free-online-tutorial-course/" rel="noopener noreferrer"&gt;8 time complexities that every programmer should know&lt;/a&gt; (With Videos and Examples)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://web.stanford.edu/class/cs9/sample_probs/TwoSum.pdf" rel="noopener noreferrer"&gt;Comparing different solutions for Two Sum&lt;/a&gt; (Stanford)&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Everything JavaScript Arrays &amp; Array Methods!</title>
      <dc:creator>Waverley Leung</dc:creator>
      <pubDate>Fri, 23 Apr 2021 20:49:19 +0000</pubDate>
      <link>https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-3n9</link>
      <guid>https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-3n9</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;What are arrays?&lt;/li&gt;
&lt;li&gt;How do we create arrays?&lt;/li&gt;
&lt;li&gt;
Methods

&lt;ul&gt;
&lt;li&gt;
Basic Methods: pop, push, shift, unshift, splice, slice&lt;/li&gt;
&lt;li&gt;
Advanced Methods: reduce, sort, concat, filter, join, map, find, forEach&lt;/li&gt;
&lt;li&gt;
Fun Methods: toString, includes, fill, indexOf, findIndex&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

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

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

&lt;/ul&gt;



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

&lt;p&gt;One data structure that many other complex ones build off of is the humble array. Therefore, it’s important to have a strong foundational understanding and knowledge of arrays before diving into other data structures. In this post we will cover what arrays are, how to create them, and 20 methods ranging from ones you will commonly use, some that are a bit more complex (mainly because of callback functions), and some fun ones to know as well. &lt;/p&gt;

&lt;p&gt;Before you go, if you’re interested in learning more data structures and want another resource for algorithms, check out the series &lt;a href="https://dev.to/mehmehmehlol"&gt;Megan Lo&lt;/a&gt; and I are collaborating on! The series will focus on data structures and algorithms, and our first post covers the crossover of Strings and Arrays. If you need a refresher on Strings, check out her post &lt;a href="https://dev.to/mehmehmehlol/when-to-use-these-string-methods-in-javascript-3m4h"&gt;here&lt;/a&gt;, otherwise visit our &lt;a href="https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-4ek4"&gt;collab&lt;/a&gt; and stay tuned for more!&lt;/p&gt;

&lt;p&gt;Without further ado, let’s dive into the wonderful world of arrays!&lt;/p&gt;

&lt;p&gt;P.S. Since this is a very long post, feel free to skip around as you like 😉&lt;/p&gt;



&lt;h2&gt;
  
  
  What are arrays? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;According to MDN, JavaScript arrays are “list-like objects whose prototype has methods to perform traversal and mutation operations”. In other words, arrays organize their items sequentially and have built-in methods that allow you to easily lookup and add/remove information based on its position. Array positions, also known as indexes, start at zero.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fun fact&lt;/em&gt;: Unlike non-scripting languages like Java, C, or C++, JavaScript (as a scripted language) does not have static arrays where you need to specify in advance how many elements you plan to store because they have a fixed size. Instead, JavaScript arrays are dynamic, meaning the size of it will grow or shrink as needed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fun Fact&lt;/em&gt;: Arrays are a special type of object! That said, the array’s object properties are kept separate from its elements and the methods you would use on the array’s elements cannot be used on its object properties. To set or access the array’s object property collection, you need to use bracket or dot notation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="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="c1"&gt;// “object”&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// “object”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrays have fast lookups of O(1) time because you can simply retrieve an element based on its given index, no matter how large the size of the array is.&lt;/li&gt;
&lt;li&gt;It is time expensive O(n)  to insert or delete from an array because it requires the other elements to “scoot” over to make room or fill the gap.&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  How do we create arrays? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Essentially there are two ways to create an array:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With the array literal
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;literalEmptyArray&lt;/span&gt; &lt;span class="o"&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;literalFilledArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;With the new constructor
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;constructorEmptyArray&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;constructorFilledArray&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That said, a third way to create an array is the &lt;code&gt;of&lt;/code&gt; method! Essentially the &lt;code&gt;of&lt;/code&gt; method creates a new Array instance from the passed in number of arguments, regardless of number or type of the arguments. The difference between the &lt;code&gt;of&lt;/code&gt; method and &lt;code&gt;Array&lt;/code&gt; constructor is what they do with the arguments; &lt;code&gt;Array.of(7)&lt;/code&gt; creates an array with a single element, 7, whereas &lt;code&gt;Array(7)&lt;/code&gt; creates an empty array with a length property of 7 (Note: this implies an array of 7 empty slots, not slots with actual undefined values)&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&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;// [1]&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// [1, 2, 3]&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [undefined]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





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

&lt;p&gt;Before going into some of the numerous (I roughly estimate more than 35) methods that arrays have, let’s first review what it means for something to be destructive vs non-destructive.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Destructive&lt;/em&gt;: The action mutates the original array, meaning that once you perform the action on the original array, you will not be able to have the original’s information again. Rather, the original has become updated.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Non-destructive&lt;/em&gt;: The action does not mutate the original array, meaning that once you perform the action on the original array, you will have the original’s information. Therefore, you will be able to have both the original and updated information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding and being aware of when a method is destructive vs non-destructive is important when you will ultimately need to decide which method to use. Now, let’s take a look at some basic, advanced, and fun methods!&lt;/p&gt;
&lt;h3&gt;
  
  
  Basic Methods &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The methods we will be covering are: &lt;code&gt;pop&lt;/code&gt;, &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;shift&lt;/code&gt;, &lt;code&gt;unshift&lt;/code&gt;, &lt;code&gt;splice&lt;/code&gt;, and &lt;code&gt;slice&lt;/code&gt;. To demonstrate each method, the base array &lt;a&gt;&lt;/a&gt; we will be referring to is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;iceCream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;vanilla&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;chocolate&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;strawberry&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;green&lt;/span&gt; &lt;span class="nx"&gt;tea&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 4 most common use cases for methods on an array is to destructively add or remove an element from the beginning or end of it. &lt;/p&gt;

&lt;p&gt;In case you can't see the image below, here's a summary of the methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;push&lt;/code&gt;&lt;/strong&gt;: Add an item to the end of an Array&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;pop&lt;/code&gt;&lt;/strong&gt;: Remove an item from the end of an Array&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;unshift&lt;/code&gt;&lt;/strong&gt;: Add an item to the beginning of an Array&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;shift&lt;/code&gt;&lt;/strong&gt;: Remove an item from the beginning of an Array&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%2F4g841pk0jc1v8ang6oed.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%2F4g841pk0jc1v8ang6oed.png" alt="basic-destructive-methods"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Other common cases are copying or removing a section of the array. While they have similar names, these methods are &lt;code&gt;splice&lt;/code&gt; and &lt;code&gt;slice&lt;/code&gt; and it's important to remember whether or not you want the action to be destructive or non-destructive.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;splice&lt;/code&gt;: Remove an item by index position (destructive)
&lt;/h4&gt;

&lt;p&gt;When using &lt;code&gt;splice&lt;/code&gt;, you must pass in what index you want to start removing items from (inclusive). You can optionally include a second argument index to say where you want to stop removing items from (inclusive), however if you don’t the method will automatically remove to the end.  Additionally, starting from the third argument, any that you include will be elements added to the array, beginning from start (first argument). If you do not specify any elements, &lt;code&gt;splice&lt;/code&gt; will only remove elements from the array. That said, if no arguments at all are passed in, the return value will be an empty array.&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;// general&lt;/span&gt;
&lt;span class="nb"&gt;Array&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;startIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// including the optional parameters&lt;/span&gt;
&lt;span class="nb"&gt;Array&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;startIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;endIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget the original array for the following example!&lt;br&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%2Fay1g79rufdmej02uunhu.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%2Fay1g79rufdmej02uunhu.png" alt="splice-method"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;slice&lt;/code&gt;: Copy an Array (non-destructive)
&lt;/h4&gt;

&lt;p&gt;If you simply want to make a copy of an array you do not need to pass in any arguments. That said, you do have the option to include the starting index (inclusive) and ending index (non-inclusive) to copy from. This method is often used over &lt;code&gt;splice&lt;/code&gt; because it avoids the “side-effect” of mutating the original array.&lt;/p&gt;

&lt;p&gt;If you do not pass in any arguments, by default the entire original array will be copied. If either index is negative, it extracts starting from the end or last element (&lt;code&gt;Array.length&lt;/code&gt; - index). On the other hand, if the arguments you pass in are larger than the actual array (for example, an array with 5 elements but passing in arguments to start at 10 and end at 50) the return value will be an empty array.&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;// general&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// including the optional parameters&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;startIndex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;endIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget the original array for the following example!&lt;br&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%2Fcjxyucp8qh6vlj50ovjg.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%2Fcjxyucp8qh6vlj50ovjg.png" alt="slice-method"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Advanced Methods &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The methods we will be covering in this section are: &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;sort&lt;/code&gt;, &lt;code&gt;concat&lt;/code&gt;, &lt;code&gt;flat&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;join&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;, and &lt;code&gt;forEach&lt;/code&gt;. Before continuing on, it’s important to know that many of the methods have the same parameters; in this case &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;, and &lt;code&gt;forEach&lt;/code&gt;. Instead of repeating it each time, I’ll leave the explanation of the parameters up here for you to refer to! &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three arguments you can pass into the callback function, two of them being optional. The one argument you must pass in is the current value, which represents the current element being processed. The other two arguments are the index of the current element and array the method was called upon. Besides the callback function, you can also use the &lt;code&gt;thisArg&lt;/code&gt; parameter, which is the value to use as &lt;code&gt;this&lt;/code&gt; when executing the callback. However, if the callback uses an arrow function the &lt;code&gt;thisArg&lt;/code&gt; can be omitted because all arrow functions lexically bind the &lt;code&gt;this&lt;/code&gt; value.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;reduce&lt;/code&gt;: Reduce to a single value (destructive)
&lt;/h4&gt;

&lt;p&gt;Essentially, the reduce method takes in a callback function that executes the callback on each element of the array, resulting in single output value. The callback (reducer) function you provide must at the very least include two parameters: the accumulator and current value. The accumulator accumulates callback's return values; in other words, it is the accumulated value previously returned in the last invocation of the callback On the hand, the current value is the value currently being processed in the array.&lt;/p&gt;

&lt;p&gt;Optionally, the reduce method can take in a second argument that will represent the initialValue. This value is what the accumulator will start as if it is passed in. Additionally, the callback can take in other parameters for the index and array, which represent the index of the current element being processed and the array the reduce method was called on.&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;currentValue&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;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// do something&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;currentValue&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;sort&lt;/code&gt;: Sorts the elements (destructive)
&lt;/h4&gt;

&lt;p&gt;When calling this method on the array it will sort it in place and return the sorted version. By default the elements will be sorted in &lt;strong&gt;ascending&lt;/strong&gt; order by converting the elements to strings and then comparing their Unicode code points. It’s important to know how they are sorted because in a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. Something important to note, all undefined elements are sorted to the end of the array.&lt;/p&gt;

&lt;p&gt;Optionally, and if you want to be more specific on how to sort (i.e. for integers), you can pass in a callback (compare) function that compares two arguments; the first and second element, often referred to as &lt;strong&gt;a&lt;/strong&gt; and &lt;strong&gt;b&lt;/strong&gt;, to each other. Under the hood, if the callback function returns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;less than 0, it means that the current order is correct; the first element will remain before the second element (&lt;strong&gt;a&lt;/strong&gt; will still come before &lt;strong&gt;b&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;0, it means that the elements are equal to each other; the order will remain the same with respect to each other, but sorted with respect to all different elements.&lt;/li&gt;
&lt;li&gt;greater than 0, it means that the current order is incorrect; the second element will be before the first element (&lt;strong&gt;b&lt;/strong&gt; before &lt;strong&gt;a&lt;/strong&gt;)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// including the optional parameters&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;concat&lt;/code&gt;: Merge arrays (non-destructive)
&lt;/h4&gt;

&lt;p&gt;This method is used to merge two or more arrays, returning a new array without mutating the originals. To be more specific, the new array has all of the elements of the array it is called on, followed in order by, for each argument, the elements of the argument or the argument itself. However, if an argument is a nested array it will not take the nested array out, rather it will only remove it from the array it is in (one-level deep)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fun Fact&lt;/em&gt;: &lt;code&gt;concat&lt;/code&gt; copies the object references of the original into the new array so that both the original and new array refer to the same object! Therefore, if a referenced object is modified, the changes are visible to both the new and original arrays.&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;Array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Array2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;flat&lt;/code&gt;: Creates a new array with the sub-array elements concatenated into it (non-destructive)
&lt;/h4&gt;

&lt;p&gt;Building off of the &lt;code&gt;concat&lt;/code&gt; method, the &lt;code&gt;flat&lt;/code&gt; method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. For a single level array, this accomplishes that same thing as the &lt;code&gt;reduce&lt;/code&gt; method calling upon the &lt;code&gt;concat&lt;/code&gt; method on its accumulator. On the other hand, to enable deep level flatten without the flat method, you can use recursion with reduce and concat.&lt;/p&gt;

&lt;p&gt;While not required, you can optionally pass in an argument that specifies how deep a nested array structure should be flattened. By default this argument is 1, for a single level array.&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// including the optional parameters&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flat&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;filter&lt;/code&gt;: Returns all the elements that pass the test function provided (non-destructive)
&lt;/h4&gt;

&lt;p&gt;This method creates a new array with all the elements that pass a callback (test) function. As it tests each element of the array it is called on, it returns a value that coerces to true to keep the element, or to false otherwise. When the value is false, it essentially skips over the element and does not include it in the array. If nothing passes the test, an empty array is returned. For more information on the parameters for this function, skip back to the beginning of the Advanced Methods section!&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// do something&lt;/span&gt;
   &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;join&lt;/code&gt;: Join all the elements from an array to a string (non-destructive)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;join&lt;/code&gt; creates and returns a string by concatenating, or joining, all of the elements of the array it was called on. By default the elements are separated by commas, however you can specify what you want to join/separate the elements by. On the other hand, if there is only one element in the array, the single item will be returned as a string without separators, and if there are no elements, an empty string is returned.&lt;/p&gt;

&lt;p&gt;As mentioned, including an argument for the separator parameter is optional if you want the elements to be joined with a comma. Passing in an empty string as the argument will result in the elements joined without any characters/separators. Otherwise, the parameter is what you want to separate each pair of adjacent elements of the array for the returned string. If necessary, the separator is converted to a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;map&lt;/code&gt;: Creates a new array with the results of a callback function (non-destructive)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;map&lt;/code&gt; takes in a callback function that is called once for every element of the array it is called on. Each time the callback is executed, it returns the value into the new array, which is returned at the end. That said, if you do not use the returned (new) array and/or do not return a value from the callback, using the &lt;code&gt;map&lt;/code&gt; method is considered anti-pattern. Instead you should use the &lt;code&gt;forEach&lt;/code&gt; method or a for loop. For more information on the parameters for this function, skip back to the beginning of the Advanced Methods section!&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// do something&lt;/span&gt;
   &lt;span class="nx"&gt;element&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;find&lt;/code&gt;: Return the value of the first element that satisfies the provided function (non-destructive)
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;find&lt;/code&gt; method only returns the first value of the element that satisfies the callback (test) function. If no element passes the test, the &lt;code&gt;find&lt;/code&gt; method will return undefined. That said, if you want to return the index of the element instead of its value, you can use the &lt;code&gt;findIndex&lt;/code&gt; method instead. For more information on the parameters for this function, skip back to the beginning of the Advanced Methods section!&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// do something&lt;/span&gt;
   &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;forEach&lt;/code&gt;: Loop over an Array (non-destructive)
&lt;/h4&gt;

&lt;p&gt;Similar to a &lt;code&gt;for&lt;/code&gt; loop, &lt;code&gt;forEach&lt;/code&gt; executes a callback function once for each element in the array. While the &lt;code&gt;forEach&lt;/code&gt; method will not mutate the array it was called on, it is possible for the callback function to mutate it. That said, the &lt;code&gt;forEach&lt;/code&gt; method expects a synchronous function, always returns undefined, and is not chainable. Therefore, the typical use case is to execute side effects at the end of a chain. For more information on the parameters for this function, skip back to the beginning of the Advanced Methods section!&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;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;element&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h3&gt;
  
  
  Fun Methods &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now, time for some “fun” methods! The methods we will cover here are: &lt;code&gt;toString&lt;/code&gt;, &lt;code&gt;includes&lt;/code&gt;, &lt;code&gt;fill&lt;/code&gt;, &lt;code&gt;indexOf&lt;/code&gt;, and &lt;code&gt;findIndex&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;toString&lt;/code&gt;: Returns a string representing the array and its elements (non-destructive)
&lt;/h4&gt;

&lt;p&gt;Like its name, the &lt;code&gt;toString&lt;/code&gt; method turns the array’s elements it was called on to a string. To be more specific, this method joins the array and returns one string containing each array element separated by commas.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fun fact&lt;/em&gt;: JavaScript calls the &lt;code&gt;toString&lt;/code&gt; method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;includes&lt;/code&gt;: Returns a boolean if a value exists in an array (non-destructive)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;includes&lt;/code&gt; determines whether an array includes a certain value among its entries, returning true or false as appropriate. It does this by checking each element for equality with the value rather than using a testing callback function. That said, if you need to find if any element satisfies a provided testing callback function, you can use the some method.&lt;/p&gt;

&lt;p&gt;The argument you must pass in is the value you want the method to search for; keep in mind when comparing strings and characters, &lt;code&gt;includes&lt;/code&gt; is case-sensitive. The optional second argument is the index to start searching for the value and by default is zero. That said, if the index passed in is greater than or equal to the length of the array, false is returned and the array will not be searched. On the other hand, if the index is negative the method uses the absolute value of it as the number of elements from the end of the array at which to start the search&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  &lt;code&gt;fill&lt;/code&gt;: Fills all the elements of an array with a static value (destructive)
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;fill&lt;/code&gt; method changes all elements in an array to a static value, from a start index to an end index. It then returns the modified array with the filled values.&lt;/p&gt;

&lt;p&gt;There are three parameters however only the first one is required. The first argument you must pass in is the value to fill the array with. It’s important to know that all of the elements in the array will be this exact value. The other two optional parameters are for the start index, default is zero, and the end index, default is array.length.&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;staticValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h4&gt;
  
  
  indexOf &amp;amp; findIndex: Find the index of an item in the Array (non-destructive)
&lt;/h4&gt;

&lt;p&gt;Similar to each other in that both return the first index that satisfies a condition. However, while &lt;code&gt;findIndex&lt;/code&gt; is based on the element that satisfies a testing callback function, &lt;code&gt;indexOf&lt;/code&gt; checks each element for equality with the value. Additionally, -1 is returned by &lt;code&gt;indexOf&lt;/code&gt; if the element you are searching for is not present, whereas -1 is returned by &lt;code&gt;findIndex&lt;/code&gt; if nothing satisfies the callback. That said, if you need to find if any element satisfies the provided testing function, you can use the some method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;findIndex&lt;/code&gt; has the same parameters detailed in the beginning of the Advanced Methods section. On the other hand, &lt;code&gt;indexOf&lt;/code&gt; takes in an argument for the element to search for and optionally the index to start searching for. If you include the second argument of the index to start searching and the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;startIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// do something&lt;/span&gt;
   &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&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%2Fyfrprhjctbudkz0l38ut.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%2Fyfrprhjctbudkz0l38ut.png" alt="indexOf-findIndex-methods"&gt;&lt;/a&gt; &lt;/p&gt;



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

&lt;p&gt;Congratulations! I declare you “master of JavaScript arrays and (most) array methods”! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/12Lt1KWHowKtKo/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/12Lt1KWHowKtKo/giphy.gif" alt="diploma"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But seriously though, this was a lot of information and I hope you’ll be able to refer to it in the future! Here are some key takeaways, as well as a reminder of what methods we covered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrays organize their items sequentially and have built-in methods that allow you to easily lookup and add/remove information based on its position.&lt;/li&gt;
&lt;li&gt;JavaScript arrays are special type of object and unlike non-scripting languages, are dynamic&lt;/li&gt;
&lt;li&gt;To create arrays you can use the array literal, new constructor, or of method&lt;/li&gt;
&lt;li&gt;Additionally, you can copy, concatenate arrays, and convert a string into an array by using the spread operator &lt;/li&gt;
&lt;li&gt;
Basic methods about adding, removing, or copying an array: &lt;code&gt;pop&lt;/code&gt;, &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;shift&lt;/code&gt;, &lt;code&gt;unshift&lt;/code&gt;, &lt;code&gt;splice&lt;/code&gt;, &lt;code&gt;slice&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
Advanced methods to merge: &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;concat&lt;/code&gt;, &lt;code&gt;flat&lt;/code&gt;, &lt;code&gt;join&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
Advanced methods do something based on a callback: &lt;code&gt;sort&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;forEach&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
Fun methods that have to do with the value or index: &lt;code&gt;includes&lt;/code&gt;, &lt;code&gt;indexOf&lt;/code&gt;, &lt;code&gt;findIndex&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
Fun methods to know: &lt;code&gt;toString&lt;/code&gt;, &lt;code&gt;fill&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you made it to the end thank you so much for reading, I hope you found it useful! I recommend checking out my friend Megan's &lt;a href="https://dev.to/mehmehmehlol/when-to-use-these-string-methods-in-javascript-3m4h"&gt;When to Use these String Methods in JavaScript&lt;/a&gt;, for a similar post on String methods. And don't forget about my &lt;a href="https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-4ek4"&gt;collaboration post&lt;/a&gt; with Megan that covers the crossovers between Strings and Arrays!&lt;/p&gt;





&lt;h2&gt;
  
  
  Resources &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="noopener noreferrer"&gt;All array methods&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.interviewcake.com" rel="noopener noreferrer"&gt;Interview Cake&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>From String to Array to String</title>
      <dc:creator>Waverley Leung</dc:creator>
      <pubDate>Fri, 23 Apr 2021 20:27:00 +0000</pubDate>
      <link>https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-4ek4</link>
      <guid>https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-4ek4</guid>
      <description>&lt;h2&gt;
  
  
  About Writers
&lt;/h2&gt;

&lt;p&gt;Hello everyone! &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2F37.media.tumblr.com%2F1dee0c9e399b34cc10c12d04506d5f10%2Ftumblr_n53cb0KpVC1smcbm7o1_500.gif" 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/http%3A%2F%2F37.media.tumblr.com%2F1dee0c9e399b34cc10c12d04506d5f10%2Ftumblr_n53cb0KpVC1smcbm7o1_500.gif" alt="Hello GIF"&gt;&lt;/a&gt;&lt;br&gt;
This is Waverley and Megan! We are both tech enthusiasts and graduated from Flatiron School recently. Both of us enjoy writing blogs to help other programmers learn and we learn from writing as well (win-win!). This will be our first time writing blogs on dev.to and collaborating on a series with each other, and we are so excited to create content for you all! &lt;strong&gt;Feedback is appreciated as we continue to navigate this process&lt;/strong&gt; 🙏 &lt;/p&gt;

&lt;p&gt;When we first decided to collaborate we came up with a lot of topics to write about. However, as we are both currently in our job search and as bootcamp graduates, we wanted to work on something that will help us now and others in the future. Ultimately, we decided to have this series focus on data structures and algorithms, something we both feel is important to know but not exactly the easiest to understand. &lt;/p&gt;

&lt;p&gt;Also, check out our Medium blogs for more of our work:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://waverley-place.medium.com/" rel="noopener noreferrer"&gt;Waverley&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://meganslo.medium.com/" rel="noopener noreferrer"&gt;Megan&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Intro &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Today, we are going to talk about converting array to string, and vice versa. This is one of the commonly seen strategies that is used when solving string-and-array-related coding questions. &lt;/p&gt;

&lt;p&gt;Before we get started, if you are interested in knowing array methods, here's &lt;a href="https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-3n9"&gt;All About JavaScript Arrays &amp;amp; Array Methods!&lt;/a&gt; by Waverley; and for string methods, here's &lt;a href="https://dev.to/mehmehmehlol/when-to-use-these-string-methods-in-javascript-3m4h"&gt;When to Use these String Methods in JavaScript&lt;/a&gt; by Megan.&lt;/p&gt;


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

&lt;ul&gt;
&lt;li&gt;Intro&lt;/li&gt;
&lt;li&gt;
From Array to String

&lt;ul&gt;
&lt;li&gt;toString()&lt;/li&gt;
&lt;li&gt;join()&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
From String to Array

&lt;ul&gt;
&lt;li&gt;split()&lt;/li&gt;
&lt;li&gt;Array.from()&lt;/li&gt;
&lt;li&gt;Object.assign([], string)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Bonus

&lt;ul&gt;
&lt;li&gt;Spread Operator&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  From Array to String &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Something that we often need to do is convert an array to a string. Thankfully JavaScript has two built-in methods to accomplish this: &lt;code&gt;toString()&lt;/code&gt; and &lt;code&gt;join()&lt;/code&gt;.&lt;/p&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt; &lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;toString()&lt;/code&gt;: Returns a string representing the array and its elements (non-destructive)
&lt;/h4&gt;

&lt;p&gt;Like its name, the &lt;code&gt;toString()&lt;/code&gt; method turns the array’s elements it was called on to a string. To be more specific, this method joins the array and returns one string containing each array element separated by commas.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Fun fact&lt;/em&gt;: JavaScript calls the toString method automatically when an array is to be represented as a text value or when an array is referred to in a string concatenation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&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;1a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// "1,2,a,1a"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;join()&lt;/code&gt;: Join all the elements from an array to a string (non-destructive)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;join()&lt;/code&gt; creates and returns a string by concatenating, or joining, all of the elements of the array it was called on. By default the elements are separated by commas, however you can specify what you want to join/separate the elements by. On the other hand, if there is only one element in the array, the single item will be returned as a string without separators, and if there are no elements, an empty string is returned.&lt;/p&gt;

&lt;p&gt;As mentioned, including an argument for the separator parameter is optional if you want the elements to be joined with a comma. Passing in an empty string as the argument will result in the elements joined without any characters/separators. &lt;/p&gt;

&lt;p&gt;Otherwise, the parameter is what you want to separate each pair of adjacent elements of the array for the returned string. If necessary, the separator is converted to a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;joinArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wind&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;Water&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;Fire&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;joinArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;      &lt;span class="c1"&gt;// 'Wind,Water,Fire' b/c no separator so joined with commas and no spaces&lt;/span&gt;
&lt;span class="nx"&gt;joinArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 'Wind, Water, Fire' b/c separator is comma and space&lt;/span&gt;
&lt;span class="nx"&gt;joinArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'Wind + Water + Fire' b/c separator is space, plus sign, space&lt;/span&gt;
&lt;span class="nx"&gt;joinArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“”&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// 'WindWaterFire' b/c separator is an empty string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the Table of Contents&lt;/p&gt;




&lt;h2&gt;
  
  
  From String to Array &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;split()&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;As mentioned above, we can &lt;code&gt;join&lt;/code&gt; string from array. We can also &lt;code&gt;split&lt;/code&gt; string to array. Here, we are going to utilize the &lt;code&gt;join()&lt;/code&gt; example to see the alternative results from above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;splitToArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wind, Water, Fire&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;splitToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="cm"&gt;/* [
     'W', 'i', 'n', 'd', ',',
     ' ', 'W', 'a', 't', 'e',
     'r', ',', ' ', 'F', 'i',
     'r', 'e'
   ], no separators
*/&lt;/span&gt;
&lt;span class="nx"&gt;splitToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;// [ 'Wind,', 'Water,', 'Fire' ], split by the whitespaces&lt;/span&gt;
&lt;span class="nx"&gt;splitToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="c1"&gt;// [ 'Wind', ' Water', ' Fire' ], split by commas&lt;/span&gt;
&lt;span class="c1"&gt;// As you can see there're still whitespaces in front of each element&lt;/span&gt;
&lt;span class="c1"&gt;// To solve this, we can do the following:&lt;/span&gt;
&lt;span class="nx"&gt;splitToArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;, &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [ 'Wind', 'Water', 'Fire' ], split by commas and one additional whitespace&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is probably the most common way to split a string to an array and join afterwards.&lt;/p&gt;

&lt;p&gt;Most of the time, when we want to apply array methods to a given string, this is a go-to method to do so. For instance, if we want to reverse a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;example&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In comparison to:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;reversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="c1"&gt;// having to create a new string takes space&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;char&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;reversed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;reversed&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;reversed&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;Unless your interviewer specifies &lt;code&gt;reverse()&lt;/code&gt; method is not allowed to be used, using the &lt;code&gt;split()&lt;/code&gt; and &lt;code&gt;join()&lt;/code&gt; method would help create a more readable, cleaner code. Also, since &lt;code&gt;reverse()&lt;/code&gt; is not a string method, this is where we can utilize our array method in this case.&lt;/p&gt;

&lt;p&gt;Palindrome is also another common question which you can make use of the &lt;code&gt;split()&lt;/code&gt; and &lt;code&gt;join()&lt;/code&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;code&gt;Array.from(string)&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;There are two ways you can use &lt;code&gt;Array.from()&lt;/code&gt;. Either you want to modify and create a new shallow-copied array OR &lt;em&gt;drumroll please&lt;/em&gt; 🥁🥁🥁 converting string to array.&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;// To modify an array (Not the focus of this section)&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [ 2, 4, 6, 8, 10 ]&lt;/span&gt;

&lt;span class="c1"&gt;// Convert string to array&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;J&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;a&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;v&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;a&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;S&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;c&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;i&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;p&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;t&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, these are just some suggestive ways of converting string to array. &lt;br&gt;
According to this &lt;a href="https://blog.shovonhasan.com/never-use-array-from-to-convert-strings-to-arrays/" rel="noopener noreferrer"&gt;article&lt;/a&gt;, using the same reversed example we have above, the runtime of using &lt;code&gt;String.prototype.split()&lt;/code&gt; is way faster than using &lt;code&gt;Array.from()&lt;/code&gt;. &lt;br&gt;
But you know, at least we know &lt;code&gt;Array.from()&lt;/code&gt; is one of the possible ways to convert string to array!&lt;/p&gt;


&lt;h4&gt;
  
  
  &lt;code&gt;Object.assign([], string)&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;You probably heard of &lt;code&gt;Object.keys()&lt;/code&gt;, &lt;code&gt;Object.entries()&lt;/code&gt; which would return a new array. What about &lt;code&gt;Object.assign()&lt;/code&gt;?&lt;br&gt;
Similar deal! &lt;br&gt;
According to MDN, the definition of &lt;code&gt;Object.assign()&lt;/code&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the target object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Object.assign()&lt;/code&gt; takes two arguments: &lt;code&gt;target&lt;/code&gt; and &lt;code&gt;source&lt;/code&gt;. In our case, our &lt;code&gt;target&lt;/code&gt; is an empty array &lt;code&gt;[]&lt;/code&gt; and our &lt;code&gt;source&lt;/code&gt; is the given string (which is an enumerable/ can be iterated object).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;([],&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ['f', 'o', 'o']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay! It works... on the surface... If you move this to TypeScript...&lt;/p&gt;




&lt;p&gt;✋🏻PAUSE✋🏻&lt;br&gt;
For those who are not familiar with TypeScript, don't worry. Here's a quick breakdown. You don't have to fully understand TypeScript. All you have to know is that TypeScript is a superset of JavaScript. Most of the time, variables are assigned to a primitive data type in advance.&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;// In JS&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num&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="c1"&gt;// In TS&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool thing about using TypeScript is that you would also see the variable type by hovering over the variable.&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%2Fbxwuodfn7dzmy9yshmc0.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%2Fbxwuodfn7dzmy9yshmc0.png" alt="Screenshot from CodeCademy"&gt;&lt;/a&gt;&lt;br&gt;
(Screenshot Credit: CodeCademy -- Learn TypeScript course)&lt;/p&gt;

&lt;p&gt;Capiche? Hopefully you got the gist of it. Back to the topic...&lt;/p&gt;



&lt;p&gt;If you move this to TypeScript, the type of this new copied array does not return as &lt;strong&gt;an array of string&lt;/strong&gt;. 😱 &lt;br&gt;
If we look at other methods we mentioned above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;split&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to copy and paste the code to the &lt;a href="https://www.typescriptlang.org/play/" rel="noopener noreferrer"&gt;playground&lt;/a&gt; and hover over &lt;code&gt;split&lt;/code&gt; and &lt;code&gt;from&lt;/code&gt;. &lt;br&gt;
They both return &lt;code&gt;split: string[]&lt;/code&gt; and &lt;code&gt;from: string[]&lt;/code&gt;. (&lt;code&gt;string[]&lt;/code&gt; means array of strings.)&lt;/p&gt;

&lt;p&gt;However, when you hover over &lt;code&gt;const obj = Object.assign([], str)&lt;/code&gt;. It returns...&lt;br&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%2F4arulbcswlcpc2uuhkoc.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%2F4arulbcswlcpc2uuhkoc.png" alt="Screenshot for Object.assign()"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;obj : never[] &amp;amp; "foo"&lt;/code&gt;, a.k.a. it is never an array but a string.&lt;/p&gt;

&lt;p&gt;Isn't that interesting? 😮 (thanks to our &lt;a href="https://www.samanthaming.com/tidbits/83-4-ways-to-convert-string-to-character-array/#a-caveat-about-object-assign-%E2%9A%A0%EF%B8%8F" rel="noopener noreferrer"&gt;resource&lt;/a&gt;)&lt;/p&gt;



&lt;p&gt;Although we have 3 ways to convert strings to array, including the spread operator (will be explained in the Bonus Section below), it seems like &lt;code&gt;split()&lt;/code&gt; is the best way to do so!&lt;/p&gt;

&lt;p&gt;Back to the Table of Contents&lt;/p&gt;


&lt;h3&gt;
  
  
  Bonus &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Spread Operator &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;While the spread operator (&lt;code&gt;...&lt;/code&gt;)  is not specific to arrays, it is something helpful to know and use! &lt;/p&gt;

&lt;p&gt;From MDN, the spread operator: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, essentially the spread operator takes each of the elements inside of the passed in object or array and adds it to the array it is being added to. &lt;/p&gt;

&lt;p&gt;As a result, it is often used when all elements from an object or array need to be included in another list of some kind; typical use cases include copying arrays or concatenating an array to the end of an existing array. However, specifically for copying an array, the spread operator goes one level deep. Meaning that if you spread an array into another array, the array it was spread into will not be nested.&lt;/p&gt;

&lt;p&gt;Furthermore, it’s important to know that you cannot spread objects into an array or an array into an object. This is because objects alone are not iterable. However, when used in an array or with iterating functions (such as &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;reduce&lt;/code&gt;), they become iterable. &lt;/p&gt;

&lt;p&gt;Under the hood, when the merging occurs 2 objects together with the spread operator, it is assumed that another iterating function is used. On the other hand, if you spread a string into an array, it will create an array with each char in the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// general example&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;elements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;constructorArray&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;newElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [4, 1, 2, 3]&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;literalArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;elements&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;// [4, 1, 2, 3]&lt;/span&gt;

&lt;span class="c1"&gt;// copying an array- showing how the spread operator does not nest&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [[1], [2], [3]]&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [[1], [2], [3]]&lt;/span&gt;

&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;shift&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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [[], [2], [3]]&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [[2], [3]]&lt;/span&gt;

&lt;span class="c1"&gt;// cannot spread an object into an array&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;key1&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;value1&lt;/span&gt;&lt;span class="dl"&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;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// TypeError: obj is not iterable&lt;/span&gt;

&lt;span class="c1"&gt;// spreading a string into an array&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Before You Go... &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Let's conclude what we have discussed!&lt;/p&gt;

&lt;h5&gt;
  
  
  From Array to String, we have...
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;toString()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;join()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  From String to Array, we have...
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;split()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Assign.from()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Object.assign([], string)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Last but not least, spread operator (&lt;code&gt;...&lt;/code&gt;) can be used to convert array to string, and vice versa. &lt;/p&gt;

&lt;p&gt;Hope you enjoy our first collab article and please give us feedback if you have!&lt;br&gt;
&lt;a href="https://i.giphy.com/media/M9NbzZjAcxq9jS9LZJ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/M9NbzZjAcxq9jS9LZJ/giphy.gif" alt="Thank You GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Back to the Table of Contents&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;String &amp;lt;=&amp;gt; Array

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.samanthaming.com/tidbits/83-4-ways-to-convert-string-to-character-array/" rel="noopener noreferrer"&gt;4 Ways to Convert String to Character Array in JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://flaviocopes.com/how-to-convert-array-to-string-javascript/" rel="noopener noreferrer"&gt;How to convert an Array to a String in JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Array

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dev.to/wlcreate/all-about-javascript-arrays-array-methods-3n9"&gt;All About JavaScript Arrays &amp;amp; Array Methods!&lt;/a&gt; (by Waverley)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;String

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dev.to/mehmehmehlol/when-to-use-these-string-methods-in-javascript-3m4h"&gt;When to Use these String Methods in JavaScript&lt;/a&gt; (by Megan)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

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

&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Nevertheless, Waverley Coded</title>
      <dc:creator>Waverley Leung</dc:creator>
      <pubDate>Tue, 30 Mar 2021 16:40:45 +0000</pubDate>
      <link>https://dev.to/wlcreate/nevertheless-waverley-coded-53g7</link>
      <guid>https://dev.to/wlcreate/nevertheless-waverley-coded-53g7</guid>
      <description>&lt;p&gt;A year ago this month I decided to transition into tech. My journey so far has been wonderful and eye-opening, and I’ve met so many amazing and inspiring people who I otherwise never would have crossed paths with. Thinking about the Waverley of March 2019 who decided to take the plunge and the Waverley of today, I can’t help but reflect on how much I’ve grown in this time and be grateful to all of the people who have supported me.&lt;/p&gt;

&lt;p&gt;In honor of this anniversary and to thank everyone who have come before me and who will come after, I leave this piece of Waverley of March 2021 here 💞&lt;/p&gt;



&lt;h2&gt;
  
  
  Two important lessons:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;“Plus Est En Vous”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A huge reason why I didn’t make the transition to tech earlier was because I listened to what other people said and limited myself to their expectations. I wasn’t afraid of exceeding their expectations (which in hindsight isn’t a reliable goal), but I was afraid of exceeding expectations that they didn’t want me to exceed. Constantly being told I’m “too intelligent”, “thinking too much”, “not pushing myself enough”, among other things. As a result, anything that would make me “more intelligent” I ignored, instead pushing myself to work harder to be seen more as what they wanted. Every time I doubted or questioned what I was doing, I would think about what they said I should want and be. I internalized them, thinking they were what I wanted and what was needed of me. Reality is, I put myself in a box. Therefore, this past year taught me to live based on my expectations of myself and grow by challenging those expectations, not live based on expectations other people put on me. To have faith and believe in myself.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collaboration = Communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ultimately I’ve learned that the difference between working with others and true collaboration is that one can’t happen without communication. While there are details in between, when I think of collaboration I think of a positive, supportive environment where teammates are constantly learning (from each other and through the work itself), communicating with empathy, open to discussion, and are excited to work. That means when you don’t know or understand something, say so. But, that also means that there needs to be space for you to say it comfortably and not feel ashamed for it. For the person who hears the “I don’t know” to empathize and try to help in whatever capacity they can. On the other hand, when you see someone on your team struggling but aren’t saying anything, it means to offer your help anyway or send them a resource. If someone is dealing with everyday life issues, it means so much to know that your team has your back and remind you that you’re not just a worker. &lt;/p&gt;

&lt;p&gt;Especially during COVID where we’re all working from home and meeting virtually, it means being able to be able to communicate this not only verbally but written as well. Remember that it’s not just what you’re saying, but how you’re saying it. Even though with words on a screen you can’t hear how the words are being said, being thoughtful with your word choice, emojis, gifs, and reactions. If despite all that your words are still misinterpreted by the other party, you can still communicate that it wasn’t your intention, elaborate, and say what you mean again. Other times, you might say something that only later on, you realize is wrong. Have the courage and humility to apologize, and take the next step to do your best to educate yourself and learn from the experience. &lt;/p&gt;

&lt;p&gt;I could go on more, but in my opinion that’s why collaboration can’t happen without good, effective communication. If you can’t hear/talk to your teammates, how can you really collaborate?&lt;/p&gt;



&lt;h2&gt;
  
  
  Two things I want to work on:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Setting boundaries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This ties into advocating for myself and reminding myself to remember what I really want. Now that I’m not limiting myself based on other people, I’m embracing the freedom that brings. There’s so much I want to learn and so many wonderful opportunities out there to experience. However, even though there’s so much that I want to do, I also need to remember what my dream is and have that ground me. That starts with setting boundaries on how much I let everything take up space time-wise, emotionally and mentally. Reminding myself to be honest that I can’t do everything, to be grateful for the opportunities that come my way but I don’t always have to say “yes” to them, and to communicate my boundaries to the people they affect. I’m still working on this, so I can’t give much insight or advice, but I hope that by next year I’ll have something new to share. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cultivating and deepening positive, supportive relationships with people who push me to be my best self&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Growing up I was often the “shy quiet” kid. I was happy making other people smile or laugh, but I didn’t need to be surrounded by people. Often you’d find me lost in my own little world. But when I’d come back to earth, I’d be alone. I didn’t expect anyone to wait for me, and all I would have to do is see the person the next day or call/text them and everything would be fine. But honestly, after a while it became harder and harder to do because life goes on. &lt;/p&gt;

&lt;p&gt;I like to believe the people I’m with are always looking out for my best interest, but that’s sadly not always the case. I’ve had a lot of relationships with people who kept me in the same place. When I was upset they would comfort me, which I appreciated. But sometimes I was wrong, and when I would be right I wouldn’t take any further action. Not realizing this pattern for years I kept making the same decisions, the same mistakes and the cycle would repeat. These experiences made me realize that I want to be in relationships that push me to be my best self, and I want to reciprocate and do this for others as well. &lt;/p&gt;

&lt;p&gt;Even though I have never met my cohort from Flatiron School or my team from The Collab Lab in person, knowing that I have a community to turn to means a lot. To know that I have people I can turn to for advice, to celebrate wins, to share worries, and are willing to tell me the honest hard truth when I need it, is the world to me. I always appreciate when they reach out to check in on me when I’ve been in my little world for too long. On the flip side, contributing to these communities is such an amazing feeling as well; realizing that by sharing my thoughts and knowledge I can help people. While I continue to navigate what my introverted tendencies mean for me to socialize and relationships in general, I hope that in the next year I will contribute to more communities, make new friends, and stay in touch with those dear to me.&lt;/p&gt;




&lt;p&gt;Someone told me right after I graduated college that your twenties are when you finally start to learn about yourself after going through years of school, education, and basically being told what to do your whole life. I replied with a polite smile and agreed, but I naively thought that didn’t apply to me. I mean, what does it even mean to “learn about yourself”? I’ve been myself my whole life, how could I possibly not know who I am? In the months leading to my graduation, I already figured out my plan and knew what I wanted. I knew who I was. &lt;/p&gt;

&lt;p&gt;How wrong my fresh out of college self was; it took a career transition less than two years after I graduated to realize it. I’m grateful for this new journey in more ways than one, and I’m excited for what’s ahead and to learn more about who I am. Thank you for reading this, for those who have supported me, and for everyone who comes after; I hope that this piece of my story helps you wherever you are in your journey. &lt;/p&gt;

&lt;p&gt;Special shoutout to The Code Benders, The Collab Lab, and the larger Flatiron School community. Super special shoutout to Sylwia Vargas for encouraging me to share my story!&lt;/p&gt;




&lt;p&gt;Cover image: Photo by &lt;a href="https://unsplash.com/@giabyte?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Gia Oris&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/start?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>wecoded</category>
      <category>codenewbie</category>
      <category>womenintech</category>
      <category>choosetochallenge</category>
    </item>
  </channel>
</rss>
