<?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: Jamelse</title>
    <description>The latest articles on DEV Community by Jamelse (@jamelse).</description>
    <link>https://dev.to/jamelse</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F925157%2F096daca6-5a6f-4213-a22d-f3ac167f5a76.jpg</url>
      <title>DEV Community: Jamelse</title>
      <link>https://dev.to/jamelse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamelse"/>
    <language>en</language>
    <item>
      <title>A Simple Guide to Active Model Serializers in Ruby on Rails</title>
      <dc:creator>Jamelse</dc:creator>
      <pubDate>Thu, 20 Jul 2023 03:57:58 +0000</pubDate>
      <link>https://dev.to/jamelse/a-simple-guide-to-active-model-serializers-in-ruby-on-rails-4o7p</link>
      <guid>https://dev.to/jamelse/a-simple-guide-to-active-model-serializers-in-ruby-on-rails-4o7p</guid>
      <description>&lt;p&gt;Serialization is an important consideration for developing robust and efficient Ruby on Rails APIs. The process of serializing involves transforming complicated data structures, Ex: Ruby objects, into a form that can be quickly communicated and reassembled on the client-side. Active Model Serializers is a strong Rails gem that streamlines serialization. We'll look at what Active Model Serializers are, how to set them up, and how to use their features to develop clean and consistent APIs in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Active Model Serializers?
&lt;/h2&gt;

&lt;p&gt;Before we get into how to use AMS, we must first understand what the gem does. AMS offers a straightforward and adaptable means of managing JSON serialization in Rails applications. It works by putting a layer of abstraction between your Rails models and the JSON representation to the API. AMS allows you to simply specify the structure and relationships of your API answers without clogging up your controllers with serialization logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up / Installation
&lt;/h2&gt;

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

&lt;p&gt;Include the gem 'active_model_serializers' in your gemfile:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pX5mOqFG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hzld2cgcw0u378h7qjg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pX5mOqFG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hzld2cgcw0u378h7qjg.png" alt="Image description" width="247" height="24"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set Up:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we need to generate an ActiveModel::Serializer for our whatever out model is. In this example, I will generate it for my Expense model. Thankfully, the gem provides a generator through naming convention. In your terminal, run:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BFsMpEg3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z6l1md13cedfxbkw722.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BFsMpEg3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5z6l1md13cedfxbkw722.png" alt="Image description" width="209" height="21"&gt;&lt;/a&gt;&lt;br&gt;
This should generate a new folder in /app called &lt;strong&gt;serializers&lt;/strong&gt;. In that newly generated folder, will be a newly generated ruby file calledexpense_serializer.rb. In my example, my Expense model has :name, :amount, and :date attributes. The file will look something like this-&lt;br&gt;
&lt;strong&gt;In /app/serializers/expense_serializer.rb :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0_fbktzH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjm16lzhjjpnyf83koqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0_fbktzH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sjm16lzhjjpnyf83koqa.png" alt="Image description" width="473" height="81"&gt;&lt;/a&gt;&lt;br&gt;
If we wanted to customize our JSON response, we simply change which attributes we include. For example if I wanted my JSON response to NOT include the :date attribute, I would simply remove it from the list: &lt;/p&gt;

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

&lt;p&gt;Now that all of that is set up. When we call /expenses or /expenses/:id we should get our preferred JSON response!&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Methods
&lt;/h2&gt;

