<?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: Josh Blengino</title>
    <description>The latest articles on DEV Community by Josh Blengino (@jblengino510).</description>
    <link>https://dev.to/jblengino510</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%2F670392%2F214697f5-fb2f-4e1c-8ed5-29154cb636f8.jpg</url>
      <title>DEV Community: Josh Blengino</title>
      <link>https://dev.to/jblengino510</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jblengino510"/>
    <language>en</language>
    <item>
      <title>Uploading files in a React/Rails App using Active Storage</title>
      <dc:creator>Josh Blengino</dc:creator>
      <pubDate>Tue, 28 Sep 2021 05:23:41 +0000</pubDate>
      <link>https://dev.to/jblengino510/uploading-files-in-a-react-rails-app-using-active-storage-201c</link>
      <guid>https://dev.to/jblengino510/uploading-files-in-a-react-rails-app-using-active-storage-201c</guid>
      <description>&lt;p&gt;Uploading files is a common thing that we do on the internet.  Uploading images to Instagram, uploading audio files to SoundCloud, uploading videos to Youtube, uploading images to this blog, etc.  If you are looking to integrate a feature like this into your React/Rails application, I got you.  In this blog I will be using &lt;strong&gt;postgresql&lt;/strong&gt; for the database and I will be dealing with audio files such as mp3, wav, etc.  Thanks to &lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html" rel="noopener noreferrer"&gt;Active Storage&lt;/a&gt;, the process for doing this is not as complicated as you might think.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backend setup
&lt;/h2&gt;

&lt;p&gt;Run this in your console:&lt;/p&gt;

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

rails active_storage:install


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

&lt;/div&gt;

&lt;p&gt;This will create 2 tables that you can view in your &lt;code&gt;./db/migrate&lt;/code&gt; folder, then run:&lt;/p&gt;

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

rails db:migrate


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

&lt;/div&gt;

&lt;p&gt;If you check your &lt;code&gt;schema&lt;/code&gt; this is what you should see:&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%2Fset9svkygkiz6eug9cv6.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%2Fset9svkygkiz6eug9cv6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Don't worry too much about these for now, I will be linking other helpful blogs at the bottom that explain more in detail about how these tables work under the hood if you want to know more.&lt;/p&gt;

&lt;h4&gt;
  
  
  has_one_attached macro
&lt;/h4&gt;

&lt;p&gt;This macro will magically associate a file attachment to whatever model you apply it to.  In the context of this example, I want a user to be able to upload beats with an audio file attached.  You can name the macro whatever you want I just decided to call it &lt;code&gt;:audio_data&lt;/code&gt; (e.g. :audio, :audio_file, :image, :picture, etc) I recommend naming it with a description that matches the type of file you are going to be working with:&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%2Fwhx21f7glxe9lf1lfvz1.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%2Fwhx21f7glxe9lf1lfvz1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Controller
&lt;/h4&gt;

&lt;p&gt;Make sure to add it to your controller params:&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%2F7ujyrt5pf4b3nmd926t2.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%2F7ujyrt5pf4b3nmd926t2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Serializer
&lt;/h4&gt;

&lt;p&gt;Your serializer should look something like this, make sure to include &lt;strong&gt;line 2&lt;/strong&gt; and just replace &lt;code&gt;audio_data&lt;/code&gt; with whatever you named it:&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%2Flluldezqv5erziqeblzp.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%2Flluldezqv5erziqeblzp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontend setup
&lt;/h2&gt;

&lt;p&gt;Now that our backend is ready to handle the upload from the frontend, let's create a basic form:&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%2Fyu5nm5xctr3k9gr1dkp5.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%2Fyu5nm5xctr3k9gr1dkp5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  State
&lt;/h4&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%2Fmoj0il40d0vwt92hbu0q.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%2Fmoj0il40d0vwt92hbu0q.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The form
&lt;/h4&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%2Frhfyl428a98ojp2w5tc9.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%2Frhfyl428a98ojp2w5tc9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;input's accept attribute&lt;/strong&gt; - we are setting the value to &lt;em&gt;"audio/*"&lt;/em&gt; which allows a user to upload all audio file types.  Use &lt;em&gt;"image/*"&lt;/em&gt; for images and &lt;em&gt;"video/*"&lt;/em&gt; for videos.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;onChange&lt;/strong&gt; - with file uploads, they are going to be stored in an array called &lt;code&gt;files&lt;/code&gt;.  Because we are only uploading 1 file we just access our file at the [0] index.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Making the POST request to our backend
&lt;/h4&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%2F5suqj68o73llzlwxmtpt.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%2F5suqj68o73llzlwxmtpt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Files require different packaging than your typical &lt;code&gt;JSON.stringify()&lt;/code&gt;.  We need to send the body as &lt;strong&gt;formData&lt;/strong&gt; by creating an empty formData object and then appending our state to it.  The right side of the comma is our state we had at the top, make sure the left side of the comma matches what you have in your &lt;code&gt;schema&lt;/code&gt;.  Don't bother trying to &lt;strong&gt;console.log&lt;/strong&gt; the formData object either, you will just get an empty object.&lt;/p&gt;

&lt;h4&gt;
  
  
  Rest of the code
&lt;/h4&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%2Fozk70kq6j89vvk0ekpy1.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%2Fozk70kq6j89vvk0ekpy1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Just fetching a user and setting it in state to provide a &lt;code&gt;user_id&lt;/code&gt;, also fetching from beats to use in the audio player.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing it out
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  Making a GET request in Postman
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4jsa3k2pdc5uez5bwi0s.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%2F4jsa3k2pdc5uez5bwi0s.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
BOOM!!  After submitting, you can now make a GET request to your server and see that your upload was successful!  &lt;/p&gt;

