<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Devcore</title>
    <description>The latest articles on Forem by Devcore (@devcore).</description>
    <link>https://forem.com/devcore</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%2Forganization%2Fprofile_image%2F4403%2F6694385c-049c-4f25-ba91-3ca664f5b374.png</url>
      <title>Forem: Devcore</title>
      <link>https://forem.com/devcore</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/devcore"/>
    <language>en</language>
    <item>
      <title>How can you remove a specific item from an array?</title>
      <dc:creator>Leo Cuéllar</dc:creator>
      <pubDate>Fri, 30 Jul 2021 21:37:37 +0000</pubDate>
      <link>https://forem.com/devcore/how-can-you-remove-a-specific-item-from-an-array-3ah</link>
      <guid>https://forem.com/devcore/how-can-you-remove-a-specific-item-from-an-array-3ah</guid>
      <description>&lt;h2&gt;
  
  
  Quick summary
&lt;/h2&gt;

&lt;p&gt;This tutorial will cover the &lt;code&gt;.splice()&lt;/code&gt; and &lt;code&gt;.indexOf()&lt;/code&gt; array methods and their application in removing a specific item from an array in JavaScript.&lt;/p&gt;




&lt;p&gt;It's the year 3025. Humanity has finally got out of the solar system and is now exploring new frontiers. Unfortunately, JavaScript still has not got a .remove() method.&lt;/p&gt;

&lt;p&gt;Let's start with a simple array. The contents or data types inside of it don't matter.&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;numbers&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;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, suppose we need to remove the number 2. Let's see how to do that:&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1 - Remove one occurrence
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;index&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;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&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;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&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;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 5, 5, 3, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, as you can see, we have declared a new function called &lt;code&gt;removeItem()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This function takes two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The array that we want to modify&lt;/li&gt;
&lt;li&gt;The value we have to remove&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, we want to see if that value exists in the array. We do that with the &lt;code&gt;.indexOf()&lt;/code&gt; array method. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.indexOf()&lt;/code&gt; method applied to any array will return the index of a given value if it exists in the array. If it doesn't, it will return &lt;code&gt;-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After that, we create a condition that checks that the index value does exists.&lt;/p&gt;

&lt;p&gt;If it exists, we apply the &lt;code&gt;.splice()&lt;/code&gt; method to remove it from the array, and once everything is done, we return the resultant array.&lt;/p&gt;

&lt;h2&gt;
  
  
  .splice()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;.splice()&lt;/code&gt; is a powerful method that allows us to change the contents of an array by removing and adding elements to it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Remember. It does so by mutating the original array.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this case, we passed two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The start index from where we will start making changes&lt;/li&gt;
&lt;li&gt;The delete count&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That means that in our use of the &lt;code&gt;.splice()&lt;/code&gt; method, we are just telling it to remove the element on the given index.&lt;/p&gt;

&lt;p&gt;What if we want to remove all the matching items?&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2 - Remove all occurrences V1
&lt;/h2&gt;

&lt;p&gt;Let's see how to do that:&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;removeAllItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;val&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="nx"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&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;removeAllItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 3] notice the missing 2. Remember that .splice() mutates the array and we already removed the number 2.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, here we are applying &lt;code&gt;.splice()&lt;/code&gt; in the same way. The only difference is that we are iterating over the array to work over all the elements and see if they are occurrences of the item we want to delete.&lt;/p&gt;

&lt;p&gt;Now, notice that we are not using a &lt;code&gt;for&lt;/code&gt; loop. The only reason for that is that we don't want to increment our index if we remove it from the array. That would make our function skip some elements, and we don't want that. &lt;/p&gt;

&lt;p&gt;Let me show you another way of removing all the occurrences of an item in an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 3 - Remove all occurrences V2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers2&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeAllItems2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&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="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;val&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;newNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;removeAllItems2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers2&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 2, 3, 2, 4]. No mutation&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 3, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is my personal favorite. We are using the &lt;code&gt;.filter()&lt;/code&gt; method to return a new array.&lt;/p&gt;

&lt;p&gt;The filter method iterates over all the elements in an array and, in this case, if the element is not equal to the value we want to remove, it will add it to a new array, and after it finishes, it will return the new array.&lt;/p&gt;

&lt;p&gt;This is an excellent option if you don't want to mutate the original array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;So there you go. We just saw three methods to remove a specific item for an array, either to remove one occurrence or all of them. I hope this helps you on your development journey. Thanks for reading!&lt;/p&gt;




