<?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: Rachel Humes</title>
    <description>The latest articles on Forem by Rachel Humes (@racheljoyh).</description>
    <link>https://forem.com/racheljoyh</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%2F973741%2Fb13e0598-46ec-4f6e-aea9-2604e196d737.jpg</url>
      <title>Forem: Rachel Humes</title>
      <link>https://forem.com/racheljoyh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/racheljoyh"/>
    <language>en</language>
    <item>
      <title>React with Redux: A Beginner's Guide</title>
      <dc:creator>Rachel Humes</dc:creator>
      <pubDate>Sat, 28 Jan 2023 21:46:31 +0000</pubDate>
      <link>https://forem.com/racheljoyh/react-with-redux-a-beginners-guide-1pg</link>
      <guid>https://forem.com/racheljoyh/react-with-redux-a-beginners-guide-1pg</guid>
      <description>&lt;p&gt;Another phase at Flatiron is in the books and I finished my first full stack application with React/Rails libraries. It was satisfying to finally put together all the pieces that I have been acquiring at each phase of my bootcamp and create something that was super fun to make!&lt;/p&gt;

&lt;p&gt;Along the way, I found that working with state in a more complex application was a little more tricky to manage. This led me to start researching better ways to manage state. &lt;/p&gt;




&lt;h2&gt;
  
  
  Redux
&lt;/h2&gt;

&lt;p&gt;One of the tried and true ways to manage state is to use Redux. It has been around for quite a few years now and was created to manage state in a more centralized manner and structured so that it is easier to debug. In conjunction with the Redux Toolkit (contains packages and functions that are essential to building a Redux app), it guides you to write code that is predictable, testable and helps ensure your web app will behave correctly. Since it has been around the block for awhile, it also has a robust ecosystem with many different addons for your needs.&lt;/p&gt;

&lt;p&gt;However, if you are working with a smaller project, Redux might be more trouble than it is worth since there is a lot of boilerplate code and restrictions. You should probably only implement it when: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You have a lot of state that is needed in numerous components in your app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The app state is updated frequently over time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The logic to update a certain state is complex.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The app has a medium to large codebase and will be worked on by many people.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is important to think about the complexity of your app before deciding whether to use it or not. Usually smaller projects can get away with useContext or simply just lifting state. &lt;/p&gt;




&lt;h2&gt;
  
  
  Redux's key players: The store, reducer and dispatcher
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;store&lt;/strong&gt; essentially houses your global state. You should not directly modify state held in here and instead create an "action"/object that describes the action you want to invoke. These actions are held in what is called a &lt;strong&gt;reducer&lt;/strong&gt;. They do the behind the scenes work and calculate the new state based on the previous state and action. The &lt;strong&gt;dispatcher&lt;/strong&gt; is what connects UI to the &lt;strong&gt;store&lt;/strong&gt; and successively the &lt;strong&gt;reducer&lt;/strong&gt;. When the event handler (i.e onClick or onSubmit) is invoked, the &lt;strong&gt;dispatcher&lt;/strong&gt; takes the user action and runs it to the &lt;strong&gt;store ** which then runs through the **reducer&lt;/strong&gt; to determine the correct response action and returns the updated state. &lt;/p&gt;

&lt;p&gt;Below is an illustration for better visualization of the data flow but we will also go through a simple example of how it works together:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F06q51aiuamntu14e17ij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F06q51aiuamntu14e17ij.png" alt="Image description" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Creating a simple counter with Redux&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After installing 'react-redux' and '@reduxjs/toolkit' into our application we need to create a &lt;strong&gt;slice&lt;/strong&gt;. It is a function that allows us to separate our reducer and action logic by the different features within our app.&lt;br&gt;
&lt;/p&gt;

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

import { createSlice } from "@reduxjs/toolkit";

export const slice = createSlice({
  name: "counter",
  initialState: {
    value: 0,
  },
  reducers: {
    increment: (state) =&amp;gt; {
      state.value += 1;
    },
    decrement: (state) =&amp;gt; {
      state.value -= 1;
    },
  },
});

export const actions = slice.actions;
export const reducer = slice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the slice function, we set the name or type as the feature we are trying to create. Then we set the initial state of our counter feature to 0 and create the reducer objects that will return the actions we want to invoke. Make sure to name the actions something that can be easily interpreted by others that will be looking at your code (i.e increment, decrement). Export the actions and reducer. &lt;/p&gt;

&lt;p&gt;Next, in our store file we want to configure our store and import the &lt;strong&gt;configureStore&lt;/strong&gt; hook from our toolkit as well as our counter reducer we just created&lt;br&gt;
&lt;/p&gt;

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