&lt;p&gt;AMS also allows us customize information by creating instance methods on our ExpenseSerializer class. We can then add this attribute to our list and get that method rendered in our JSON response! For example, if we wanted to create a method that shows us a readable amount of our expense name and cost we could do something like: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a6L3AxVG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a9iac0wc0s8gzgi9ws6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a6L3AxVG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a9iac0wc0s8gzgi9ws6.png" alt="Image description" width="425" height="158"&gt;&lt;/a&gt;&lt;br&gt;
This should add a readable_expense attribute to the end of our JSON response with something like, "Phone Bill: $120".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Associations:&lt;/strong&gt;&lt;br&gt;
If we have multiple associated models, AMS can handle this by using the Active Record macros has_many and belongs_to. We can use these macros to include them in our JSON response! For example, lets say we have a User Class along with our Expense Class. In this instance, the User will have &lt;strong&gt;many&lt;/strong&gt; expenses, and an expense will &lt;strong&gt;belong&lt;/strong&gt; to a user. Firstly, we must make sure both of our models contain this association.&lt;br&gt;
&lt;strong&gt;In /app/models/user.rb&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BFyizqXf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2klwqr2dz0ni1a1ulor.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BFyizqXf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2klwqr2dz0ni1a1ulor.png" alt="Image description" width="258" height="51"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;In /app/models/expense.rb&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SQe-q2yZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsmidhw2w8rsfxaxbxsx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SQe-q2yZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsmidhw2w8rsfxaxbxsx.png" alt="Image description" width="304" height="51"&gt;&lt;/a&gt;&lt;br&gt;
To add this to our serializers, it's as simple as including these macros in our UserSerializer and ExpenseSerializer classes.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WPPCpSvy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/851l883g3dip4xlnk63b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WPPCpSvy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/851l883g3dip4xlnk63b.png" alt="Image description" width="546" height="122"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lXySeJBJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wo8pem5bpvivyngg19ma.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lXySeJBJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wo8pem5bpvivyngg19ma.png" alt="Image description" width="464" height="120"&gt;&lt;/a&gt;&lt;br&gt;
In this case, this is a one-to-many association. The same can be done the same way for a many-to-many association. However, rather than including &lt;code&gt;has many :users, through: :expenses&lt;/code&gt; like in our model. We only define this in our model and simply call the has_many macro in our serializer class file. &lt;/p&gt;

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

&lt;p&gt;Active Model Serializers is a fantastic gem that simplifies the serialization process in Ruby on Rails applications. By following this guide, you can get started with AMS and create simple, clean and effective APIs. Consider using customization options where necessary, and look into using alternative adapters if you wish to utilize a different format than JSON. If you made it this far, thank you for reading, I hope this guide could have helped simplify some of the concepts and uses of AMS. Last but not least, best of luck and &lt;strong&gt;happy coding :)&lt;/strong&gt; . &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Beginner's Guide To React Router (v6)</title>
      <dc:creator>Jamelse</dc:creator>
      <pubDate>Mon, 28 Nov 2022 22:00:09 +0000</pubDate>
      <link>https://dev.to/jamelse/beginners-guide-to-react-router-v6-1fa4</link>
      <guid>https://dev.to/jamelse/beginners-guide-to-react-router-v6-1fa4</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvbvcn9raxltgj2cemcu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvbvcn9raxltgj2cemcu.png" alt="Image description" width="800" height="418"&gt;&lt;/a&gt;&lt;br&gt;
React router is a very useful tool for creating a React app. React Router is probably the most popular library developers use to do all of their &lt;strong&gt;client-side routing&lt;/strong&gt;. The reason we use client-side routing is because Create React App is designed to create single-page applications. Meaning, the application only contains one HTML document and the server only has to load the original GET of this document, CSS, and JS files; rather than multiple pages. The big benefit and reason to this is for the &lt;strong&gt;speed&lt;/strong&gt; of the application. What does React Router do? There are many things React Router can do, but the main two features to remember: &lt;br&gt;
&lt;strong&gt;1. Rendering components based on on the URL.&lt;/strong&gt; Ex: When the url is /ShoppingList, your ShoppingList component will display.&lt;br&gt;
 &lt;strong&gt;2. Page navigation using JavaScript.&lt;/strong&gt; Ex: On a navigation bar, when you click the Shopping List tab, the page content updates to the ShoppingList component. &lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;You can install React Router from the public NPM directory in two ways. In the terminal from the root directory of your application run either: &lt;br&gt;