&lt;p&gt;This article was first published on &lt;a href="https://devcore.io/en/javascript/how-can-you-remove-a-specific-item-from-an-array/"&gt;devcore.io&lt;/a&gt;. go check it out!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>HOCs vs Hooks. What to use and why?</title>
      <dc:creator>Leo Cuéllar</dc:creator>
      <pubDate>Fri, 16 Jul 2021 19:29:21 +0000</pubDate>
      <link>https://forem.com/devcore/hocs-vs-hooks-what-to-use-and-why-nnb</link>
      <guid>https://forem.com/devcore/hocs-vs-hooks-what-to-use-and-why-nnb</guid>
      <description>&lt;h2&gt;
  
  
  Quick summary
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we will see how to refactor a higher order component into a custom hook and the main benefits and caveats of using one over the other.&lt;/p&gt;




&lt;p&gt;Recently I published a little tutorial about &lt;a href="https://devcore.io/en/react/what-is-a-higher-order-component/"&gt;Higher Order Components&lt;/a&gt;. I consider that an important topic since it's widely asked in job interviews and is still used in many applications.&lt;/p&gt;

&lt;p&gt;A fellow developer read the article and kindly suggested I talk about the pros and cons of using them and how hooks replace the need for HOCs, so here we go!&lt;/p&gt;

&lt;h2&gt;
  
  
  A little talk about hooks
&lt;/h2&gt;

&lt;p&gt;On February 16, 2019, React 16.8 was released to the public. &lt;a href="https://es.reactjs.org/blog/2019/02/06/react-v16.8.0.html"&gt;The one with hooks&lt;/a&gt; was the main description given to that release.&lt;/p&gt;

&lt;p&gt;Since then, an extensive discussion has arisen about whether or not hooks will replace common React patterns.&lt;/p&gt;

&lt;p&gt;It was a relief for many since we already preferred to use functional components over class components. It allowed for the development of easier to read, easier to write, and easier to test components, and many of us switched to that pattern in no time. &lt;/p&gt;

&lt;p&gt;Since then, I haven't touched a class component, and honestly, I'm very glad.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are higher order components?
&lt;/h2&gt;

&lt;p&gt;A HOC is a component that takes one or more components as props and returns new components.&lt;/p&gt;

&lt;p&gt;HOCs can easily allow you to reduce the amount of duplicate logic on your application. Let me show you an example:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/cool-dawn-39m12"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you don't understand what's happening there I really encourage you to check my tutorial about &lt;a href="https://devcore.io/en/react/what-is-a-higher-order-component/"&gt;Higher Order Components&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Anyway, let's break out that code to understand it.&lt;/p&gt;

&lt;p&gt;You can see that I created a HOC called &lt;code&gt;withData&lt;/code&gt;, which accepts a child component. &lt;/p&gt;

&lt;p&gt;When the HOC mounts, it fetches data from the pokemon API and returns the child component with an appended prop containing the fetching results.&lt;/p&gt;

&lt;p&gt;Then we have the &lt;code&gt;ListResults&lt;/code&gt; component, which takes a &lt;code&gt;results&lt;/code&gt; prop and renders an unordered list with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactoring into a custom hook
&lt;/h2&gt;

&lt;p&gt;Look at this code:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/fast-framework-69pct"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So here, you can see that we created a custom hook called &lt;code&gt;useData&lt;/code&gt;, which fetches from the API and returns the results.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;ListResults&lt;/code&gt; component can then use it to get some data and render it in a list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;I like to keep my articles simple to help new developers understand concepts like this. This could involve deeper topics, patterns, and principles that we can cover if you want, but for the sake of simplicity, allow me to conclude.&lt;/p&gt;

&lt;p&gt;HOC is just a pattern, which means that it's not written in stone that you have to use class components to write them.&lt;/p&gt;

&lt;p&gt;Hooks give us the ability to handle state and side effects in our functional components and our functional-based HOCs, but if you're like me and prefer to use functional components and your HOC uses hooks to work, why not create a custom hook instead?&lt;/p&gt;

&lt;p&gt;This will not be the case every time, but hopefully, this tutorial gives more clarity when deciding if you need a HOC or a custom hook.&lt;/p&gt;