import { configureStore } from "@reduxjs/toolkit";
import { reducer as counterReducer } from "./slices/counter";

export default configureStore({
  reducer: {
    counter: counterReducer,
  },
});

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

&lt;/div&gt;



&lt;p&gt;Finally, we need our dispatcher to connect us to the UI. In our App component we will import useSelector and useDispatch hook from 'react-redux'. We take the imported actions from our reducer and set them in our dispatch hook to be invoked upon the onClick event. Finally, the useSelector() hook is important as it takes in the function argument that targets the part of the state we want (the counter) and will allow us to "select" or extract the value for it to be displayed to the user.&lt;br&gt;
&lt;/p&gt;

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

import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { actions } from "./slices/counter";

function App() {
  const count = useSelector((state) =&amp;gt; state.counter.value);
  const dispatch = useDispatch();

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;Counter&amp;lt;/h1&amp;gt;
        &amp;lt;p&amp;gt;{count}&amp;lt;/p&amp;gt;
        &amp;lt;button onClick={() =&amp;gt; dispatch(actions.increment())}&amp;gt;+&amp;lt;/button&amp;gt;
        &amp;lt;button onClick={() =&amp;gt; dispatch(actions.decrement())}&amp;gt;-&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;That does it for the basics of Redux! Try it out for yourself and if you have a large scale project on the horizon with a lot of collaborators, Redux might be the right choice for you!&lt;/p&gt;

&lt;p&gt;Happy coding! &lt;/p&gt;

</description>
      <category>dart</category>
      <category>flutter</category>
      <category>designpatterns</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>The Power of Community: Ruby and Grieving</title>
      <dc:creator>Rachel Humes</dc:creator>
      <pubDate>Mon, 09 Jan 2023 21:19:09 +0000</pubDate>
      <link>https://forem.com/racheljoyh/the-power-of-community-ruby-and-grieving-3f1a</link>
      <guid>https://forem.com/racheljoyh/the-power-of-community-ruby-and-grieving-3f1a</guid>
      <description>&lt;p&gt;Today, I finished up Phase 3 at Flatiron School where we learned another language, Ruby, and how to connect the backend with the frontend via Sinatra. I was able to pass the code challenge and knock out a project that I am pretty proud of, thankfully. Thankfully, because during this phase, it wasn't the content that proved to be the most difficult as I initially expected but it was the unexpected death of my father. &lt;/p&gt;

&lt;p&gt;When I got the phone call on that Sunday morning, I felt gutted. I had a code challenge that Thursday but my motivation to get anything done immediately vanished. Writing a single line of code seemed impossible. I told my cohort the news and that I probably wouldn't be at school that week and I was contemplating whether I could even continue this program.&lt;/p&gt;

&lt;p&gt;However, it was then I realized how special my community is at Flatiron School. The support I received from them while grieving is something that I will forever be grateful for and after a couple of days being at home and unbelievably sad, I realized I needed to be at school and around their energy to push me to get to the finish line of this phase. So for the rest of the week we all had many study sessions together to make sure that not just me but &lt;strong&gt;everyone&lt;/strong&gt; made it to the next phase together and I am happy to say that we did. &lt;/p&gt;

&lt;p&gt;Although, it's going to take awhile for me to feel okay again, I have a great community around me and most importantly, I know my father would want me to continue to pursue my dreams and would be proud of the progress I have made. So this one is for you, dad. &lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>A Guide to useEffect Cleanup</title>
      <dc:creator>Rachel Humes</dc:creator>
      <pubDate>Sat, 10 Dec 2022 02:26:57 +0000</pubDate>
      <link>https://forem.com/racheljoyh/a-guide-to-useeffect-cleanup-27dj</link>
      <guid>https://forem.com/racheljoyh/a-guide-to-useeffect-cleanup-27dj</guid>
      <description>&lt;p&gt;Currently, I am in phase two at Flatiron School and the past three weeks have been all things React. I have learned some great concepts that have definitely helped me become a better developer and one of those concepts is cleaning up the useEffect hook. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why cleaning up is important
&lt;/h2&gt;

&lt;p&gt;Let's say you have a function that continuously runs a timer component in the background&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Timer() {
  const [time, setTime] = useState(new Date());

  useEffect(() =&amp;gt; {
    setInterval(() =&amp;gt; {
      setTime(new Date());
    }, 1000);
  }, []);

  return &amp;lt;div&amp;gt;{time.toString()}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this component first renders, the useEffect hook will initialize and create an interval that runs every 1 second and set the time. We could then set state and create a button that will hide and show the timer. We might imagine that this also means that the timer will stop running once it is hidden.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [showTimer, setShowTimer] = useState(true);

  return (
    &amp;lt;div&amp;gt;
      {showTimer ? &amp;lt;Timer /&amp;gt; : null}
      &amp;lt;button onClick={() =&amp;gt; setShowTimer(!showTimer)}&amp;gt;Toggle Clock&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this is not the case. Once you toggle the button, you will receive an error like the one below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffswg39rmmpz8mpcas3qc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffswg39rmmpz8mpcas3qc.png" alt="Image description" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This error indicates that even though the clock is hidden, it is still running continuously in the background and re-rendering the component every second. This could potentially cause unwanted behaviors and performance issues. &lt;/p&gt;

&lt;p&gt;The solution to this error would be to create a clean up function that runs once the component is unmounted. With our example it would look a little something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Timer() {
  const [time, setTime] = useState(new Date());

  useEffect(() =&amp;gt; {
    const timerID = setInterval(() =&amp;gt; {
      setTime(new Date());
    }, 1000);

    // returning a cleanup function
    return function cleanup() {
      clearInterval(timerID);
    };
  }, []);

  return &amp;lt;div&amp;gt;{time.toString()}&amp;lt;/div&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now once the timer is toggled off, the clean up function will run and clear the interval we set. &lt;/p&gt;

&lt;h2&gt;
  
  
  The cleanup function lifecycle
&lt;/h2&gt;

&lt;p&gt;The lifecycle of our timer component without the clean up would look like this &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fbnw7dqxs4w1ffydxlare.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbnw7dqxs4w1ffydxlare.png" alt="Image description" width="800" height="37"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with the cleanup function added to our component's lifecycle would now look like this once the component is unmounted upon the click of the toggle off button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fmbe034s565s6yql9nlm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmbe034s565s6yql9nlm1.png" alt="Image description" width="800" height="41"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In summary, clean up functions are a great way to optimize the performance of your web app and to avoid unwanted behaviors. However, it is not always necessary to use clean up functions with your useEffect hooks. Scenarios like running timers, clocks or a subscription to a WebSocket connection are a few use cases where a clean up function is important. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ai</category>
      <category>googlecloud</category>
    </item>
    <item>
      <title>Using Destructuring in JS to Write Cleaner Code</title>
      <dc:creator>Rachel Humes</dc:creator>
      <pubDate>Thu, 17 Nov 2022 22:42:39 +0000</pubDate>
      <link>https://forem.com/racheljoyh/using-destructuring-in-js-to-write-cleaner-code-2601</link>
      <guid>https://forem.com/racheljoyh/using-destructuring-in-js-to-write-cleaner-code-2601</guid>
      <description>&lt;p&gt;As a beginner in vanilla JavaScript, I still struggle with writing clean, concise and easy to read code (otherwise known as DRY code). So, I get excited when I learn a method that makes my code look more ✨elegant✨. Today, we will go over how destructuring does just that. &lt;/p&gt;




&lt;h2&gt;
  
  
  How I initially learned...
&lt;/h2&gt;

&lt;p&gt;Normally, if we wanted to target a specific element in an array we would use the indices of the array. Below we have an array with the first 6 letters of the alphabet. To target the letter A and assign to it its own local variable, we set the variable equal to the letter's index in the array as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--buM9OBTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sd5yauuewil2qt9vul0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--buM9OBTh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sd5yauuewil2qt9vul0i.png" alt="Image description" width="628" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we console.log the newly made variable we get back the letter A in the original array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pKysnpih--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/py07lafdpxeoqliarbu3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pKysnpih--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/py07lafdpxeoqliarbu3.png" alt="Image description" width="470" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As for objects, we would use dot notation to set new variables to specific elements within the object. Like so...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jWnmT1rH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3n5xq9jvvvb4x60lukv7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jWnmT1rH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3n5xq9jvvvb4x60lukv7.png" alt="Image description" width="661" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And when we console.log the new variable it will show the value for the key muppetName... &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oi82mB35--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqcw347a6986te8nmwc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oi82mB35--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqcw347a6986te8nmwc5.png" alt="Image description" width="495" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now this seems all fine and dandy but what if we had much larger arrays or objects and we wanted to set each element or value to it's own local variable? That would make for a lot of lines of code that is not so easy to read. That's where the wonderful tool of destructuring comes in and cleans everything up. &lt;/p&gt;




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

&lt;p&gt;The destructuring assignment syntax was introduced in ES6 and it allows you to take objects and arrays and easily extract the values and assign them to their own variables. &lt;/p&gt;

&lt;h2&gt;
  
  
  Destructuring arrays
&lt;/h2&gt;

&lt;p&gt;Below we have the same array as before but now we can set each value of the array to its own variable in one line of code. To break this down, the new variables need to equal the index positions of the original array and essentially inserted into a new array. Then the new array must be set equal to the original array variable. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iVw4eeO4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzbecfk3ja09ucutpz4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iVw4eeO4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzbecfk3ja09ucutpz4a.png" alt="Image description" width="573" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And when we console.log each individual variable the values of the original array will show. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eV9zkgVK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz28xe1qsdo1d13onfcd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eV9zkgVK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz28xe1qsdo1d13onfcd.png" alt="Image description" width="465" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also skip values within arrays by taking out value we want to skip and keeping the comma. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tjDwKrVM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nh58fqaefhpx9gt0pct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tjDwKrVM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0nh58fqaefhpx9gt0pct.png" alt="Image description" width="595" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will return&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9D0epExU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvhw7lzy11fuimgabkca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9D0epExU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvhw7lzy11fuimgabkca.png" alt="Image description" width="469" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another pretty useful trick is the ability to copy the rest of the array and put it into a new array using the spread operator. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kx3X8efA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zetl6tg9andm52g8ole.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kx3X8efA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zetl6tg9andm52g8ole.png" alt="Image description" width="611" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Adding the spread operator syntax to the array constant then returns &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jAOXYNgM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dudz23mjx43zz1ov45c0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jAOXYNgM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dudz23mjx43zz1ov45c0.png" alt="Image description" width="469" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Functions that return arrays can also be destructured as show below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SyNbdHxJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a284f1ixcfs1bmgjarr6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SyNbdHxJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a284f1ixcfs1bmgjarr6.png" alt="Image description" width="481" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Same concept except the array constant is set to the function call with whatever parameters you want to pass and it will return the added and subtracted values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_sTedbKU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snb8gjfoiqd8o503hpoq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_sTedbKU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snb8gjfoiqd8o503hpoq.png" alt="Image description" width="463" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see this functionality works well on arrays but the power of it is really shown when it is used to destructure objects. &lt;/p&gt;




&lt;h2&gt;
  
  
  Destructuring objects
&lt;/h2&gt;

&lt;p&gt;Unpacking objects works in basically the same way as destructuring an array except instead of square brackets we use curly braces and the variables we designate are not based on the index position but on the name of the key in the object. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VqJO0hT---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5s4xz040opjoscpeni9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VqJO0hT---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5s4xz040opjoscpeni9.png" alt="Image description" width="602" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the variables designated in the curly braces match the keys in the object it will return the values of the nestedName and nestedColor...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8nXroCuE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8v89d1odwkrh3m01yvh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8nXroCuE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8v89d1odwkrh3m01yvh.png" alt="Image description" width="470" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, in the instance that we don't want the variable name to match the key name we can easily change it like so&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vir_9mxj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7q36yvdl5a0ss57oz8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vir_9mxj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7q36yvdl5a0ss57oz8g.png" alt="Image description" width="639" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And when we console.log the new variable name it will console the same value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8nXroCuE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8v89d1odwkrh3m01yvh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8nXroCuE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8v89d1odwkrh3m01yvh.png" alt="Image description" width="470" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also access the nested key value pairs within the object we are working with and directly assign variables to them as well. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kDyW5E4V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8dx3blgzcfnxd01ep35.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kDyW5E4V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8dx3blgzcfnxd01ep35.png" alt="Image description" width="734" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We set the new curly braced variables equal to the nested object we wanted to access and when we logged the variables it returned the values we wanted!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jrQrsixi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eaas0m8t560y002rdw3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jrQrsixi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eaas0m8t560y002rdw3a.png" alt="Image description" width="461" height="164"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  To wrap things up
&lt;/h2&gt;

&lt;p&gt;With all that said, here is a good visual representation of how destructuring cleans up your code when you need to assign local variables to each key value pair in an object...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dot notation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HlquFIUQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p6xfqrxzwx84bm1zwvg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HlquFIUQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p6xfqrxzwx84bm1zwvg.png" alt="Image description" width="676" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Destructuring:&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--deQUXNG6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yosjcdtxiavogzsbkig6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--deQUXNG6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yosjcdtxiavogzsbkig6.png" alt="Image description" width="880" height="706"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We went from seven lines of code to three! There are a few more cool destructuring tricks we can use to our advantage but these are just a couple of the basic things that I learned that I will definitely be using in my code from now on! &lt;/p&gt;

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