&lt;strong&gt;npm install react-router-dom&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2nqf487v65x34ck82vxt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2nqf487v65x34ck82vxt.png" alt="Image description" width="347" height="26"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;yarn add react-router-dom&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5qmxnfrjapm5pyznods6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5qmxnfrjapm5pyznods6.png" alt="Image description" width="347" height="26"&gt;&lt;/a&gt;&lt;br&gt;
If you are on Version 5 and need to upgrade to version 6, add the @latest tag to the end of either. Ex: npm install &lt;a href="mailto:react-router-dom@latest"&gt;react-router-dom@latest&lt;/a&gt;.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3qhkowi90xvf6ehlpno.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3qhkowi90xvf6ehlpno.png" alt="Image description" width="347" height="26"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Example
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In index.js:&lt;/strong&gt;&lt;br&gt;
We must first import BrowserRouter from react-router-dom and wrap our entire application in the BrowserRouter component.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fls4p9zkldy996m5o3ji4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fls4p9zkldy996m5o3ji4.png" alt="Image description" width="650" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In App.js:&lt;/strong&gt;&lt;br&gt;
Firstly, we must import { Routes, Route } from react-router-dom. Next, we want to import all the components we want to render. Lastly, we want to put the Routes component in the App function's return statement and nest all of our individual Route components in the Routes component.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hq5hmzpb6egu087faew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hq5hmzpb6egu087faew.png" alt="Image description" width="800" height="571"&gt;&lt;/a&gt;&lt;br&gt;
Each Route takes two props. First is the path of the route. Ex: path="/contact" will route to yourWebsite.com/contact. Second is the element of the path, or what you want to display for that path. In this example it will be the Contact component. The reason we use the &lt;strong&gt;exact&lt;/strong&gt; prop for the home route of "/" is that if we enter a value into the URL that doesn't exist like /something, it will still render our home component. This happens because "/" without &lt;strong&gt;exact&lt;/strong&gt; would still be a partial match to /something. Also, we set a path to "*" is this acts as a &lt;em&gt;catch&lt;/em&gt; in case we get a URL that doesn't meet any of the criteria. This is good for any kind of error messages or 404 not found statements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In NavBar.js:&lt;/strong&gt;&lt;br&gt;
Import either {NavLink} or {Link} from react-router-dom to start. Both Link and NavLink render as an &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag in the DOM and have the same base functionality. They are used to set URL's and keep track of the browsing history. They will both link to an internal path, EX: to="/projects". This &lt;strong&gt;to&lt;/strong&gt; prop acts as the &lt;strong&gt;href=&lt;/strong&gt; of an &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag. When these &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags are clicked, they change the URL to that path. In doing this it tells React Router to display the Route that matches the new URL. The difference between NavLink and Link is NavLink gets styled attributes when it matches the current URL. As in the name, NavLink is typically better for a navigation bar because of this styling.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojrp3n1bit77jb805286.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojrp3n1bit77jb805286.png" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;br&gt;
Side note: As discussed in the previous section with the &lt;strong&gt;exact&lt;/strong&gt; prop you may have to use exact for a basic path like "/". This is  because the other paths that contain "/" would still be partial matches. &lt;/p&gt;

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

&lt;p&gt;Client-side routing is a must for single-page applications and React Router is the best / most used tool to handle this. I hope you were able to learn something about the core functionality of React Router in order to do some client-side routing. If you made it this far, thanks for reading and happy coding :)!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional Links:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/react/react_router.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/react/react_router.asp&lt;/a&gt;&lt;br&gt;
&lt;a href="https://reactrouter.com/en/main/start/overview" rel="noopener noreferrer"&gt;https://reactrouter.com/en/main/start/overview&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/react-router" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/react-router&lt;/a&gt;&lt;/p&gt;

</description>
      <category>regex</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Quick Guide to Synchronous vs Asynchronous JavaScript and Callback Functions + Fetch</title>
      <dc:creator>Jamelse</dc:creator>
      <pubDate>Wed, 21 Sep 2022 10:03:35 +0000</pubDate>
      <link>https://dev.to/jamelse/quick-guide-to-synchronous-vs-asynchronous-javascript-and-callback-functions-fetch-1e1g</link>
      <guid>https://dev.to/jamelse/quick-guide-to-synchronous-vs-asynchronous-javascript-and-callback-functions-fetch-1e1g</guid>
      <description>&lt;p&gt;People and programmers alike will encounter many things/expressions when learning to code and/or coding. A few of these expressions are the different ways people will describe certain functions. A couple of these are &lt;em&gt;synchronous&lt;/em&gt;, &lt;em&gt;asynchronous&lt;/em&gt;, and &lt;em&gt;callback&lt;/em&gt; functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callback Functions&lt;/strong&gt;&lt;br&gt;