&lt;p&gt;This article was first published on &lt;a href="https://devcore.io"&gt;devcore.io&lt;/a&gt;. go check it out!&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using String.Replace() with RegEx</title>
      <dc:creator>Leo Cuéllar</dc:creator>
      <pubDate>Thu, 08 Jul 2021 23:10:11 +0000</pubDate>
      <link>https://forem.com/devcore/using-string-replace-with-regex-pi2</link>
      <guid>https://forem.com/devcore/using-string-replace-with-regex-pi2</guid>
      <description>&lt;p&gt;The &lt;code&gt;replace()&lt;/code&gt; method is often used in JavaScript to replace parts of a string. You may have been using it as follows:&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;I like cats&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newStr&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="nx"&gt;replace&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;We&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//We like cats&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the method basically accepts two parameters: the string to be replaced and the replacement.&lt;/p&gt;

&lt;p&gt;In reality, that's actually a little more complicated. The first parameter accepts two options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A string to be replaced, as mentioned above&lt;/li&gt;
&lt;li&gt;A RegEx object or literal. The match will be replaced with the second parameter&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The second parameter also accepts two options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The replacement string&lt;/li&gt;
&lt;li&gt;A function that will be invoked to create the replacement string&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In most real-world cases you will be looking for patterns instead of fixed strings, so using a RegEx as the first parameter will be the solution to our problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Regex with replace()
&lt;/h2&gt;

&lt;p&gt;Let's start with an example and then explain how it works:&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;reg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/ &lt;/span&gt;&lt;span class="se"&gt;\((&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;?)\)&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&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;The quick brown (some unwanted string) fox jumps over the (another unwanted string) lazy dog.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newStr&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="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//The quick brown fox jumps over the (another unwanted string) lazy dog.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what's happening? Well, in our case, anything between parenthesis (including the parenthesis) will match with our RegEx, so the &lt;code&gt;replace()&lt;/code&gt; method will replace that with our replacement string &lt;code&gt;''&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There's still one flaw with our code. It's just replacing the first match, so in the case that we want to replace everything that matches our RegEx, we would just have to use the &lt;code&gt;g&lt;/code&gt; flag in our RegEx:&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;reg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/ &lt;/span&gt;&lt;span class="se"&gt;\((&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;?)\)&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt; &lt;span class="c1"&gt;//&amp;lt;-- notice the flag&lt;/span&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;The quick brown (some unwanted string) fox jumps over the (another unwanted string) lazy dog.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newStr&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="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newStr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//The quick brown fox jumps over the lazy dog.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;RegEx is a POWERFUL tool and it makes replacing strings in JavaScript a more effective task, to say the least.&lt;/p&gt;




&lt;p&gt;This article was first published on &lt;a href="https://devcore.io/en/javascript/using-string-replace-with-regex/"&gt;devcore.io&lt;/a&gt;. go check it out!&lt;/p&gt;

</description>
      <category>replace</category>
      <category>javascript</category>
      <category>strings</category>
    </item>
    <item>
      <title>What is the difference between let and var?</title>
      <dc:creator>Leo Cuéllar</dc:creator>
      <pubDate>Thu, 08 Jul 2021 23:06:20 +0000</pubDate>
      <link>https://forem.com/devcore/what-is-the-difference-between-let-and-var-40aj</link>
      <guid>https://forem.com/devcore/what-is-the-difference-between-let-and-var-40aj</guid>
      <description>&lt;p&gt;To understand the answer to this question, it's better if we first understand the "scope" in JavaScript.&lt;/p&gt;

&lt;p&gt;The scope can be defined as "The current context of execution," meaning that when a script is running, there is only so much stuff that can be referenced or used, based on what part of our code is running at any given time.&lt;/p&gt;

&lt;p&gt;If a variable is not in the "current scope," then it will be unavailable for use.&lt;/p&gt;

&lt;p&gt;For example, in general terms, if you declare a variable inside a function, then that variable will be unavailable outside that function. In fact, if you try to do it, it will generate a nasty reference error, as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is declared inside myFunction&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;myFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;//this is declared inside myFunction&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//error: Uncaught ReferenceError: x is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  var
&lt;/h2&gt;

&lt;p&gt;ES6 introduced a new type of scope called "block scope," which is the scope of if or for statements. Basically, anything between brackets is a block.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; variables exist since way before block scope was introduced, so they have no block scope. var declarations are either function-scoped or global-scoped, which were the only two scope types available before ES6.&lt;/p&gt;

&lt;p&gt;This means that &lt;code&gt;var&lt;/code&gt; declarations will see through blocks and take the scope of the parent element. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is inside a block&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//this is inside a block (is it?)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the case above, the variable turned into a global variable since we used &lt;code&gt;var&lt;/code&gt; to declare it, and the block itself wasn't inside a function.&lt;/p&gt;