&lt;h2&gt;
  
  
  Enjoy the fruits of your labor
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/&amp;amp;lt;616666155&amp;amp;gt;" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;br&gt;
Clicking that play button is a great feeling.  Now go sicko mode and unleash your new file uploading powers!!  Below are resources that really helped me accomplish this, please let me know in the comments below if there is anything important I may have left out.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://www.w3schools.com/tags/att_input_accept.asp" rel="noopener noreferrer"&gt;HTML input accept Attribute - W3Schools&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData" rel="noopener noreferrer"&gt;FormData() - MDN&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects" rel="noopener noreferrer"&gt;Using FormData Objects - MDN&lt;/a&gt;&lt;br&gt;
&lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html#cross-origin-resource-sharing-cors-configuration" rel="noopener noreferrer"&gt;Active Storage&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@noahshafer0211/how-to-upload-mp3s-to-your-rails-backend-using-active-storage-83baaed3395a" rel="noopener noreferrer"&gt;How to upload mp3s to your rails backend using active storage&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/swlh/react-file-uploads-to-rails-cc9c62e95a9d" rel="noopener noreferrer"&gt;React File Uploads to Rails&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/swlh/upload-images-to-your-rails-api-from-react-the-easy-way-241bbe71ea85" rel="noopener noreferrer"&gt;Upload images to your Rails API from React the easy way&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/react-audio-player" rel="noopener noreferrer"&gt;React Audio Player&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>ruby</category>
      <category>rails</category>
      <category>database</category>
    </item>
    <item>
      <title>Polymorphic Associations in Rails</title>
      <dc:creator>Josh Blengino</dc:creator>
      <pubDate>Wed, 08 Sep 2021 18:32:56 +0000</pubDate>
      <link>https://dev.to/jblengino510/polymorphic-associations-in-rails-167o</link>
      <guid>https://dev.to/jblengino510/polymorphic-associations-in-rails-167o</guid>
      <description>&lt;p&gt;When setting up an API in Rails it is crucial to establish how all of your models are going to be connected to each other using &lt;a href="https://guides.rubyonrails.org/association_basics.html" rel="noopener noreferrer"&gt;&lt;strong&gt;Active Record Associations&lt;/strong&gt;&lt;/a&gt;.  Polymorphic associations are a little more advanced but can be very useful when your application becomes more complex.&lt;/p&gt;

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