Let's start with callback functions. The simple way to define callback functions is that they are functions that are passed as arguments into other functions. This function is then &lt;strong&gt;invoked&lt;/strong&gt; and completes some type of action. You will use callback functions quite a lot and quite more often as you code. They are very useful tools! A good example of their uses are that they are the specific 'arguments' that have to be passed in: Array iterator methods, eventListeners, etc. &lt;br&gt;
Example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hhlXty9T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms1j5zg1blhgqpu9x6g3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hhlXty9T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms1j5zg1blhgqpu9x6g3.png" alt="Image description" width="661" height="336"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Synchronous Functions/Code&lt;/strong&gt;&lt;br&gt;
Before we define synchronous and asynchronous functions we must first understand how JavaScript runs. JavaScript is a single threaded language. This means that JavaScript will only perform one action or piece of logic at a time when running code. With this being understood, we can now define synchronous functions/code. The definition of the word synchronous is, 'existing or occurring at the same time'. This is a good way to think of synchronous code. JavaScript runs the code sequentially from top to bottom, waiting only for the previous line of code. This type of code is one we're most used to.&lt;br&gt;
Examples of synchronous code are:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n3IcDDnm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf80qoiootnrejw1ng7r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n3IcDDnm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf80qoiootnrejw1ng7r.png" alt="Image description" width="378" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Asynchronous Functions/Code&lt;/strong&gt;&lt;br&gt;
Now that we understand how JavaScript runs its code and synchronous code, we can now define asynchronous code.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xRv7SUlw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ld1mvgw3wasveq44s70j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xRv7SUlw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ld1mvgw3wasveq44s70j.png" alt="Image description" width="778" height="422"&gt;&lt;/a&gt;&lt;br&gt;
This type of code runs &lt;em&gt;asynchronously&lt;/em&gt;. Meaning that when JavaScript runs its code, it will read that line of code and put it on the "back-burner" while JavaScript does other work- until that code or function finishes its work. This method returns a &lt;strong&gt;promise&lt;/strong&gt; to JavaScript. A promise is essentially in the name- a promise. You promise JavaScript the eventual completion or failure of an asynchronous operation. A promise can be in one of three states: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pending&lt;/strong&gt;- Initial state of the program before the promise passes or fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resolved&lt;/strong&gt;- A successful promise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rejected&lt;/strong&gt;- A failed promise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Two examples of asynchronous code/functions are the setTimeout() and fetch(&lt;em&gt;&amp;lt;--more on this later&lt;/em&gt;) methods. Lets look at a couple examples of sync vs async code:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aw2_5ZuV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6xvwtz5pb8xuos9xf0t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aw2_5ZuV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6xvwtz5pb8xuos9xf0t.png" alt="Image description" width="195" height="56"&gt;&lt;/a&gt; Returns:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yfv4XtaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubibu5jc2d0lfm89x48m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yfv4XtaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubibu5jc2d0lfm89x48m.png" alt="Image description" width="207" height="76"&gt;&lt;/a&gt;&lt;br&gt;
The above example are both examples of basic synchronous code. Lets see what happens when we add an asynchronous method to one of them:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kRSBgkFh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kklwy68y5s1czgkwwlx9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kRSBgkFh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kklwy68y5s1czgkwwlx9.png" alt="Image description" width="382" height="62"&gt;&lt;/a&gt; Returns:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j-z2aRx3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuvevu2eunw8lp0kts9z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j-z2aRx3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuvevu2eunw8lp0kts9z.gif" alt="Image description" width="502" height="203"&gt;&lt;/a&gt;🤔&lt;br&gt;
The above example here is a bit of synchronized code on the 2nd line and an asynchronized piece of code on the first line! This gives you an idea on how JavaScript will "back-burner" that code and run other code until the "back-burner" code finishes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fetch()&lt;/strong&gt;&lt;br&gt;
One method, touching again on the promise concept, and another example of  asynchronous functions are fetch requests. Fetch() is a global method that is used to retrieve data. The path link to the data that is in the parentheses is this function's &lt;strong&gt;argument&lt;/strong&gt;.This argument is a promise object.&lt;br&gt;
Example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wYCKJfQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yddakafx7d0dtpqx520l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wYCKJfQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yddakafx7d0dtpqx520l.png" alt="Image description" width="319" height="162"&gt;&lt;/a&gt;&lt;br&gt;
In the above example as you can see the fetch request is followed by two .then's. Tying together what we touched on in the beginning, they both take a &lt;strong&gt;callback function&lt;/strong&gt; as an argument. The first .then extracts the JSON content from the promise object by using JavaScript's built in .json() method (resp =&amp;gt; resp.json()), making the data useable for us! The second .then uses it's callback function to do whatever we want with this data (.then((data) =&amp;gt; console.log(data));)! Fetch essentially touches on everything we covered earlier from callback functions to asynchronous functions! Fetch has &lt;em&gt;several&lt;/em&gt; requests that you can make with several different ways to use it. Here are some good resources to cover the different requests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch"&gt;Using the Fetch API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET"&gt;GET&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST"&gt;POST&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH"&gt;PATCH&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE"&gt;DELETE&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Here we learned what callback functions are, the difference between synchronous vs asynchronous code/functions, and how fetch() can tie all of these things together. These concepts can be kind of hard to grasp for the unfamiliar, but as always the best thing you can do is practice. If you would like to read a bit more on asynchronous JavaScript, you can &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous"&gt;here&lt;/a&gt;. If you made it this far, thank you for your time reading through. I hope you could have learned something new or maybe refreshed yourself on existing knowledge. Be well, never stop moving forward, and happy coding :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Array Iterator Methods in JavaScript</title>
      <dc:creator>Jamelse</dc:creator>
      <pubDate>Wed, 14 Sep 2022 00:43:26 +0000</pubDate>
      <link>https://dev.to/jamelse/understanding-array-iterator-methods-in-javascript-1k9a</link>
      <guid>https://dev.to/jamelse/understanding-array-iterator-methods-in-javascript-1k9a</guid>
      <description>&lt;p&gt;Arrays in JavaScript are useful data structures that are used to store collections of information. Looping through arrays is a powerful tool to iterate through arrays and do things with its data when programming. While loops work well, there are simpler methods to iterate through information in Java. These methods are actually built into JavaScript! They're called Array iterator methods.&lt;/p&gt;