&lt;p&gt;Take this other example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myOtherFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is inside a block&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;myOtherFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;//this is inside a block&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//error: Uncaught ReferenceError: x is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, as you can see, the variable saw through the block, as expected, but this time it took the scope of the wrapping function. When we tried to reference the variable outside the function, it gave us another error.&lt;/p&gt;

&lt;p&gt;So that's basically how &lt;code&gt;var&lt;/code&gt; works. Let's see what's the difference with let.&lt;/p&gt;

&lt;h2&gt;
  
  
  let
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; was introduced in ES6 along with &lt;code&gt;const&lt;/code&gt; as a new way to declare variables.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; works similarly to &lt;code&gt;var&lt;/code&gt;, but this one is block-scoped.&lt;/p&gt;

&lt;p&gt;Let's see it in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is inside a block&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;//this is inside a block (now it is)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//error: Uncaught ReferenceError: x is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty straightforward, isn't it? This time the &lt;code&gt;let&lt;/code&gt; declaration helped us keep it inside the block.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;In general, you should avoid the declaration of global variables, and using &lt;code&gt;var&lt;/code&gt; can lead to that without you even noticing.&lt;/p&gt;

&lt;p&gt;Today you will find that &lt;code&gt;let&lt;/code&gt; is used almost everywhere, and it does have its benefits 👌. It can particularly help you avoid bugs in your applications caused by the use of global variables.&lt;/p&gt;




&lt;p&gt;This article was first published on &lt;a href="https://devcore.io/en/javascript/what-is-the-difference-between-let-and-var/"&gt;devcore.io&lt;/a&gt;. go check it out!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>let</category>
      <category>var</category>
      <category>es6</category>
    </item>
    <item>
      <title>How to use Lodash debounce method?</title>
      <dc:creator>Leo Cuéllar</dc:creator>
      <pubDate>Thu, 08 Jul 2021 23:01:29 +0000</pubDate>
      <link>https://forem.com/devcore/how-to-use-lodash-debounce-method-fkc</link>
      <guid>https://forem.com/devcore/how-to-use-lodash-debounce-method-fkc</guid>
      <description>&lt;p&gt;Recently I was applying for a react developer position at some company. In the process, I had to solve three tasks, which, surprisingly, were not so hard as people implied in some reviews I read.&lt;/p&gt;

&lt;p&gt;For a React dev position, in almost every interview you may take for any company, you will be asked to fetch data from an API and use it somehow, which was no exception on this occasion.&lt;/p&gt;

&lt;p&gt;The catch for me was that I was required to use the &lt;code&gt;debounce()&lt;/code&gt; method from the lodash library, and to be honest, I’ve heard of the library, but I’ve never used it before.&lt;/p&gt;

&lt;p&gt;Let me tell you what I learned from this interview.&lt;/p&gt;

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

&lt;p&gt;So, basically, lodash is a utility library that simplifies common programming tasks and gives us more capabilities when executing them.&lt;/p&gt;

&lt;p&gt;I would love to give you a lot of examples but let me save that for future articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a debounced function?
&lt;/h2&gt;

&lt;p&gt;A debounced function is a function that delays its execution a certain amount of milliseconds after the last call was received.&lt;/p&gt;

&lt;p&gt;Let’s separate the three types of functions involved in this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your normal function: this is the function that you want to debounce&lt;/li&gt;
&lt;li&gt;The debounced function: your same function but debounced, which means that it will work as the definition above says.&lt;/li&gt;
&lt;li&gt;The debounce function: a function that will receive two parameters, a function to debounce and some time in milliseconds. This function will return the debounced function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lodash debounce() method is that debounce function mentioned in point 3.&lt;/p&gt;

&lt;p&gt;Let’s see it with a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logHi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi&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;debouncedLogHi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;logHi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;debouncedLogHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;debouncedLogHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;debouncedLogHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;//console: Hi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the timer will start running from the last call of the &lt;code&gt;debouncedLogHi()&lt;/code&gt; function. After 1500 milliseconds, the function will run.&lt;/p&gt;

&lt;p&gt;Passing parameters to a debounced function&lt;br&gt;
You can also pass parameters to a debounced function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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;debouncedLogMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;logMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;debouncedLogMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;debouncedLogMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;debouncedLogMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;third message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//console: third message&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, once the timer ends after the last call to the debounced function, the invoked function will be the last.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;So yeah, lodash is awesome, and I'll definitely write more about it in the future. Subscribe to my newsletter if you want to be posted about future posts.&lt;/p&gt;