&lt;p&gt;This is a new concept for me and so far I have learned that polymorphism is a core concept in Object-Oriented-Programming that can be used in many different contexts.  This blog will be focusing on this concept in the context of building a Rails API.  We can get a more clear understanding of this concept by breaking it down into its &lt;em&gt;Greek&lt;/em&gt; roots:&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%2Fk2jk3tep3cdd3fvdk8t4.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%2Fk2jk3tep3cdd3fvdk8t4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
If something is polymorphic it has many forms.  For example, &lt;code&gt;numbers&lt;/code&gt; in Ruby can be &lt;code&gt;integers&lt;/code&gt;(whole #'s), or &lt;code&gt;floats&lt;/code&gt;(decimal #'s).  A polymorphic association in Rails allows you to associate a single model with multiple models through one association declaration (instead of having multiple &lt;code&gt;belongs_to&lt;/code&gt; associations).&lt;/p&gt;

&lt;h2&gt;
  
  
  Where could this be useful?
&lt;/h2&gt;

&lt;p&gt;Let's say you are building an app that allows users to post content, comment on other posts, and like other posts.  Pretty common features that we are all familiar with.  Now you want to extend the like feature to comments allowing a user to like individual comments.  Your ERD might (or might not) look something like this:&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%2Fx1gboqhtub5j2kvrkve1.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%2Fx1gboqhtub5j2kvrkve1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Something like this would work fine, but if you wanted to extend the like feature throughout your app to potential new models in the future you would need to create a separate likes table to the database for each new model you introduce.  Overtime this could unnecessarily crowd your database and will end up becoming a &lt;strong&gt;repetitive&lt;/strong&gt; task.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polymorphic associations
&lt;/h2&gt;

&lt;p&gt;With polymorphic associations we can create a &lt;em&gt;poly_likes&lt;/em&gt; table that will act as an &lt;strong&gt;interface&lt;/strong&gt; between other models we want to associate likes to.  This isn't the greatest looking ERD but it is looking a little better.  Now everytime we want to introduce a new model, we don't need to create a separate likes table to go with that model.&lt;br&gt;&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%2Fuwjpdacxy5g3kltgnkw2.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%2Fuwjpdacxy5g3kltgnkw2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a poly_likes table
&lt;/h2&gt;

&lt;p&gt;To create our new model in the terminal we are going to include an interesting column/value pair.&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%2Fwdz4k86q5is3n9x761xt.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%2Fwdz4k86q5is3n9x761xt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  What does the migration show us?
&lt;/h4&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%2Fxripx39pxp683ijnm7n5.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%2Fxripx39pxp683ijnm7n5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Lets run the migration and check out the schema
&lt;/h4&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%2Fyo8usygvfo442aneeste.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%2Fyo8usygvfo442aneeste.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  What about the model?
&lt;/h4&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%2Fss4drf850mxf0tps0nrg.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%2Fss4drf850mxf0tps0nrg.png" alt="Alt Text"&gt;&lt;/a&gt;&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%2Fs40yohyisbyfk1mihjlr.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%2Fs40yohyisbyfk1mihjlr.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
If you're seeing all of this for the first time the syntax might seem a little weird.   Why are our associations named with "-able"?  &lt;/p&gt;

&lt;h2&gt;
  
  
  Naming convention for polymorphic associations
&lt;/h2&gt;

&lt;p&gt;If you're familiar with Rails you know that sticking to naming conventions is part of Rails' magic.  To make all of this work, Rails uses "-able" with the class name to name polymorphic associations.  Basically what we are doing here is making a comment and a post "likeable".  Let's make sure the rest of our models are set up to make all of this work:&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%2Fzhjj7mk6nesjhkmuja9g.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%2Fzhjj7mk6nesjhkmuja9g.png" alt="Alt Text"&gt;&lt;/a&gt;&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%2Fi7lzpom6d2tt959ypgra.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%2Fi7lzpom6d2tt959ypgra.png" alt="Alt Text"&gt;&lt;/a&gt;&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%2Fz0x2trs9vqizkmrggu9r.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%2Fz0x2trs9vqizkmrggu9r.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
PLEASE correct me if I am wrong, but I left out the "as: likeable" from the &lt;em&gt;user&lt;/em&gt; model because that would make a user likeable which is not what I want in this situation (who knows maybe you want to allow a user to be liked).  In this situation I just want a &lt;em&gt;poly_like&lt;/em&gt; to be associated with a specific user.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing it out in the console
&lt;/h2&gt;

&lt;p&gt;If you take a close look at the query being generated it is looking for a &lt;em&gt;poly_like&lt;/em&gt; that has a &lt;em&gt;user_id&lt;/em&gt; which is what I wanted.  In these examples I did not create any poly_like seeds so we are getting back an empty array, but we can focus on the queries to see how these associations are working.&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%2Fmw202fxo2ja7j9mykgh3.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%2Fmw202fxo2ja7j9mykgh3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Now let's check a comment and a post
&lt;/h4&gt;

&lt;p&gt;If we look at the queries again we can see that the &lt;em&gt;likeable_id&lt;/em&gt; matches the id of the respective object and the &lt;em&gt;likeable_type&lt;/em&gt; references the model &lt;strong&gt;type&lt;/strong&gt; in the form of a string!&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%2F2ern8ihg0ym7kwo8gg8t.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%2F2ern8ihg0ym7kwo8gg8t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pros
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keeps our code DRY by not having to use a bunch of repetitive &lt;code&gt;belongs_to&lt;/code&gt; or &lt;code&gt;has_many&lt;/code&gt; associations across all of our models.&lt;/li&gt;
&lt;li&gt;Prevents you from needing to modify existing models and/or adding new join tables to create associations with newly introduced models.  (Ex: we did not have to add a separate likes table for the comment &amp;amp; post models like we did in the first ERD.)&lt;/li&gt;
&lt;li&gt;Makes it easier to add new relationships.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cons
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Syntax could take some getting use to at first.&lt;/li&gt;
&lt;li&gt;No foreign keys.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Polymorphism might be a little tricky to wrap your head around at first, but it is really just a fancy word for saying an &lt;strong&gt;object&lt;/strong&gt; can be related to many &lt;strong&gt;different&lt;/strong&gt; &lt;strong&gt;types&lt;/strong&gt; of objects.  In my short experience with polymorphism I can see why this a powerful concept but it is probably not necessary for every situation.  I am not an expert on this topic and I used this blog as an opportunity to learn an important concept.  Please let me know in the comments below if there are any corrections I need to make to this post or if there are some other practical use cases where polymorphism could really come in handy.  Hopefully this post made polymorphism more approachable for you.  I linked some very helpful resources below that really helped give me a fundamental understanding.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=pTB0EiLXUC8" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=pTB0EiLXUC8&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=SjUVEUCMoX8" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=SjUVEUCMoX8&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=5EidzTqsw-E&amp;amp;t=620s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=5EidzTqsw-E&amp;amp;t=620s&lt;/a&gt;&lt;br&gt;
&lt;a href="https://guides.rubyonrails.org/association_basics.html#polymorphic-associations" rel="noopener noreferrer"&gt;Polymorhpic associations&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/single-table-inheritance-vs-polymorphic-associations-in-rails-af3a07a204f2/" rel="noopener noreferrer"&gt;freeCodeCamp blog on polymorphic associations&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used" rel="noopener noreferrer"&gt;Stackoverflow polymorphism&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>database</category>
    </item>
    <item>
      <title>Halftime: Trusting the Process during a coding bootcamp</title>
      <dc:creator>Josh Blengino</dc:creator>
      <pubDate>Wed, 25 Aug 2021 20:01:49 +0000</pubDate>
      <link>https://dev.to/jblengino510/halftime-trusting-the-process-during-a-coding-bootcamp-253i</link>
      <guid>https://dev.to/jblengino510/halftime-trusting-the-process-during-a-coding-bootcamp-253i</guid>
      <description>&lt;p&gt;The purpose of this blog is to share my experience @&lt;a href="https://flatironschool.com/"&gt;Flatiron School&lt;/a&gt; so far, for anyone out there who is new to coding and seriously considering applying to a coding bootcamp.  I am writing this in hopes of helping you decide if a coding bootcamp is the right option for you.  If you are reading this or another blog post like this, that shows that you are &lt;strong&gt;serious&lt;/strong&gt; and &lt;strong&gt;care&lt;/strong&gt; about what you do, which is a step in the right direction and something you should feel good about while navigating through the uncertain territory of a potential career switch.&lt;/p&gt;

&lt;p&gt;If you haven't heard it by now, a coding bootcamp is a serious commitment. It's going to require everything: time, energy, and money.  Why would you make a sacrifice like this if you weren't at least a little serious about it? Making a move like this could be really impractical based on your situation.  So, &lt;strong&gt;Why&lt;/strong&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  Knowing your "Why?"
&lt;/h2&gt;

&lt;p&gt;Defining your "why" is one of the first things that Flatiron School really challenges you to think about when going through the application process.  This is something that I genuinely appreciated because it showed me their intentions to make students a priority.  They force you to really think about it before handing them all of your hard earned money.  Admissions gave me a whole week to think about it before sharing it to them during a non-technical interview.  I don't remember the colleges that I attended ever asking me to look within myself like this before asking me for my money. 🤔&lt;/p&gt;

&lt;h2&gt;
  
  
  A little bit of background about me
&lt;/h2&gt;

&lt;p&gt;I am from the Bay Area in Northern California, and have no background in computer science.  Before making my switch into tech, I graduated with a BS in Kinesiology with the intention of pursuing grad school.  I am currently enrolled in Flatiron's full-time (online) Software Engineering program which is broken up into 5 phases over 15 total weeks.   &lt;/p&gt;

&lt;h2&gt;
  
  
  Prework
&lt;/h2&gt;

&lt;p&gt;After going through the application process, Flatiron is going to give you access to material that is going to be your foundation of knowledge going into Day 1 of the bootcamp.  I highly recommend giving yourself atleast &lt;strong&gt;2 full months&lt;/strong&gt; (maybe even 3 months) to go through the prework to give yourself time to let the material sink in.  The prework will introduce you to the terminal, Git, GitHub, HTML, CSS, and Javascript. &lt;/p&gt;

&lt;h2&gt;
  
  
  Week 1 (Phase 1)
&lt;/h2&gt;

&lt;p&gt;The first week was the by far my toughest week and really tested my "why".  I was already asking myself, "what did I get myself into?".  Lectures were moving fast and we were assigned a ton of homework for the week.  I knew the coding bootcamp was not going to be easy, but man... they call it a bootcamp for a reason! &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tBTkRZWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzdfrmpjstk1i25823ai.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tBTkRZWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzdfrmpjstk1i25823ai.jpeg" alt="Alt Text" width="299" height="169"&gt;&lt;/a&gt;&lt;br&gt;
During this phase I really struggled to keep up with the pace of the program.  I found myself feeling very behind not only by the amount of assignments that needed to get done, but also in my understanding of the concepts.  There was a lot of information to take in, and a lot of moving parts to that information.  The &lt;strong&gt;imposter syndrome&lt;/strong&gt; that Flatiron warns you about during the application process leading up to Day 1 was getting REAL.&lt;/p&gt;

&lt;h2&gt;
  
  
  My first coding challenge (week 2)
&lt;/h2&gt;

&lt;p&gt;Each phase is 3 weeks, and there is a coding challenge at the end of the 2nd week of each phase.  A coding challenge is basically a test. It's their version of a midterm.  You are given some tasks, and about 3 hours to execute those tasks in code.  &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cl5gxEyn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebz4p977csyfuuy4ulff.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cl5gxEyn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebz4p977csyfuuy4ulff.gif" alt="Alt Text" width="300" height="208"&gt;&lt;/a&gt;&lt;br&gt;
I did not pass the first coding challenge, and this is exactly how I felt.  This was a critical moment for me, I was extremely hard on myself after receiving my results, doubtful thoughts were crowding my mind: "I should have been good enough!", "Am I smart enough for this?", "Maybe my brain isn't wired for this type of stuff.", "Will I have to be held back?".  This was one of the few times in my life I genuinely contemplated quitting, and I am pretty stubborn about finishing anything that I start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bouncing Back
&lt;/h2&gt;

&lt;p&gt;After &lt;strong&gt;allowing&lt;/strong&gt; myself to let those negative emotions and thoughts pass, I knew that quitting was not an option.  The next day I noticed on our Slack channel that a some of my classmates had DM'd me yesterday right after class to check in on me and make sure I was alright.  My frustration was probably written all over my face.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RqUM175---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhlfsbrwdhwifjt6muh7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RqUM175---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhlfsbrwdhwifjt6muh7.gif" alt="Alt Text" width="300" height="169"&gt;&lt;/a&gt;&lt;br&gt;
The sense of urgency to DM me literally right after class ended that day really uplifted my spirits because it showed me the amazing support system I have around me and most importantly, that I am not alone.  Our main instructor also helped to put things in perspective by reminding all of us that, "this is only your second week of coding EVER in your life".  For most of us this was true, the majority of my cohort also comes from non-tech backgrounds.  Sometimes you just need to take a step back and look at the bigger picture which helped me realize that everything I was experiencing at the time was absolutely normal.  Once again, Flatiron warned all of us that this would happen!&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I'm at
&lt;/h2&gt;

&lt;p&gt;Thanks to the amazing support system that is my cohort, I have made it to phase 3 and am on my way to phase 4.  As the program has been progressing, material started sticking faster, I developed a rhythm for completing labs, and making time to study outside of class.  Some of my classmates were even asking me for help 😱.  Despite the workload gradually increasing and becoming more difficult throughout different points of the program, I have been able to stay afloat, I just needed some time to adapt.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yn3y-D3N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfb4kcg2bnqnawtvls8m.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yn3y-D3N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfb4kcg2bnqnawtvls8m.gif" alt="Alt Text" width="480" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where I'm headed
&lt;/h2&gt;

&lt;p&gt;A lot has happened since phase 1.  So far we have learned &lt;strong&gt;Javascript&lt;/strong&gt; in phase 1, &lt;strong&gt;React&lt;/strong&gt; (a Javascript framework) in phase 2, and &lt;strong&gt;Ruby&lt;/strong&gt; in phase 3.  Phase 4 we will be learning &lt;strong&gt;Ruby on Rails&lt;/strong&gt; which is a Ruby framework.  Phase 5 we will all be on our own to build our own fullstack application (frontend &amp;amp; backend) applying everything Flatiron has taught us.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Major Takeways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Coding is a very difficult skill to learn&lt;/strong&gt;: don't get too hard on yourself, if you are feeling stressed out by a particular problem, sometimes you just need to walk away from your desk for 5 minutes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't fall into the trap of comparing yourself to your classmates&lt;/strong&gt;: it can also be easy to discourage yourself when the material is coming easier to others than it is for yourself, or you might have a few people in your cohort who have some programming experience or a computer science background which can also be intimidating.  EVERYONE has different SPEEDS and STRENGTHS.  Just focus on you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your cohort is your greatest resource&lt;/strong&gt;:  you are not alone in all of this.  Everyone else in your cohort is going through similar, if not the same emotions and struggles as you.  You are all tackling the same material.  DO NOT isolate yourself and be afraid to ask for help in fear of "looking dumb".  Take advantage of the opportunity to grow and collaborate with your classmates because you are all in it together anyways.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Get comfortable being uncomfortable&lt;/strong&gt;:  you will surprise yourself when you see how quick you start to digest the material.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HYGZVdN3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vv0tx6xqqvykb8fk8xb1.png" alt="Alt Text" width="560" height="315"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remember your "Why"&lt;/strong&gt;:  anything in life that is worth it is going to be difficult.  A coding bootcamp is going to push you if you come from a computer science background or not.  There is going to be many challenges thrown at you in many different forms.  It's in these moments that remembering your "why" will help you to push through.
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;My experience so far at Flatiron School has been a lot of ups &amp;amp; downs.  There has been a ton of growth!  Besides all of the coding knowledge I have learned up to this point, I have also learned a lot more about myself.  I have learned more about HOW I learn and what works best for me (self-awareness is a major 🔑) and most importantly, I have learned the importance of community.  I have been blown away by my experience with my cohort.  The level of comradery that we have developed up to this point has been far beyond what I imagined it would be.  Overall, there is so much more to be gained from a coding bootcamp than just the technologies they teach.  Hopefully I have helped you get a better idea of whether or not a coding bootcamp is a good option for you or not.  Just know that it will be a journey and remember to TRUST THE PROCESS.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UiG6YnZW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv4xovnx63evdeoelg4u.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UiG6YnZW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv4xovnx63evdeoelg4u.gif" alt="Alt Text" width="300" height="169"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>motivation</category>
    </item>
    <item>
      <title>The spaceship operator &lt;=&gt; in Ruby</title>
      <dc:creator>Josh Blengino</dc:creator>
      <pubDate>Wed, 11 Aug 2021 20:41:02 +0000</pubDate>
      <link>https://dev.to/jblengino510/the-spaceship-operator-in-ruby-2fmk</link>
      <guid>https://dev.to/jblengino510/the-spaceship-operator-in-ruby-2fmk</guid>
      <description>&lt;p&gt;If you're a Ruby noob like me, you probably saw the "spaceship operator" and thought of Space Invader or Darth Vader's infamous TIE fighter.  I am currently on day 2 of learning &lt;a href="https://ruby-doc.org/" rel="noopener noreferrer"&gt;Ruby&lt;/a&gt; going into day 3 and I had trouble understanding what this interesting looking operator does and what you could use it for.  Hopefully by the end of this post, you and I can both take away some practical use cases for this method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ruby Comparison Operators
&lt;/h2&gt;

&lt;p&gt;Before we start experimenting with the &amp;lt;=&amp;gt; operator, let's take a look at more common comparison operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; true     (Greater than)&lt;/span&gt;
 &lt;span class="mi"&gt;0&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="c1"&gt;#=&amp;gt; true     (Less than)&lt;/span&gt;
 &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="c1"&gt;#=&amp;gt; true     (Greater or equal than)&lt;/span&gt;
 &lt;span class="mi"&gt;0&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="c1"&gt;#=&amp;gt; true     (Less or equal than)&lt;/span&gt;
 &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="c1"&gt;#=&amp;gt; false    (Equals)&lt;/span&gt;
 &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="c1"&gt;#=&amp;gt; true     (Not equals)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Pretty straightforward, they all &lt;strong&gt;return a boolean value&lt;/strong&gt;.  An important takeaway from one of my readings was to remember that all Ruby operators are actually &lt;em&gt;methods&lt;/em&gt;.  This is because the behavior (return value) will depend on the type of &lt;strong&gt;class&lt;/strong&gt; (object) it is called on.&lt;/p&gt;
&lt;h2&gt;
  
  
  &amp;lt;=&amp;gt; Operator
&lt;/h2&gt;

&lt;p&gt;It's technical name is the &lt;strong&gt;combined comparison operator&lt;/strong&gt;.  The major difference compared to the other comparison operators is that it &lt;strong&gt;returns 3 different values&lt;/strong&gt;: &lt;code&gt;(-1), (0), or (1)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; -1&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For &lt;code&gt;Integers&lt;/code&gt;: (0) if right side = left, (-1) if less than, and (1) if greater than.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="s2"&gt;"string"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0     (contents are the same)&lt;/span&gt;
&lt;span class="s2"&gt;"string"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"stringg"&lt;/span&gt;    &lt;span class="c1"&gt;#=&amp;gt; -1    (length of string)&lt;/span&gt;
&lt;span class="s2"&gt;"string1"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"string2"&lt;/span&gt;   &lt;span class="c1"&gt;#=&amp;gt; -1    (string integer)&lt;/span&gt;
&lt;span class="s2"&gt;"STRING"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; -1    (capital)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For &lt;code&gt;Strings&lt;/code&gt; the behavior is not as straight forward.  It looks like uppercase letters are given a lower value than lowercase letters.  Why?&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/sylviapap" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F334178%2Fa8c34313-4f2c-4a79-bac9-4ec76005f68a.jpeg" alt="sylviapap"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/sylviapap/the-ruby-enumerable-operator-and-max-vs-maxby-ac6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;The Ruby Enumerable &amp;lt;=&amp;gt; operator and #max vs. #max_by &lt;/h2&gt;
      &lt;h3&gt;Sylvia Pap ・ Feb 12 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#ruby&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#codenewbie&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#computerscience&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Another great post on this topic mentions how characters in strings are compared with binary values under the hood, which helps to better understand the above return values.&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"X-wing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Luke Skywalker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Rebel Alliance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; nil&lt;/span&gt;
&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; nil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For &lt;code&gt;Hashes&lt;/code&gt; it's even less clear...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the contents are exactly the same with expected results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"X-wing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Luke Skywalker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Rebel Alliance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:owner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:owner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:owner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:owner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 1&lt;/span&gt;
&lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:owner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:owner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here it looks like the string length comes in to play after changing fighter2 back to normal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;fighter1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"X-wing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Luke Skywalker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Rebel Alliance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; 1&lt;/span&gt;
&lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;     &lt;span class="c1"&gt;#=&amp;gt; -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though these examples do not showcase every possible scenario for these data types, we now know that the behavior of the &amp;lt;=&amp;gt; operator for &lt;code&gt;Arrays&lt;/code&gt; and &lt;code&gt;Hashes&lt;/code&gt; is a little unpredictable depending on their respective contents.  Even &lt;a href="https://ruby-doc.org/core-2.7.3/Enumerable.html#method-i-sort" rel="noopener noreferrer"&gt;Ruby's documentation&lt;/a&gt; mentions how unpredictable the return value could be based on the objects being compared. &lt;br&gt;
 That's still not very helpful for how we could use this in a practical way.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/l1KsOGE23suZQeCfm/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l1KsOGE23suZQeCfm/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Using &amp;lt;=&amp;gt; with Ruby's &lt;code&gt;sort&lt;/code&gt; method
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;star_fighters&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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"X-wing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Luke Skywalker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Rebel Alliance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"Jedi Starfighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Obi-Wan Kenobi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"The Republic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;sorted_star_fighters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;star_fighters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;sorted_star_fighters&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; {:type=&amp;gt;"TIE Fighter", :owner=&amp;gt;"Darth Vader", :affiliation=&amp;gt;"Galactic Empire", :id=&amp;gt;1}, {:type=&amp;gt;"X-wing", :owner=&amp;gt;"Luke Skywalker", :affiliation=&amp;gt;"Rebel Alliance", :id=&amp;gt;2}, {:type=&amp;gt;"Jedi Starfighter", :owner=&amp;gt;"Obi-Wan Kenobi", :affiliation=&amp;gt;"The Republic", :id=&amp;gt;3}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In this scenario, our hashes are being ordered from lowest to highest because based on how our id's are set up, fighter2's id will always be greater than fighter1's, which will return -1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;star_fighters&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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"X-wing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Luke Skywalker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Rebel Alliance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"Jedi Starfighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Obi-Wan Kenobi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"The Republic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;sorted_star_fighters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;star_fighters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;sorted_star_fighters&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; {:type=&amp;gt;"Jedi Starfighter", :owner=&amp;gt;"Obi-Wan Kenobi", :affiliation=&amp;gt;"The Republic", :id=&amp;gt;3}, {:type=&amp;gt;"X-wing", :owner=&amp;gt;"Luke Skywalker", :affiliation=&amp;gt;"Rebel Alliance", :id=&amp;gt;2}, {:type=&amp;gt;"TIE Fighter", :owner=&amp;gt;"Darth Vader", :affiliation=&amp;gt;"Galactic Empire", :id=&amp;gt;1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, our hashes are being ordered from highest to lowest because in this case, fighter2's id will always be greater than fighter1's id, which will return 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Return values are integers: &lt;code&gt;(-1), (0), or (1)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return values can be unpredictable with &lt;code&gt;Strings&lt;/code&gt; &amp;amp; &lt;code&gt;Hashes&lt;/code&gt; depending on their contents which makes &lt;code&gt;Integers&lt;/code&gt; a more attractive option to work with.&lt;/li&gt;
&lt;li&gt;The &amp;lt;=&amp;gt; operator can come in handy when using &lt;code&gt;sort&lt;/code&gt; to order your data from ascending or descending order in a cleaner way instead of doing something like this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;star_fighters&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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"TIE Fighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Galactic Empire"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"X-wing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Luke Skywalker"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"Rebel Alliance"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &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="ss"&gt;type: &lt;/span&gt;&lt;span class="s2"&gt;"Jedi Starfighter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;owner: &lt;/span&gt;&lt;span class="s2"&gt;"Obi-Wan Kenobi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;affiliation: &lt;/span&gt;&lt;span class="s2"&gt;"The Republic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;sorted_star_fighters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;star_fighters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&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;elsif&lt;/span&gt; &lt;span class="n"&gt;fighter1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fighter2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;sorted_star_fighters&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; {:type=&amp;gt;"TIE Fighter", :owner=&amp;gt;"Darth Vader", :affiliation=&amp;gt;"Galactic Empire", :id=&amp;gt;1}, {:type=&amp;gt;"X-wing", :owner=&amp;gt;"Luke Skywalker", :affiliation=&amp;gt;"Rebel Alliance", :id=&amp;gt;2}, {:type=&amp;gt;"Jedi Starfighter", :owner=&amp;gt;"Obi-Wan Kenobi", :affiliation=&amp;gt;"The Republic", :id=&amp;gt;3}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Based on my short experience with the &amp;lt;=&amp;gt; operator, it seems to be most useful in tandem with the &lt;code&gt;sort&lt;/code&gt; enumerator.  I would love to hear if anyone else knows any other use cases in the comments below.  Hopefully this post gave you a slightly better understanding of the &amp;lt;=&amp;gt; operator.  I am definitely going to continue to experiment with it.  Other languages that use the &amp;lt;=&amp;gt; operator include: Perl, Apache, Groovy, PHP, Eclipse, Ceylon, and C++.  Having a good grasp on how and when to use conditional operators to compare values will allow you to control the flow of your data like a jedi controlling the flow of The Force.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/3ohuPp7uWLgTdU83iU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3ohuPp7uWLgTdU83iU/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://www.rubyguides.com/2017/03/ruby-equality/" rel="noopener noreferrer"&gt;Ruby equality&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.rubyguides.com/2018/07/ruby-operators/" rel="noopener noreferrer"&gt;Ruby operators&lt;/a&gt;&lt;br&gt;
&lt;a href="https://ruby-doc.org/core-2.7.3/Enumerable.html#method-i-sort" rel="noopener noreferrer"&gt;Ruby sort enumerator&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/html/html_charset.asp" rel="noopener noreferrer"&gt;HTML Encoding (Character Sets)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=tpsdxtf01po" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=tpsdxtf01po&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=f4NItw7r33E" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=f4NItw7r33E&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Destructuring in Vanilla JS for Beginners</title>
      <dc:creator>Josh Blengino</dc:creator>
      <pubDate>Tue, 20 Jul 2021 17:22:52 +0000</pubDate>
      <link>https://dev.to/jblengino510/destructuring-in-vanilla-js-for-beginners-14dn</link>
      <guid>https://dev.to/jblengino510/destructuring-in-vanilla-js-for-beginners-14dn</guid>
      <description>&lt;p&gt;Destructuring is a very useful way for accessing multiple properties within an array or an object in Javascript.  Learning this technique is going to be essential if you are planning to learn React in the future.  Even if you do not plan to learn React, this technique is also a great way to make your code cleaner and easier to work with.  &lt;/p&gt;

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

&lt;p&gt;When you destructure an array or object you are converting it to a smaller array, object, or variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Array Destructuring
&lt;/h2&gt;

&lt;p&gt;Below we have two arrays to work with:&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;jedi&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;Yoda&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;Qui Gon Jinn&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;Obi Wan Kenobi&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;Luke SkyWalker&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;sith&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;Emperor Palpatine&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;Darth Vader&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;Darth Maul&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;Kylo Ren&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;If we wanted to access each jedi we would have to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jedi&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;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;jedi&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="nx"&gt;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;jedi&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;jedi&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;In our console we would get:&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;Yoda&lt;/span&gt;
&lt;span class="nx"&gt;Qui&lt;/span&gt; &lt;span class="nx"&gt;Gon&lt;/span&gt; &lt;span class="nx"&gt;Jinn&lt;/span&gt;
&lt;span class="nx"&gt;Obi&lt;/span&gt; &lt;span class="nx"&gt;Wan&lt;/span&gt; &lt;span class="nx"&gt;Kenobi&lt;/span&gt;
&lt;span class="nx"&gt;Luke&lt;/span&gt; &lt;span class="nx"&gt;SkyWalker&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a tedious way of accessing each jedi in our jedi array.&lt;br&gt;
Instead, let's destructure our jedi 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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstJedi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondJedi&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jedi&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;firstJedi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our console this would print:&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;Yoda&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We took the array we wanted to destructure and put it on the right side and assigned it to some variable names. Each variable's position will correspond with each element in the array we are destructuring.  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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstJedi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondJedi&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jedi&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;secondJedi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will print:&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;Qui&lt;/span&gt; &lt;span class="nx"&gt;Gon&lt;/span&gt; &lt;span class="nx"&gt;Jinn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we wanted to combine both the jedi and the sith arrays?  To do this we can use the &lt;strong&gt;spread operator&lt;/strong&gt; (...):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jedi&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;Yoda&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;Qui Gon Jinn&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;Obi Wan Kenobi&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;Luke SkyWalker&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;sith&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;Emperor Palpatine&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;Darth Vader&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;Darth Maul&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;Kylo Ren&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;jediVsSith&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;jedi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;sith&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;jediVsSith&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our console will print:&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;Yoda&lt;/span&gt;
&lt;span class="nx"&gt;Qui&lt;/span&gt; &lt;span class="nx"&gt;Gon&lt;/span&gt; &lt;span class="nx"&gt;Jinn&lt;/span&gt;
&lt;span class="nx"&gt;Obi&lt;/span&gt; &lt;span class="nx"&gt;Wan&lt;/span&gt; &lt;span class="nx"&gt;Kenobi&lt;/span&gt;
&lt;span class="nx"&gt;Luke&lt;/span&gt; &lt;span class="nx"&gt;SkyWalker&lt;/span&gt;
&lt;span class="nx"&gt;Emperor&lt;/span&gt; &lt;span class="nx"&gt;Palpatine&lt;/span&gt;
&lt;span class="nx"&gt;Darth&lt;/span&gt; &lt;span class="nx"&gt;Vader&lt;/span&gt;
&lt;span class="nx"&gt;Darth&lt;/span&gt; &lt;span class="nx"&gt;Maul&lt;/span&gt;
&lt;span class="nx"&gt;Kylo&lt;/span&gt; &lt;span class="nx"&gt;Ren&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jtW-spqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tr2fqppkotpylj2qmdye.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jtW-spqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tr2fqppkotpylj2qmdye.gif" alt="Darth Maul fighting Obi Wan &amp;amp; Qui Gon"&gt;&lt;/a&gt;&lt;br&gt;
Destructuring allows us to access elements within our arrays in a way that is easier to read by assigning each element to a variable name of our choosing without having to use bracket notation.&lt;/p&gt;
&lt;h2&gt;
  
  
  Object Destructuring
&lt;/h2&gt;

&lt;p&gt;Destructuring an object is going to be very similar to an array only we will be using curly braces &lt;strong&gt;{}&lt;/strong&gt;.  Below we have two objects:&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;darthVader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;famouseLine&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 am your FATHER!!&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;yoda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;famouseLine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fear is the path to the darkside.&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;If we want to access yoda's age and lightsaber:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lightSaber&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;yoda&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;age&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;lightSaber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our console:&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="mi"&gt;900&lt;/span&gt;
&lt;span class="nx"&gt;green&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of our variables being based on position like it was with arrays, they will be based on the name of the key instead.  The names of the variables must match the names of the keys. &lt;/p&gt;

&lt;h3&gt;
  
  
  What if we want to use a different variable name?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yodaAge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yodaLightSaber&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;yoda&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;yodaAge&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;yodaLightSaber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our console will give the same results:&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="mi"&gt;900&lt;/span&gt;
&lt;span class="nx"&gt;green&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What if we want to add a default value to a key that yoda does not already have?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yodaAge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;yodaLightSaber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;anotherYodaQuote&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 greatest teacher, failure is.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;yoda&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;yodaAge&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;yodaLightSaber&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;anotherYodaQuote&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this our console will show our added default value but does not actually add the key/value pair to our yoda 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="mi"&gt;900&lt;/span&gt;
&lt;span class="nx"&gt;green&lt;/span&gt;
&lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;greatest&lt;/span&gt; &lt;span class="nx"&gt;teacher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;failure&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using the spread operator(...) with object destructuring:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;yoda&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;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Console:&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;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;famouseLine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fear is the path to the darkside.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;strong&gt;spread operator&lt;/strong&gt; we accessed the rest of yoda's properties except for the age property because we already declared it.  The variable &lt;strong&gt;rest&lt;/strong&gt; to the right of the spread operator can be a variable name of your choosing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Combining objects using the spread operator(...)
&lt;/h3&gt;

&lt;p&gt;Let's remove some properties to each object and see what happens:&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;darthVader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="na"&gt;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;famouseLine&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 am your FATHER!!&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;yoda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newJedi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;darthVader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;yoda&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;newJedi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Console:&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;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;900&lt;/span&gt;
&lt;span class="nx"&gt;lightSaber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;famouseLine&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 am your FATHER!!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how when we combined our objects, properties from the second object (&lt;strong&gt;yoda&lt;/strong&gt;) were added to the first object (&lt;strong&gt;darth vader&lt;/strong&gt;) if they were not already there and would also override the first object's property if they both had the same property.&lt;/p&gt;

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

&lt;p&gt;There are many more use cases for destructuring arrays and objects within functions.  If you are a beginner like me, it might take some time to wrap your head around this topic, but that is OKAY!  Check out the links below if you want to dive deeper into this topic. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tk6lk15U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2cosb6tymrz8p663zmy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tk6lk15U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2cosb6tymrz8p663zmy.jpeg" alt="A yoda meme telling you to be patient and learn"&gt;&lt;/a&gt;&lt;br&gt;
If you plan to learn React, becoming more comfortable with this technique will make life easier when you start working with &lt;strong&gt;props&lt;/strong&gt; and &lt;strong&gt;components&lt;/strong&gt;.  Stay tuned for &lt;strong&gt;Part II&lt;/strong&gt; of this blog topic where we will destructure some props and components within functions in React!&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;MDN Destructuring Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.freecodecamp.org/news/javascript-object-destructuring-spread-operator-rest-parameter/"&gt;freeCodeCamp Javascript Object Destructuring&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>arrays</category>
      <category>objects</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