&lt;p&gt;I, like many others, struggled with the concept of array iteration, and its specific built-in methods. Although the methods are very very handy, they can be somewhat difficult to get an understanding of.&lt;/p&gt;

&lt;p&gt;Here, I will dive into JavaScript's different built-in array iteration methods, and hopefully you will leave here with a better understanding on how they work!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;METHODS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;.find() Method&lt;/strong&gt;&lt;br&gt;
The Array.find() method is used to iterate through the given array it's attached to and will return the &lt;em&gt;&lt;strong&gt;first&lt;/strong&gt;&lt;/em&gt; element that "satisfies" the passed argument/condition. This argument/condition in the parentheses is a function! This allows the .find() method to execute more complex searches. If nothing meets the given condition in the callback function it will return &lt;em&gt;undefined&lt;/em&gt;. Example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JKmxEOh5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rj2zf4eir9bshya2vljw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JKmxEOh5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rj2zf4eir9bshya2vljw.png" alt="Image description" width="536" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.filter() Method&lt;/strong&gt;&lt;br&gt;
The Array.filter() method is used to iterate through a given array and will return &lt;em&gt;&lt;strong&gt;ALL&lt;/strong&gt;&lt;/em&gt; elements that satisfy the passed argument/condition. These passed elements will be copied into a newly created array that is created by the method itself. This method also takes a callback function as the argument/condition so it can execute more complex searches. One note is since this method creates an entirely new array, results that don't satisfy the condition at all won't return &lt;em&gt;undefined&lt;/em&gt;. Rather, these results will return an empty array. Easiest way to think of it is in the name itself: a &lt;strong&gt;filter&lt;/strong&gt;! You take away everything you don't want and leave behind the things you do!&lt;br&gt;
Example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h5eAtbFT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e35eqnxjmowct4uyvujr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h5eAtbFT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e35eqnxjmowct4uyvujr.png" alt="Image description" width="780" height="543"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.map() Method&lt;/strong&gt;&lt;br&gt;
The Array.map() method is used to iterate through a given array and &lt;em&gt;&lt;strong&gt;modifies all&lt;/strong&gt;&lt;/em&gt; of the elements of the array. This method also creates a new array. It will be the same length of the original array, and will be populated by all the modified results. This method also takes a callback function, which will be used to defined how we want to modify the given array. Side note: depending on the types of data in the array you could get interesting results. For example, an array containing strings and numbers may transform differently.&lt;br&gt;
Example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_QFV32O3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbxl1l3xba87bid0ae8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_QFV32O3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbxl1l3xba87bid0ae8e.png" alt="Image description" width="598" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.reduce() Method&lt;/strong&gt;&lt;br&gt;
The Array.reduce() method is used to iterate through a given array to &lt;em&gt;&lt;strong&gt;reduce&lt;/strong&gt;&lt;/em&gt; all the given elements of the array to a single value. The method takes a callback function which is the "reducer" in this case. The function passes on each element of the array in order. The return result of this is a single value. The callback function should also take an initial value. Easiest way to think of this is this number is where the callback function will begin to execute. Else, if left empty, the first element of the array at index 0 is used as the initial value- which would then start the iteration at index 1 (the second value of the array) and you could get some surprising results from this. In some cases it won't matter, in some it might. With that being said, it's usually better to put a number. If you want to start at the first element of the array, put 0, and so on.&lt;br&gt;
Example:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--55ZqWqOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uboquiz2u5b8kcdvef5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--55ZqWqOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uboquiz2u5b8kcdvef5l.png" alt="Image description" width="800" height="239"&gt;&lt;/a&gt;&lt;br&gt;
Note: In Example 1 it doesn't matter if this contains an initial value. Here it is without:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9n-WTkLy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hriaws05aygd86xwlwef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9n-WTkLy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hriaws05aygd86xwlwef.png" alt="Image description" width="786" height="149"&gt;&lt;/a&gt;&lt;br&gt;
However, in Example 2 it &lt;em&gt;does&lt;/em&gt; matter if it contains an initial value. Here it is without: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CpfzQDxh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfmd0941ac81m8yrhsi2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CpfzQDxh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfmd0941ac81m8yrhsi2.png" alt="Image description" width="713" height="152"&gt;&lt;/a&gt;&lt;br&gt;
In example 2 without the initial value the callback function never executes for the first element of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.forEach() Method&lt;/strong&gt;&lt;br&gt;
The array.forEach() method is used to iterate through a given array and executes the provided argument(function) for each element. The provided argument/condition takes a callback function. Unlike previous methods, .forEach() differs in the way that it doesn't have a built-in return value. forEach() is the least expressive iterator method. Most of its purposes are better served by the uses of .map() or .reduce(). So when should you use .forEach() then? Well, there aren't a lot of real uses for forEach(). One good one is to log each value of an array - which is great to use when debugging!. It's better in this case than .map() because there is no use for creating a new array if you just want to log the values of an array.&lt;br&gt;
Example of forEach:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bZVv4lDq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qipj9p07j57e3mp4e99s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bZVv4lDq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qipj9p07j57e3mp4e99s.png" alt="Image description" width="414" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Comments
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--btnLcGgU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3jogwptku4j38pxqmuv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--btnLcGgU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3jogwptku4j38pxqmuv.jpg" alt="Image description" width="800" height="698"&gt;&lt;/a&gt;&lt;br&gt;
Array iterator methods are very very powerful tools for programmers. While hard to understand and grasp the concepts of each one and some maybe more than others; The best thing you can do at the end of the day is code. &lt;strong&gt;Practice practice practice&lt;/strong&gt; makes perfect. Just writing code and practicing using these methods will make them much easier to understand. Once you can fully grasp/understand the concepts and uses for each of these methods, you will have these powerful tools at your coding arsenal. I hope this could help people appreciate and learn more about array iteration methods and how they work. &lt;/p&gt;

&lt;p&gt;Keep coding and never stop moving forward &amp;lt;3&lt;/p&gt;

&lt;p&gt;My &lt;a href="https://github.com/jamelse"&gt;GitHub&lt;/a&gt; :)&lt;/p&gt;

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