&lt;p&gt;This article was first published on &lt;a href="https://devcore.io/en/javascript/how-to-use-lodash-debounce-method/"&gt;devcore.io&lt;/a&gt;. go check it out!&lt;/p&gt;

</description>
      <category>debounce</category>
      <category>lodash</category>
      <category>javascript</category>
    </item>
    <item>
      <title>What is a Higher Order Component?</title>
      <dc:creator>Leo Cuéllar</dc:creator>
      <pubDate>Thu, 08 Jul 2021 22:57:45 +0000</pubDate>
      <link>https://forem.com/devcore/what-is-a-higher-order-component-hhe</link>
      <guid>https://forem.com/devcore/what-is-a-higher-order-component-hhe</guid>
      <description>&lt;h2&gt;
  
  
  Quick summary
&lt;/h2&gt;

&lt;p&gt;In this tutorial we will talk about Higher-Order Components (HOC), a widely used react concept and a topic that is commonly discussed in front-end developer interviews. We will discuss what they are and how to write them.&lt;/p&gt;

&lt;p&gt;You may have heard about the don’t-repeat-yourself (DRY) programming principle, where we look to reduce the amount of duplicate logic on our applications.&lt;/p&gt;

&lt;p&gt;Well, this principle has evolved as a pattern that you can see on higher-order functions on JavaScript, or as Higher-Order Components in React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Higher-Order Functions
&lt;/h2&gt;

&lt;p&gt;Let’s first understand what are higher-order functions, since I think you may have been using them a lot without knowing their higher-order nature.&lt;/p&gt;

&lt;p&gt;A higher-order function is a function that takes a function as a parameter, returns another function, or both.&lt;/p&gt;

&lt;p&gt;Let’s take the &lt;code&gt;map()&lt;/code&gt; array method as an example. This method takes a function as a parameter which means that it is a higher-order function, but how are we recycling logic with this method? well, the &lt;code&gt;map()&lt;/code&gt; method does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;iterates over every element of an array&lt;/li&gt;
&lt;li&gt;applies the passed function to every element&lt;/li&gt;
&lt;li&gt;the returned values will be added to a new array&lt;/li&gt;
&lt;li&gt;returns the resulting array&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;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;const&lt;/span&gt; &lt;span class="nx"&gt;mappedArr&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;map&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;=&amp;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;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mappedArr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//[2, 4, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as you can see, we are recycling that logic over and over again, every time we call the &lt;code&gt;map()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Let’s see another example, this time building a higher-order function that returns another function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num2&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;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addFive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// addFive = (num2) =&amp;gt; 5 + num2&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addFive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// 5 + 12 = 17&lt;/span&gt;
&lt;span class="c1"&gt;// 17&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in the last example our ‘add’ function serves the purpose of building ‘adder’ functions. You pass a number as a parameter and it will return a function that adds that number to any other number.&lt;/p&gt;

&lt;p&gt;Passing 5 as a parameter will return this function &lt;code&gt;(num2) =&amp;gt; 5 + num2&lt;/code&gt; so we basically used our higher-order function to build another function that adds 5 to any number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Higher-order components
&lt;/h2&gt;

&lt;p&gt;Now that you better understand the concept, let's define a higher-order component. A HOC is a component that takes one or more components as props and returns new components.&lt;/p&gt;

&lt;p&gt;It’s important to mention that HOC’s don’t modify the components passed, they just return new components.&lt;/p&gt;

&lt;p&gt;Let’s see a very basic implementation of a HOC:&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;withComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;someComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Hi&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sameComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;withComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someComponent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we are passing a component to our HOC and then returning it again. In reality, you would implement some logic, pass some props, style it, etc…&lt;/p&gt;

&lt;p&gt;Let's see another example:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/naughty-http-2mlo9"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In this example you can see how I created a simple component that return some text. Then I created a HOC that accepts any component and then returns it inside a span tag that has some style. Im sharing this with you through codesandbox so you can see the result. Try and experiment with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Hope this gave you at least a basic understanding of HOC’s and how to write them. It’s a widely used concept in React that you will encounter in a lot of libraries such as Redux, for example.&lt;/p&gt;




&lt;p&gt;This article was first published on &lt;a href="https://devcore.io/en/react/what-is-a-higher-order-component/"&gt;devcore.io&lt;/a&gt;. go check it out!&lt;/p&gt;

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