<?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: Deborah Kurz</title>
    <description>The latest articles on DEV Community by Deborah Kurz (@deborah).</description>
    <link>https://dev.to/deborah</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%2F1108937%2F8cf89de8-4031-4a32-935d-9b793242f771.png</url>
      <title>DEV Community: Deborah Kurz</title>
      <link>https://dev.to/deborah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deborah"/>
    <language>en</language>
    <item>
      <title>How to use Context in React</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Tue, 03 Sep 2024 02:05:49 +0000</pubDate>
      <link>https://dev.to/deborah/how-to-use-context-in-react-3aa9</link>
      <guid>https://dev.to/deborah/how-to-use-context-in-react-3aa9</guid>
      <description>&lt;p&gt;Welcome back, friends! &lt;/p&gt;

&lt;p&gt;Today we're going over the basics of a React Hook called 'useContext'. 'useContext' is a powerful tool that goes a step beyond 'useState' to create a global-like State, and allows us to pass variables and functions down to children and grandchildren components without passing props directly. Using context will completely transform your coding skills!&lt;/p&gt;

&lt;p&gt;But I’m getting ahead of myself. If you are not familiar with 'useState', jump over to my previous article first, then come back and prepare to be amazed! &lt;br&gt;
&lt;a href="https://dev.to/deborah/how-to-use-state-in-react-2pah"&gt;How To Use 'useState'&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now that you are up to speed on ‘useState’, let’s dive into ‘useContext’!&lt;/p&gt;
&lt;h1&gt;
  
  
  What is 'useContext'?
&lt;/h1&gt;

&lt;p&gt;'useContext' is ideal for data that needs to be placed on a global scope (such as a username that will keep someone logged in throughout the entirety of the application's use), but it is not the end-all-be-all shortcut to passing props down to children components and should be used sparingly.&lt;/p&gt;

&lt;p&gt;However, 'useContext' does allow us to pass data without passing props directly and is therefore extremely helpful when we &lt;em&gt;do&lt;/em&gt; encounter data that needs to be accessed by several children components or be made available across the entirety of the application. &lt;/p&gt;

&lt;p&gt;In order to get 'useContext' up and running we need to take two steps: first, we need to create a context object ('&lt;strong&gt;createContext&lt;/strong&gt;'), then we need to access the value via '&lt;strong&gt;useContext&lt;/strong&gt;'. &lt;/p&gt;

&lt;p&gt;Before we walk through this process, be aware that the following code examples have been simplified in order to give you a better idea of what 'useContext' is all about and how to use it. But, you should be aware that 'createContext' is often declared in a separate file of its own. In these examples, however, I am likening 'Parent.js' to a typical 'App.js' file (a component on the top of the component hierarchy). 'Parent.js' is where I have defined all my state variables and the functions that update those state variables. I chose to declare 'createContext' in my top-level component instead of creating its own file to simplify this explanation so you can better understand the core concepts of context. &lt;/p&gt;
&lt;h1&gt;
  
  
  With all that said, let’s get started!
&lt;/h1&gt;


&lt;h4&gt;
  
  
  1: Declaring 'createContext()'
&lt;/h4&gt;

&lt;p&gt;The first thing we need to do is to declare and export a variable called 'Context' which we will use later in our child components [we're creating a variable now in order to make our code more simple and so we can eventually place a value (data) inside of it to be accessed later]:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
export Context = React.createContext();

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

&lt;/div&gt;



&lt;p&gt;‘Context’ is a context object created by calling ‘createContext’. The context object holds a component called Provider which we will now be able to call and then pass the variables and functions that we want to keep at our 'global' level. &lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  2: Calling 'Provider'
&lt;/h4&gt;

&lt;p&gt;With our ‘Context’ variable ready to use let’s now jump down to our JSX in the return statement of our 'Parent' component. Here we will call ‘Context’ and wrap it in opening tags (angle brackets), and also call Provider like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
return(
    &amp;lt;Context.Provider &amp;gt;
        // Other JSX 
    &amp;lt;/Context.Provider&amp;gt;
);

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

&lt;/div&gt;



&lt;p&gt;In order to complete 'Context.Provider', we need to provide a value to ‘Provider’. To do this we will pass an object to 'value'. Inside the object, we will place any and all data that we want to access in child components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
return(
    &amp;lt;Context.Provider value ={{ example, setExample, handleExample }}&amp;gt;
        // Other JSX  
    &amp;lt;/Context.Provider&amp;gt;
);

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

&lt;/div&gt;



&lt;p&gt;It is &lt;strong&gt;VERY IMPORTANT&lt;/strong&gt; to note that we need to put ALL child components that will access context inside the 'Context.Provider' tags. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
return(
    &amp;lt;Context.Provider value ={{ example, setExample, handleExample }}&amp;gt;
        &amp;lt;Child /&amp;gt;
        &amp;lt;Components /&amp;gt;
        &amp;lt;Go /&amp;gt;
        &amp;lt;Here /&amp;gt;
    &amp;lt;/Context.Provider&amp;gt;
);

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

&lt;/div&gt;



&lt;p&gt;Notice that we don’t need to pass any props directly to the child components so long as we put the props inside ‘value’.&lt;/p&gt;

&lt;p&gt;Now that we have used 'createContext' and passed all our global-data to 'Context.Provider' we are ready to move on to the child components and use 'Context'. &lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  3: Importing 'Context' (child component)
&lt;/h4&gt;

&lt;p&gt;Let’s go to a child component, which (for the sake of this example) is housed in the file: 'Child.js'. As is life with coding: if you want to use something you need to import it. Let’s go ahead and get ‘Context’ from where we declared it in ‘Parent.js’ so we can use it in this child component (‘Child.js’):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import Context from ‘./Parent.js’;

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

&lt;/div&gt;



&lt;p&gt;Great! 'Context' is available for us to use now.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  4: Importing 'useContext' (child component)
&lt;/h4&gt;

&lt;p&gt;Now that we have access to ‘Context’ in the child component we need to import 'useContext' into the file so we can pass 'Context' to it (more on this shortly):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useContext } from ‘react’;
import Context from ‘./Parent.js’;

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

&lt;/div&gt;



&lt;p&gt;With 'useContext' and 'Context' now available to us, let's get working!&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  5: Using 'useContext' and 'Context' (child component)
&lt;/h4&gt;

&lt;p&gt;First, we need to declare a variable for 'useContext'. We’ll do this inside the 'Child' component in a  similar fashion to how we would declare 'useState':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useContext } from ‘react’;
import Context from ‘./Parent.js’;

function Child(){
    const { example, setExample } = useContext(Context)

    // The rest of our code

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

&lt;/div&gt;



&lt;p&gt;In this code we are using curly braces {} to denote destructuring assignment. That's a fancy way of saying we are able to call multiple variables and functions stored in Context, in one line of code. We are also passing ‘Context’ to 'useContext' so we can access the values defined in 'Context.Provider' (which we declared in ‘Parent.js’).  &lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  6: Using context in your code (child component)
&lt;/h4&gt;

&lt;p&gt;Believe it or not, you are all set! You can now use the context values in your code just like you would normally use State variables or functions. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const exampleId = example.id;

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setExample(newExample);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Brilliant! Isn't it?&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Let’s Recap:
&lt;/h1&gt;

&lt;p&gt;Congratulations! You now have all the tools to get started with 'createContext' and 'useContext'. You understand that 'useContext' allows you to create something of a ‘global state' that can pass variables and functions to components without passing props directly through child components.&lt;/p&gt;

&lt;p&gt;We also delved into the six steps required to get context working in your applications. You are now ready to begin coding with 'createContext' and 'useContext', but in case you ever need a quick-start guide, this is for you: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; In your parent component, declare and export a variable called 'Context' using 'createContext':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const Context = React.createContext();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; In the parent component’s JSX, wrap all child components that need access to context in 'Context.Proivder' and pass any variables/functions in an object to 'value':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Context.Provider value={{ example, setExample, handleExample }}&amp;gt;
    &amp;lt;ExampleChildComponent /&amp;gt;
&amp;lt;/Context.Provider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; In your child component, import 'useContext':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useContext } from ‘react’;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Also import ‘Context’ from the parent component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Context from “./Parent.js’;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5.&lt;/strong&gt; Then use 'useContext' and pass it your ‘Context’ variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { example, handleExample } = useContext(Context);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6.&lt;/strong&gt; Finally, use the context you now have access to (in our examples above this would be '&lt;em&gt;example&lt;/em&gt;' and '&lt;em&gt;handleExample&lt;/em&gt;') however you need to in the child component.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Well done! Until next time, happy coding!
&lt;/h3&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
One last note for those of you who want to go a little deeper: here are the official documentation resources I also referenced while learning 'useContext' and writing this blog. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Official Documentation:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://react.dev/reference/react/createContext" rel="noopener noreferrer"&gt;https://react.dev/reference/react/createContext &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Legacy Official Documentation&lt;/strong&gt;, still somewhat helpful for understanding 'useContext': &lt;br&gt;
&lt;a href="https://legacy.reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;https://legacy.reactjs.org/docs/context.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How To Use Formik &amp; Yup For Form Validations</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Tue, 16 Jul 2024 17:10:50 +0000</pubDate>
      <link>https://dev.to/deborah/how-to-use-formik-yup-for-form-validations-334l</link>
      <guid>https://dev.to/deborah/how-to-use-formik-yup-for-form-validations-334l</guid>
      <description>&lt;p&gt;Welcome back, fellow coders!&lt;/p&gt;

&lt;p&gt;Today we are going to be using the frontend library known as ‘Formik’ combined with a schema builder called “Yup” in a step-by-step framework to help you understand and implement validations on your React forms.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Formik and Yup?
&lt;/h2&gt;

&lt;p&gt;When I first started learning Formik (and using Yup with it) I didn’t feel confident in it because I didn't understand what it was for. So, before we begin with the step-by-step process, let’s make sure we’re on the same page when it comes to understanding what both Formik and Yup are and why they are important.&lt;br&gt;
Formik is a library used in React, and its official docs proudly state that it allows you to “Build forms in React, without the tears” (see ‘*Formik’ in the Reference section). What that statement really means is that Formik keeps track of values, validations, submissions, etc. in order to save you time and allow you to mentally keep track of more important details in your code (*Formik).&lt;br&gt;
As I mentioned previously, Yup is a schema builder (and also a library) that allows you to define a schema and use it hand-in-hand with Formik to create validations (*Yup).&lt;/p&gt;

&lt;p&gt;Okay, now let’s talk about the steps we can use in React to validate a form with Formik and Yup.&lt;/p&gt;
&lt;h2&gt;
  
  
  Formik and Yup Basics
&lt;/h2&gt;

&lt;p&gt;Before we dive into a hands-on example, let’s detail out the steps we will need to take:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the necessary libraries to get Formik and Yup running.&lt;/li&gt;
&lt;li&gt;Create a function for our Yup schema.&lt;/li&gt;
&lt;li&gt;Inside the Formik function, define initial values, validation schema, and instructions for onSubmit.&lt;/li&gt;
&lt;li&gt;Update our JSX to use Formik.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s get started!&lt;/p&gt;
&lt;h2&gt;
  
  
  Formik and Yup Step-By-Step Guide
&lt;/h2&gt;

&lt;p&gt;Recently I built a simple goals-tracker application and used Formik in the “Add Goal” form to validate the user intake and ensure that the value was a string between 1-20 characters long.&lt;br&gt;
I’ll be using the “Add Goal” form as an example going forward.&lt;br&gt;
To start with, this is what the form looked like in the User Interface:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fig94x0u5jznaiavwplg4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fig94x0u5jznaiavwplg4.png" alt="Add goal button" width="546" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you know what the basic UI looks like, let’s transition to the code.&lt;br&gt;
This is where we will start (without any Formik or Yup):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

function AddGoal({ handleGoal }){ 

    return(
        &amp;lt;div&amp;gt;
            &amp;lt;form onSubmit= &amp;gt;
                &amp;lt;input 
                    id="goal"
                    name="goal"
                    type="text" 
                    value=
                    placeholder = "Add A New Goal"
                    onChange=
                /&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
                &amp;lt;button type="submit"&amp;gt;Add My Goal&amp;lt;/button&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
                &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
            &amp;lt;/form&amp;gt;
        &amp;lt;/div&amp;gt;
    )
};

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

&lt;/div&gt;



&lt;p&gt;Now let’s implement the steps we outlined above:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Import the necessary libraries to get Formik and Yup running.
&lt;/h4&gt;

&lt;p&gt;Near the top of our page we will import the needed libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import { useFormik } from "formik";
import * as yup from "yup";

// Rest of code

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Create a function for your Yup schema.
&lt;/h4&gt;

&lt;p&gt;Now that Formik and Yup are available to us, we’re going to use Yup first to create our validations ("Add Goal" only has one user input, “goal”, but if you have more, you need to put a comma between them):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddGoal({ handleGoal }){
    const formschema = yup.object().shape({
        goal: yup.string().required("Please enter a goal with at least 1 character.").max(20)
    })

// Rest of code

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

&lt;/div&gt;



&lt;p&gt;In the code above we are declaring a variable “formschema” and using yup with its ‘object()’ and ‘shape()’ methods. These methods are basically saying “This is an object and its ‘shape’ will look like the following”.&lt;br&gt;
On our "goal" key, which we will use in a POST request to a database later on, we are once again using Yup with the following methods to create validations for our user input: .string(), .required(), and .max().&lt;/p&gt;

&lt;p&gt;Although our code technically isn’t working yet, let’s pretend it is for a moment and get a visual of what happens when we try to add a goal without entering anything into our input form:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh7qipzib3816nr82d1zo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh7qipzib3816nr82d1zo.png" alt="Finished form with validation error example" width="800" height="89"&gt;&lt;/a&gt;&lt;br&gt;
Here we can see that the validation has triggered the ".required()" part of our formschema and thrown an error.&lt;/p&gt;

&lt;p&gt;Okay, back to reality where we have NOT yet done everything needed yet to get these validation errors looking like the above photo… Let’s move on to the next step.&lt;/p&gt;
&lt;h4&gt;
  
  
  3. Inside the Formik function, we will define initial values, validation schema, and instructions for when onSubmit is triggered.
&lt;/h4&gt;

&lt;p&gt;Next on our to-do list is to create a variable called ‘formik'. Inside this variable, we will call the ‘useFormik’ hook and define the initial value(s) for our form, call our formschema to define the schema inside the useFormik hook, make a post request with our new user value, and finally reset the form.&lt;/p&gt;

&lt;p&gt;Let’s break this process up into pieces:&lt;br&gt;
&lt;strong&gt;1) First, declare a variable and assign the useFormik hook to it:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddGoal({ handleGoal }){
    const formschema = yup.object().shape({
        goal: yup.string().required("Please enter a goal with at least 1 character.").max(20)
    })

    const formik = useFormik({})

    //Rest of code

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2) Define initial values for the form:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddGoal({ handleGoal }){
    const formschema = yup.object().shape({
        goal: yup.string().required("Please enter a goal with at least 1 character.").max(20)
    })

    const formik = useFormik({
        initialValues: {
            goal: ""
        }, 
    })

    //Rest of code

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

&lt;/div&gt;



&lt;p&gt;(Make sure to include a comma at the end of your initialValues object).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3) Use formschema when adding the validationSchema key:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddGoal({ handleGoal }){
    const formschema = yup.object().shape({
        goal: yup.string().required("Please enter a goal with at least 1 character.").max(20)
    })

    const formik = useFormik({
        initialValues: {
            goal: ""
        },
        validationSchema: formschema,
    })

    // Rest of code

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4) Give onSubmit a function with the “values” and “{ resetForm }” arguments:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddGoal({ handleGoal }){
    const formschema = yup.object().shape({
        goal: yup.string().required("Please enter a goal with at least 1 character.").max(20)
    })

    const formik = useFormik({
        initialValues: {
            goal: ""
        },
        validationSchema: formschema,
        onSubmit: (values, { resetForm })=&amp;gt;{}
    })

    // Rest of code

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

&lt;/div&gt;



&lt;p&gt;Inside onSubmit we will create our POST request (which we will &lt;em&gt;not&lt;/em&gt; be going into detail on how to build in this article). However, in our second ".then()", we will call “resetForm(‘’)” in order to clear the form once a user has submitted it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function AddGoal({ handleGoal }){
    const formschema = yup.object().shape({
        goal: yup.string().required("Please enter a goal with at least 1 character.").max(20)
    })

    const formik = useFormik({
        initialValues: {
            goal: ""
        },
        validationSchema: formschema,
        onSubmit: (values, { resetForm })=&amp;gt;{

            const configObj = {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(values)
            }
            const url = `http://localhost:5555/goals`
            fetch(url, configObj)
            .then(r=&amp;gt;r.json())
            .then(goalObj =&amp;gt; {
                handleGoal(goalObj);
                resetForm("");
            })
        }
    })

//Rest of code

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

&lt;/div&gt;



&lt;p&gt;And there you have it! We have finished creating our formik function! That just leaves us one last thing to do:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Update the JSX to use Formik&lt;/strong&gt;&lt;br&gt;
(Our JSX before updating:)&lt;br&gt;
&lt;/p&gt;

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

return(
        &amp;lt;div&amp;gt;
            &amp;lt;form onSubmit= &amp;gt;
                &amp;lt;input 
                    id="goal"
                    name="goal"
                    type="text" 
                    value=
                    placeholder = "Add A New Goal"
                    onChange=
                /&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
                &amp;lt;button type="submit"&amp;gt;Add My Goal&amp;lt;/button&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
                &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
            &amp;lt;/form&amp;gt;
        &amp;lt;/div&amp;gt;
    )
};

export default AddGoal

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let’s update the following items in our JSX:
Update "onSubmit" with Formik:
&lt;/li&gt;
&lt;/ul&gt;

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

return(
        &amp;lt;div&amp;gt;
          &amp;lt;form onSubmit={formik.handleSubmit}&amp;gt;

          // Rest of code

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;(Inside our input tag) update "value" with Formik:
&lt;/li&gt;
&lt;/ul&gt;

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

return(
        &amp;lt;div&amp;gt;
          &amp;lt;form onSubmit={formik.handleSubmit}&amp;gt;
                &amp;lt;input 
                    id="goal"
                    name="goal"
                    type="text" 
                    value={formik.values.goal}
                    placeholder = "Add A New Goal"
                    onChange=
                /&amp;gt;

          // Rest of code

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;(Still inside our input tag) update "onChange" with Formik:
&lt;/li&gt;
&lt;/ul&gt;

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

return(
        &amp;lt;div&amp;gt;
          &amp;lt;form onSubmit={formik.handleSubmit}&amp;gt;
                &amp;lt;input 
                    id="goal"
                    name="goal"
                    type="text" 
                    value={formik.values.goal}
                    placeholder = "Add A New Goal"
                    onChange={formik.handleChange}
                /&amp;gt;

          // Rest of code

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let’s circle back to when we demonstrated the validation errors earlier in this article and finish setting that up by creating styled errors in a &lt;p&gt; tag:
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

    return(
        &amp;lt;div&amp;gt;
            &amp;lt;form onSubmit={formik.handleSubmit}&amp;gt;
                &amp;lt;input 
                    id="goal"
                    name="goal"
                    type="text" 
                    value={formik.values.goal}
                    placeholder = "Add A New Goal"
                    onChange={formik.handleChange}
                /&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
                &amp;lt;button type="submit"&amp;gt;Add My Goal&amp;lt;/button&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
                &amp;lt;p style={{ color: "red" }}&amp;gt;{formik.errors.goal}&amp;lt;/p&amp;gt;
                &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
            &amp;lt;/form&amp;gt;
        &amp;lt;/div&amp;gt;
    )
};

export default AddGoal

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

&lt;/div&gt;



&lt;p&gt;And that’s it! We've just walked through how to easily create Formik/Yup validations in code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap:
&lt;/h2&gt;

&lt;p&gt;In case you ever need a quick reference of Formik and Yup in the future, here are the steps you need to follow to get your forms validating properly:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Import the necessary libraries to get Formik and Yup running.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Create a function for your Yup schema.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Create a function that calls the useFormik hook. Inside the hook, define:&lt;/strong&gt;&lt;br&gt;
    1) initialValues.&lt;br&gt;
    2) validationSchema&lt;br&gt;
    3) onSubmit [hand it a function with ‘values’ and { resetForm }. Make your POST request and call resetForm(“”) to clear input].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Update your JSX to use Formik:&lt;/strong&gt;&lt;br&gt;
    1) In your &lt;em&gt;form&lt;/em&gt; tag, add formik.handleSubmit to ‘onSubmit’.&lt;br&gt;
    2) In your &lt;em&gt;input&lt;/em&gt; tag, add formik.values.(goal) to ‘value’.&lt;br&gt;
    3) In your &lt;em&gt;input&lt;/em&gt; tag, add formik.handleChange to ‘onChange’.&lt;br&gt;
    4) Create a &lt;em&gt;paragraph&lt;/em&gt; tag with styling for formik.errors.(goal).&lt;/p&gt;

&lt;h3&gt;
  
  
  Happy Coding!
&lt;/h3&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;*Formik: &lt;a href="https://formik.org/" rel="noopener noreferrer"&gt;https://formik.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*Yup: &lt;a href="https://www.npmjs.com/package/yup" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/yup&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Flatiron School's Phase-4 Formik and Form Validation Lesson was also used for reference by the author, though not quoted. For more information on Flatiron's School, please visit their &lt;a href="https://flatironschool.com/welcome-to-flatiron-school/?utm_source=Google&amp;amp;utm_medium=ppc&amp;amp;utm_campaign=12728169839&amp;amp;utm_content=127574231184&amp;amp;utm_term=flatiron%20school&amp;amp;uqaid=513799628798&amp;amp;CjwKCAjwtNi0BhA1EiwAWZaANNwq2Lzfa51pn82WxMSgpEgdkEM9-cJAR1XptO4OqOfZExYj8Gj1PBoCkNcQAvD_BwE&amp;amp;gad_source=1&amp;amp;gclid=CjwKCAjwtNi0BhA1EiwAWZaANNwq2Lzfa51pn82WxMSgpEgdkEM9-cJAR1XptO4OqOfZExYj8Gj1PBoCkNcQAvD_BwE" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How To Write Basic List Comprehension In Python</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Thu, 09 May 2024 20:37:50 +0000</pubDate>
      <link>https://dev.to/deborah/how-to-write-basic-list-comprehension-in-python-5a4o</link>
      <guid>https://dev.to/deborah/how-to-write-basic-list-comprehension-in-python-5a4o</guid>
      <description>&lt;p&gt;Welcome back, fellow coders! &lt;br&gt;
Today we are going to simplify list comprehension so you can feel confident when writing them.&lt;/p&gt;

&lt;p&gt;“List Comprehension” sounds like a massive term when you are first learning how to write the syntax (at least that's how I felt when I started learning) but don’t worry, soon you will be writing them like second nature!&lt;/p&gt;
&lt;h2&gt;
  
  
  What Is List Comprehension?
&lt;/h2&gt;

&lt;p&gt;Put simply, list comprehension is a powerful tool in Python: It uses a &lt;code&gt;for&lt;/code&gt; loop and returns a list object. And ALL this happens in one line. (You can think of list comprehension as the JavaScript equivalent of 'find' 'filter' AND 'map').&lt;/p&gt;

&lt;p&gt;Wow... that does sound powerful!&lt;br&gt;
Now let's learn how to write them!&lt;/p&gt;
&lt;h2&gt;
  
  
  List Comprehension Syntax
&lt;/h2&gt;

&lt;p&gt;When I first started learning list comprehension (after working in JavaScript), I felt like the syntax was backwards and confusing. But if we break the syntax into smaller parts we find that it is quite user-friendly.&lt;/p&gt;

&lt;p&gt;In fact, list comprehensions are actually easy to talk through in plain English. For example, the following list comprehension...:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greeting = [print("Hi") for person in my_school] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... would sound something like this in an English sentence:&lt;br&gt;
"print 'Hi' for each person in my_school"&lt;/p&gt;

&lt;p&gt;The above example is a trivial one but it shows the basic layout of our list comprehension syntax. Now let's get more in-depth and discuss each part of the syntax by building our own list comprehension.&lt;/p&gt;

&lt;p&gt;Our finished example will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [1,2,3,4]

my_numbers = [(num*2) for num in numbers] 

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

&lt;/div&gt;



&lt;p&gt;If we were to run this code in our console, we would expect each number to be multiplied by 2, and the following list to be returned when we call &lt;code&gt;my_numbers&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2, 4, 6, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Because a list comprehension returns a list, we need to do something with that list (assign it to a variable or return it). In the above example, we assigned it to the variable &lt;code&gt;my_numbers&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_numbers =  #See the next step
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Since we are returning a list, we place our list comprehension inside of square brackets.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_numbers = []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The first thing&lt;/strong&gt; we put inside the square brackets is what we want to return. In our example, we want our &lt;code&gt;num&lt;/code&gt; multiplied by 2.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_numbers = [(num*2)  ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To finish, we write our &lt;code&gt;for&lt;/code&gt; loop. In our example, this was &lt;code&gt;for num in numbers&lt;/code&gt; where &lt;code&gt;num&lt;/code&gt; represents the current item in the list, and &lt;code&gt;numbers&lt;/code&gt; is what we are iterating through.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_numbers = [(num*2) for num in numbers]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! That's how simple list comprehensions are to write!&lt;br&gt;
First, assign square brackets to a variable, or place them in a return statement...&lt;br&gt;
Second, add what you want to be returned by the &lt;code&gt;for&lt;/code&gt; loop...&lt;br&gt;
And third, finish the list comprehension with the &lt;code&gt;for&lt;/code&gt; loop itself.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tips For Learning List Comprehension
&lt;/h2&gt;

&lt;p&gt;Before we dive deeper into more complex list comprehensions, let's pause to discuss strategies for learning list comprehension.&lt;/p&gt;

&lt;p&gt;When I was first learning list comprehension, there were two particular strategies that my professor encouraged me to hone:&lt;/p&gt;
&lt;h4&gt;
  
  
  1. While learning list comprehension, write out your &lt;code&gt;for&lt;/code&gt; loops before writing your list comprehension.
&lt;/h4&gt;

&lt;p&gt;While learning the formatting of list comprehension syntax, I found it extremely helpful to write out my &lt;code&gt;for&lt;/code&gt; loops before I wrote my list comprehension.&lt;br&gt;
For example, I might write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for num in numbers:
    return num * 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I would translate this into my list comprehension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_numbers = [(num*2) for num in numbers]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For simple examples like the one above, you might not need to write out your &lt;code&gt;for&lt;/code&gt; loop beforehand, but once your code becomes more complicated you will see the value in doing so.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. The last thing you add to a list comprehension is what you want the &lt;code&gt;for&lt;/code&gt; loop to return.
&lt;/h4&gt;

&lt;p&gt;When you are writing out your list comprehension, and are inside the square brackets, give yourself some space and write your &lt;code&gt;for&lt;/code&gt; loop before writing what you want it to return. For example, first you would write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_numbers = [     for num in numbers]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then finish off the list comprehension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_numbers = [(num*2) for num in numbers]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will help you return the correct thing when you are working with a more complex list comprehension. It will also allow you to work through the problem in your head in English first. For example, you might say to yourself: "For num in numbers, (now add your return statement) multiply num by 2."  &lt;/p&gt;

&lt;h2&gt;
  
  
  List Comprehensions And Conditional Statements
&lt;/h2&gt;

&lt;p&gt;Hooray! Now you are comfortable with the basic syntax and have a few strategies to practice while learning how to write list comprehensions.&lt;br&gt;
What now? &lt;br&gt;
Before you move on to writing your own code, let's discuss one more important way of writing list comprehension: including conditionals.&lt;/p&gt;

&lt;p&gt;Let's continue using our numbers example from above but this time let's make sure that we are only multiplying even numbers. How would we write this in our list comprehension? &lt;/p&gt;

&lt;p&gt;By adding our conditional statement after the &lt;code&gt;for&lt;/code&gt; loop.&lt;br&gt;
If we were to write out our &lt;code&gt;for&lt;/code&gt; loop &lt;em&gt;before&lt;/em&gt; writing our list comprehension, our &lt;code&gt;for&lt;/code&gt; loop and &lt;code&gt;if&lt;/code&gt; statement might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# for num in numbers:
#     if num % 2 == 0:
#        num * 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We would then add our if statement to the list comprehension like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [1,2,3,4]

my_numbers = [(num*2) for num in numbers if num % 2 == 0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the "if" comes after the &lt;code&gt;for&lt;/code&gt; loop we learned about earlier. &lt;br&gt;
Simply put: add the word &lt;code&gt;if&lt;/code&gt;, then add your condition statement.&lt;/p&gt;
&lt;h2&gt;
  
  
  Example Of List Comprehension In A Project
&lt;/h2&gt;

&lt;p&gt;Now you know the basics and are ready to start writing your own code, but before you do, let's look at a real-world example.&lt;/p&gt;

&lt;p&gt;Recently I built a Python project with a CLI menu that stored a list of U.S. States and cities that I want to visit. I can add a State, save it, and then add cities to the State.&lt;br&gt;
I can also list all the States to see what States have already been saved, and then choose one to view details about the cities saved in that State:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1szskelxovtqwk7xzj73.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1szskelxovtqwk7xzj73.png" alt="My CLI Menu showing the cities and their associated activities saved in Hawaii" width="800" height="673"&gt;&lt;/a&gt;&lt;br&gt;
(This is my CLI menu. In it, I have chosen to view Hawaii (which I saved prior), and am currently viewing the cities (both are Maui) that are saved to that State of Hawaii. Next to each "city" is the wishlist activity I would like to do in that city/island).&lt;/p&gt;

&lt;p&gt;The cities are stored in a database. In order to list all the cities, I need to "fetch" them from the database and make sure they match the State we are currently viewing.&lt;br&gt;
Let's take a look under the hood to see how I accomplished this with list comprehension. In the following example I used list comprehension in &lt;code&gt;city_list&lt;/code&gt; to iterate through the cities and find the ones that matched the State:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def list_cities(state):
    cities = City.get_all()
    city_list = [city for city in cities if city.state_id == state.id]
    separator()
    print(f'You are in {state.name}! (Or atleast in your {state.name} Menu...)\n')
    if len(city_list) &amp;gt; 0:
        print("Your cities include:")
        for i, city in enumerate(city_list, start=1):
            print(f'{i}. {city.name}: {city.attraction}')
    else:
        print("Please add a city!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For explanation purposes, let's take this line by line:&lt;br&gt;
I am currently in a user-facing method called &lt;code&gt;list_cities()&lt;/code&gt; that is handed a &lt;code&gt;state&lt;/code&gt; object which I will use in a few lines of code to verify that the cities belong to this State:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def list_cities(state):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;cities&lt;/code&gt; I am grabbing all my cities in the database using the &lt;code&gt;get_all()&lt;/code&gt; method (which is in my City class in a different file, but its main purpose is to get all the cities in my database and turn them into objects that I can use):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    cities = City.get_all()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now for the list comprehension in &lt;code&gt;city_list&lt;/code&gt; where I use a &lt;code&gt;for&lt;/code&gt; loop to iterate through &lt;code&gt;cities&lt;/code&gt; and check if the foreign key in my city (&lt;code&gt;city.state_id&lt;/code&gt;) is equivalent to the primary key in the &lt;code&gt;state&lt;/code&gt; object (that was handed to &lt;code&gt;list_cities()&lt;/code&gt;). (The &lt;code&gt;state&lt;/code&gt; primary key is &lt;code&gt;state.id&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    city_list = [city for city in cities if city.state_id == state.id]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This list comprehension will now return a list of cities that belong to a certain State! In my code I then went on to use that list to format a user-view list of cities in my CLI.&lt;/p&gt;

&lt;p&gt;This has been a real-world example of list comprehension. And for those of you who are curious about the rest of my code, let's take a look at it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    separator()    
    #A line used to help format the user-view.
    print(f'You are in {state.name}! (Or atleast in your {state.name} Menu...)\n')   
    #A print statement used to orient the user while we transition from our State menu to our City menu.
    if len(city_list) &amp;gt; 0:   
    #If our list has at least one city, perform the following actions:
        print("Your cities include:")
        #Since we should never show database id's directly to a user, we use enumerate() to place numbers before each city when we display the cities and their associated attractions.
        for i, city in enumerate(city_list, start=1):    
            print(f'{i}. {city.name}: {city.attraction}')
    else:    
    #If your list does not have any cities in it, print this:
        print("Please add a city!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Once you become familiar with list comprehension you may find (just like I did) that they are actually kind of fun to write! But beyond how "fun" it is to write them, they are a powerful tool that will take your Python skills to the next level.&lt;br&gt;
So the next time you write a list comprehension remember to break it into parts like we did in this article in order to keep its syntax simple in your head.&lt;/p&gt;

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

&lt;p&gt;(And a special thanks to my Python instructor Nancy!)&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorials</category>
      <category>backend</category>
    </item>
    <item>
      <title>How to use State in React</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Wed, 13 Mar 2024 02:46:02 +0000</pubDate>
      <link>https://dev.to/deborah/how-to-use-state-in-react-2pah</link>
      <guid>https://dev.to/deborah/how-to-use-state-in-react-2pah</guid>
      <description>&lt;p&gt;Welcome back, fellow coders!&lt;br&gt;
Today we're going over the basics of a React Hook called useState. useState is a powerful tool that we can use to make the data more dynamic while building applications.&lt;br&gt;
It might take you a little practice before this hook is second nature, but don't worry, in the long run it will make your job so much easier!&lt;/p&gt;
&lt;h1&gt;
  
  
  What is useState?
&lt;/h1&gt;

&lt;p&gt;useState is a hook that returns an array when invoked. The array returns two powerful elements (in the examples in this article, we will use destructuring to assign those elements to variables): First up in the array is our State that will hold our dynamic data. We can update this variable with the second item in our array: a "setter" function (we invoke this function and it "sets" State to whatever we specify). And before we start using this array, we need to take a few steps:&lt;/p&gt;
&lt;h1&gt;
  
  
  1. Import {useState}:
&lt;/h1&gt;

&lt;p&gt;As mentioned earlier, useState is a React Hook and before we can use it, we first need to import it into the component we wish to use it with. We do that like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from “react”;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;[Notice how useState is placed inside the curly braces (object destructuring)].&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Declare State Variables Inside Your component:
&lt;/h1&gt;

&lt;p&gt;Next, we need to declare our State variables inside the component we want to use it in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function OurComponent(){
   const [variable, setVariable] = useState("initial value goes here");

//more code to come...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;[We've declared our State variables, but in the example above, it's important for you to understand that we are using &lt;strong&gt;array destructuring&lt;/strong&gt;, which, in short, is a way we can "separate" values from arrays into separate variables to use].&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the above code snippet, we can see how the array's first item is a variable named "variable":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   const [variable, setVariable] = useState("initial value goes here");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will use this variable later in our code just like we would any other variable.&lt;/p&gt;

&lt;p&gt;Next, we've declared our setter function which will allow us to "update" the State variable ("setVariable"):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   const [variable, setVariable] = useState("initial value goes here");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we've set an initial value for our State:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   const [variable, setVariable] = useState("initial value goes here");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(Intial values can be strings, objects, etc.)&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Calling Our Stateful Variable:
&lt;/h1&gt;

&lt;p&gt;As mentioned earlier, we can use the State variable like a normal variable. When we call it in our JSX, we simply can put curly braces around it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;{variable}&amp;lt;h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will display the following h1 in our browser:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq62kdemz7ch6hzm3l3ww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq62kdemz7ch6hzm3l3ww.png" alt="State Variable Displayed In Browser" width="399" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By following these simple steps, we have opened our application to many possibilities. Now let's look at an example of how State can benefit us.&lt;/p&gt;
&lt;h1&gt;
  
  
  useState Example:
&lt;/h1&gt;

&lt;p&gt;Recently I was creating a simple application wherein a user could access a form that would allow them to add key information about a Bible verse, then save it for later reference in two other pages of the application.&lt;br&gt;
By using State I was able to create a controlled form that allowed my application to "grab" the value from an input field and hold it in State.&lt;br&gt;
Inside my Form component, these were my Stateful variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const [testament, setTestament] = useState("");
    const [reference, setReference] = useState("");
    const [verse, setVerse] = useState("");
    const [url, setUrl] = useState("");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's take a look at my input fields inside my "form" tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form onSubmit={handleSubmit}&amp;gt;
      &amp;lt;input type="text" value={testament} placeholder={"Type 'Old' or 'New'"} onChange={(e)=&amp;gt; setTestament(e.target.value)} className="form"&amp;gt;&amp;lt;/input&amp;gt;
       &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
       &amp;lt;input type="text" value={reference} placeholder={"Example: John 3:16"} onChange={(e)=&amp;gt; setReference(e.target.value)} className="form"&amp;gt;&amp;lt;/input&amp;gt;
       &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
       &amp;lt;input type="text" value={verse} placeholder={"Example: For God so loved the world... "} onChange={(e)=&amp;gt; setVerse(e.target.value)} className="form"&amp;gt;&amp;lt;/input&amp;gt;
       &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
       &amp;lt;input type="text" value={url} placeholder={"Paste the URL here"} onChange={(e)=&amp;gt; setUrl(e.target.value)} className="form"&amp;gt;&amp;lt;/input&amp;gt;
       &amp;lt;br&amp;gt;&amp;lt;/br&amp;gt;
       &amp;lt;button&amp;gt;Add My Verse!&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a closer look at the input fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;input type="text" value={testament} placeholder={"Type 'Old' or 'New'"} onChange={(e)=&amp;gt; setTestament(e.target.value)} className="form"&amp;gt;&amp;lt;/input&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few important things to note: first, the input's "value" is set to our State variable. Second, we have an "onChange" event handler that passes the callback function that calls the setter function "setTestament" and passes it "e.target.value" (which is whatever a user types into the input field). These two items, along with the declared State variable of "testament" that we defined earlier, allow us to dynamically update the "testament" variable so it reflects whatever a user has typed into the input form.&lt;/p&gt;

&lt;p&gt;Since State is holding the value for "testament", we can later call the "testament" variable in our code. &lt;br&gt;
For example, in my Bible Verse Application, I wanted to make a POST request, and thanks to State, I could now use the values stored in State to create a &lt;em&gt;body&lt;/em&gt; for my POST request. Take a look at how I used the values stored in State to create my POST request's &lt;em&gt;body&lt;/em&gt;. You'll notice that I named the State variables the same thing as the keys in my API objects, so some of the keys/value pairs look like they are repetitive. In reality, the &lt;em&gt;key&lt;/em&gt; is the word on the left, and the State variable is being called as the value on the right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        const configObj = {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify(
                {
                    testament: testament,
//The API Object Keys and the Stateful variables are named the same thing. The Keys are on the left, and the Stateful variable is being called on the right.
                    reference: reference,
                    verse: verse,
                    url: url
                }
            )
        }

        fetch("http://localhost:3000/verses", configObj)
        .then(r=&amp;gt;r.json())
        .then(verseObj =&amp;gt; {
            addVerse(verseObj);
        })
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the information I stored in my State variables (which was retrieved from the input fields) is no longer needed in my component (because an Object has been added to the API with the information), I can clear the input fields by simply calling the setter functions and setting them to an empty string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; fetch("http://localhost:3000/verses", configObj)
        .then(r=&amp;gt;r.json())
        .then(verseObj =&amp;gt; {
            addVerse(verseObj);

//Now that I am sure my POST request was successful, I can clear the values stored in State so they will be ready for new user input:
            setTestament("");
            setReference("");
            setVerse("");
            setUrl("");
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is just one example of how useful and time-saving useState can be while we code. However, useState does have an idiosyncrasy that we need to be aware of when we begin to use State.&lt;/p&gt;

&lt;h1&gt;
  
  
  useState Is Asynchronous:
&lt;/h1&gt;

&lt;p&gt;When using State, we need to be aware that &lt;strong&gt;whenever our setter function is called, the component will re-render&lt;/strong&gt;. Take a moment to think about what you just read. &lt;br&gt;
useState's ability to re-render the component is a good thing: it allows us to update the DOM without making the user manually refresh the page to show an updated DOM. &lt;br&gt;
You'll want to remember that updating State is asynchronous. This means that React will internally keep track of the new value when a setter function is called, but it won’t update the State "variable" until after the component re-renders. Because of asynchrony, the rest of the component will finish running before the re-rendering occurs. &lt;br&gt;
Okay, let's take a look at an example to make sure we understand: If we have a State variable like in the example below, and we call the setter functions, take a look at when the console.logs print.&lt;/p&gt;

&lt;p&gt;Our code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useState} from "react";

function OurComponent (){
    const [variable, setVariable] = useState("")

    console.log("1: ", variable)

    function handleChange(e){
        setVariable(e)
        console.log("2: ", variable)
    }

    console.log("3: ", variable)

    return(
        &amp;lt;div&amp;gt;
            &amp;lt;input type="text" value={variable} onChange={e =&amp;gt; handleChange(e.target.value)}&amp;gt;&amp;lt;/input&amp;gt;
            &amp;lt;h3&amp;gt;{variable}&amp;lt;/h3&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Our initial console.logs:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07zgxwmymhroa306g0qt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07zgxwmymhroa306g0qt.png" alt="Intial Console.logs" width="203" height="94"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our next set of console.logs after typing in 1 letter:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9fuvxohp9s1yq1pcio8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9fuvxohp9s1yq1pcio8p.png" alt="1 Character Console.logs" width="199" height="72"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The following set of console.logs after typing in our 2nd letter:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F548ei0xif8xtrg5hmtt1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F548ei0xif8xtrg5hmtt1.png" alt="2 Character Console.logs" width="204" height="67"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's take a closer look at these console.logs to truly understand the asynchronous behavior of State:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In our first console.logs we did not see our "2" console.log. This was expected, as handleChange had not yet been called, so let's move on.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07zgxwmymhroa306g0qt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07zgxwmymhroa306g0qt.png" alt="Intial Console.logs" width="203" height="94"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our "2" console.log was the first to be called, but even though we had updated State in the prior line in our code, the change was not reflected in the "2" console.log. What we learned from our first console.logs was that "1" and "3" are called whenever the page re-renders and we do indeed see them with the update value after re-rendering.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9fuvxohp9s1yq1pcio8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9fuvxohp9s1yq1pcio8p.png" alt="1 Character Console.logs" width="199" height="72"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So what happens to our "2" console.log if we type another character in our input field? We see that "2" is updated with the value from our previous entry... but "1" and "3" have re-rendered once again and are displaying the correct value that is stored in our State "variable".&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F548ei0xif8xtrg5hmtt1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F548ei0xif8xtrg5hmtt1.png" alt="2 Character Console.logs" width="204" height="67"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Let's Recap:
&lt;/h1&gt;

&lt;p&gt;Congratulations! You now have all the tools to get started in useState. In this article, we discussed a little about why we like useState and then went over the necessary code to get useState working in your application. We also delved into an example of a controlled form that utilized State to keep track of input fields and send a POST request to an API. Finally, we covered one of useState’s unique attributes that can cause issues if we aren’t careful, so we'll remember that State is asynchronous.&lt;/p&gt;

&lt;p&gt;You are all ready to begin coding with useState, but in case you need a cheat sheet to get started:&lt;/p&gt;

&lt;h4&gt;
  
  
  Import the useState hook:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from “react”;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Declare the State variables inside the Component:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function OurComponent(){
   const [variable, setVariable] = useState("initial value goes here");

//The rest of your code goes here...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Call the setter function when you want to update the State variable:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setVariable("new value goes here");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Remember that State is asynchronous.
&lt;/h3&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>find(), filter(), and map() Explained As Party Games</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Tue, 07 Nov 2023 16:59:16 +0000</pubDate>
      <link>https://dev.to/deborah/find-filter-and-map-explained-as-party-games-ne9</link>
      <guid>https://dev.to/deborah/find-filter-and-map-explained-as-party-games-ne9</guid>
      <description>&lt;p&gt;Hello fellow coders! When beginning my journey of learning array iteration methods (including find(), filter(), and map()), I struggled to keep track of which method did what, and how to use each method. Can you relate? Well, today we’re going to turn these methods into analogies that you can remember! First I’ll tell you a story and then we’ll relate it to the method. I’ll include some simple code examples, so open Replit or a coding project and follow along!&lt;/p&gt;

&lt;h1&gt;
  
  
  What are methods and why are they important?
&lt;/h1&gt;

&lt;p&gt;The word “method” was hard for me to understand at first, but we can think of “methods” as “actions” or “steps”.&lt;br&gt;
The methods we’re talking about today can seem hard to grasp at first, so let’s orient ourselves with &lt;em&gt;why&lt;/em&gt; we want to learn them. &lt;br&gt;
As software engineers, we want to write code efficiently and these methods help us reach that goal. Each of these methods is important because they provide us with cleaner code that is easier to read. Also, we often can write fewer lines of code when we use these methods.&lt;/p&gt;

&lt;p&gt;Before we get started with the examples below, it's important to remember that none of these methods are Destructive (they WON'T mutate the original array). Please keep that in mind as you read through the stories below because the stories aren't perfect metaphors.&lt;/p&gt;

&lt;p&gt;Now let's learn these methods!&lt;/p&gt;
&lt;h1&gt;
  
  
  find() Method
&lt;/h1&gt;

&lt;p&gt;SURPRISE! It’s your birthday and your best friend organized a surprise party! You’ve spent the last couple of hours eating cake, opening presents, and socializing, but now it’s time to play party games! There are some fun party games planned and the first one is a scavenger hunt!&lt;/p&gt;

&lt;p&gt;This is going to be a quick game because everyone only has 1 item to find: a red balloon. You are going to have to move quickly and grab the FIRST red balloon you see (or someone else will find a red balloon first!). The referee says “Go!”, and everyone scurries away. In your mind, you are ONLY looking for red balloons and huzzah! You see one! You grab it and run back to the referee. You WON! As everyone congratulates you, you look around and realize that the room is full of red balloons, but you only SAW the first red balloon you come across. All of a sudden, you realize that you have just used the find() method without realizing it:&lt;br&gt;
We can imagine that all the balloons in the area where you looked could be translated into an array like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const balloonArray = [
  {color: 'blue', condition: 'inflated', inTrash: 'no', number: 1},
  {color: 'red', condition: 'inflated', inTrash: 'no', number: 2},
  {color: 'yellow', condition: 'inflated', inTrash: 'no', number: 3},
  {color: 'green', condition: 'inflated', inTrash: 'no', number: 4},
  {color: 'red', condition: 'inflated', inTrash: 'no', number: 5},
  {color: 'blue', condition: 'inflated', inTrash: 'no', number: 6},
  {color: 'yellow', condition: 'inflated', inTrash: 'no', number: 7},
  {color: 'blue', condition: 'inflated', inTrash: 'no', number: 8},
  {color: 'red', condition: 'inflated', inTrash: 'no', number: 9},
  {color: 'green', condition: 'inflated', inTrash: 'no', number: 10}
];

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

&lt;/div&gt;



&lt;p&gt;And you were acting like the find() method, looking for the FIRST red balloon you came across:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let findRedBalloon = balloonArray.find(balloonObject =&amp;gt; {
  if(balloonObject.color === "red"){
    return balloonObject;
  };
});

console.log(findRedBalloon);

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

&lt;/div&gt;



&lt;p&gt;Let's look at what our console.log() returned in our browser's console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdb3nq2zdbzpzyq7uvzoy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdb3nq2zdbzpzyq7uvzoy.png" alt="findRedBalloon console.log() result" width="800" height="29"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is easy to see from our console.log() that the find() method used in findRedBalloon is ONLY returning the 1st red balloon in our array. findRedBalloon returned the red balloon with the key-value pair of "number: 2". We can verify that this is indeed the first red balloon in the array by looking at our balloonArray.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's break down the find() method more in-depth by coding together
&lt;/h3&gt;

&lt;p&gt;First, we'll declare a variable, then call the find() method on the balloonArray (the array holding all the balloons):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let findRedBalloon = balloonArray.find();

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

&lt;/div&gt;



&lt;p&gt;Inside our find() method we need a callback function, and we'll write one here as an arrow function so it is even cleaner and easier to read. While we're at it, we'll also include a parameter named "balloonObject" so it reminds us that we are going to be looking through the balloonArray at each individual balloon object inside it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let findRedBalloon = balloonArray.find(balloonObject =&amp;gt; {
//our next code goes here
});

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

&lt;/div&gt;



&lt;p&gt;Our code is looking great, but now we need to define what we are doing with the balloonObject. Since we are using find() to find the 1st red balloon we come across, let's use an “if” statement to find a red balloon and return the entire balloonObject. In order to do this, we'll use dot notation to access the "color" value stored inside our balloonObject:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let findRedBalloon = balloonArray.find(balloonObject =&amp;gt; {
  if(balloonObject.color === "red"){
    return balloonObject;
  };
});

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

&lt;/div&gt;



&lt;p&gt;And now you understand the find() method! It's easy to imagine that instead of just returning the balloonObject like we did above, you could run the object through a function, or "do" something more complicated with it at this point.&lt;/p&gt;

&lt;h1&gt;
  
  
  filter() Method
&lt;/h1&gt;

&lt;p&gt;Back at your birthday party, people have barely stopped congratulating you on your win when the referee announces the next game. Each competitor starts with a dumpster full of balloons. The competitor will pull a balloon out of the dumpster. If the balloon is red, they will run across the room and place it in a basket. If it isn't red, the competitor will throw it on the floor out of the way. &lt;/p&gt;

&lt;p&gt;As soon as the referee says "Go!", you start quickly working through the dumpster, evaluating one balloon at a time. If it's red, you get it to the basket as quickly as possible. If it's not red, you throw it out of your way. Since you're so focused, you get done before anyone else! As you wait by your basket of red balloons while your friends finish sorting through their balloons, you suddenly realize that you have just acted like the filter() method, looking for all the red balloons you came across in your dumpster (array):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let filterRedBalloons = balloonArray.filter(balloonObject =&amp;gt; {
  if(balloonObject.color === "red"){
    return balloonObject;
  };
});

console.log(filterRedBalloons);

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

&lt;/div&gt;



&lt;p&gt;Let's look at what our console.log() returned in our browser's console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2bemlfx1cnb3wjcfi2a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2bemlfx1cnb3wjcfi2a.png" alt="filterRedBalloons console.log() result" width="800" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we check our balloonArray, we can verify that filterRedBalloons did indeed return all the red balloons in the array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's break down the filter() method more in-depth by coding together
&lt;/h3&gt;

&lt;p&gt;To make this example easier to understand, we’ll assume that the balloons in your dumpster were the same as the balloonArray we used in the previous example. &lt;/p&gt;

&lt;p&gt;The process you went through to sort balloons would be the find() method: go through the ENTIRE array and find ALL the red balloons. Don't stop until you have sorted all the balloons.&lt;/p&gt;

&lt;p&gt;In code, we would first declare a variable, then call the filter() method on the balloonArray like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let filterRedBalloons = balloonArray.filter();

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

&lt;/div&gt;



&lt;p&gt;Inside our filter() method we need a callback function, and we'll once again write ours as an arrow function. While we're at it, we'll make a parameter named "balloonObject":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let filterRedBalloons = balloonArray.filter(balloonObject =&amp;gt; {
//our next code goes here
});

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

&lt;/div&gt;



&lt;p&gt;Just like we did in the find() example, we're now going to write code to do something with the balloonObject. In our example we want to collect all the red balloons, so we'll use an “if” statement and dot notation to access the color value stored inside our balloonObject and compare it to "red":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let filterRedBalloons = balloonArray.filter(balloonObject =&amp;gt; {
  if(balloonObject.color === "red"){
    return balloonObject;
  };
});

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

&lt;/div&gt;



&lt;p&gt;At this point, instead of just returning balloonObject, we could do something with the balloonObject. But for this example, let's end here and move on to learn about map()!&lt;/p&gt;

&lt;h1&gt;
  
  
  map() Method
&lt;/h1&gt;

&lt;p&gt;It’s time for the final game of the party, and in hindsight, you realize the party planners were using this game to clean up the party...&lt;br&gt;
The game goes like this: Everyone has to pick up 10 balloons off the floor (it doesn't matter what color, so long as you pick up 10). To win the game, you will need to pop all the balloons as quickly as possible. Then you'll need to pick up all the popped pieces and throw them into the trashcan. Whoever pops 10 balloons first wins and gets to take home all the leftover cupcakes as their prize!&lt;/p&gt;

&lt;p&gt;You get two teammates for this challenge, and after a quick huddle, your team decides that you will be in charge of grabbing balloons off the floor. Once you have one you'll hand it to your 1st teammate. They'll be in charge of popping the balloon, and your 2nd teammate will pick up the balloon pieces and throw them in the trash.&lt;/p&gt;

&lt;p&gt;The referee says "Go!" and you begin to pick balloons off the floor and hand them to your 1st teammate one at a time. When you hand the balloon to your 1st teammate, she pops it and your 2nd teammate quickly picks up the pieces and runs over to the trashcan to throw them away. You’ve found an efficient system that’s better than your teammates, and before you know it you’ve popped 10 balloons! Your team won! Everyone congratulates you for being undefeated at party games, and while you munch on another cupcake, you start to realize that you and your teammates just did the map() method. You had 10 balloons (your array), that you sorted through individually (your balloonObject). You handed each balloonObject off to your 1st teammate, who, in code, would be a function. That function ran, and then the 2nd teammate (also a function) was passed the balloonObject. Your code would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mapBalloons = balloonArray.map(balloonObject =&amp;gt; {
  popBalloon(balloonObject);
});

function popBalloon(balloonObject){
  balloonObject.condition = "popped";
  throwBalloonAway(balloonObject);
};

function throwBalloonAway(balloonObject){
  balloonObject.inTrash = "yes";
  console.log(balloonObject);
};

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

&lt;/div&gt;



&lt;p&gt;Let's look at what our console.log() returned in our browser's console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fel4s6o6d96gc9fw3v58t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fel4s6o6d96gc9fw3v58t.png" alt="balloonObjects after console.logging" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our console.log() shows us that our "condition" and "inTrash" values have changed in each object (remember, this is a non-destructive method, so the original array has NOT changed). &lt;/p&gt;

&lt;h3&gt;
  
  
  Let's break down the map() method more in-depth by coding together
&lt;/h3&gt;

&lt;p&gt;First, we'll declare a variable, then call the map() method on the balloonArray:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mapBalloons = balloonArray.map();

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

&lt;/div&gt;



&lt;p&gt;Our map() method needs a callback function, so let's provide one in the form of an arrow function, and add a parameter of "balloonObject":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mapBalloons = balloonArray.map(balloonObject =&amp;gt; {
//our next code goes here
});

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

&lt;/div&gt;



&lt;p&gt;Next we're going to call a function that map() will pass each ballonObject individually into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mapBalloons = balloonArray.map(balloonObject =&amp;gt; {
  popBalloon(balloonObject);
});

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

&lt;/div&gt;



&lt;p&gt;And we'll assume we've already written these functions which each change a value inside the object. The important thing to realize here is that map() is passing each individual balloonObject into these functions. We didn't have to write out a for loop or anything!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function popBalloon(balloonObject){
  balloonObject.condition = "popped";
  throwBalloonAway(balloonObject);
};

function throwBalloonAway(balloonObject){
  balloonObject.inTrash = "yes";
};

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

&lt;/div&gt;



&lt;p&gt;(These functions are pretty simple, but you can imagine that you could do some pretty complex things with your own functions).&lt;/p&gt;

&lt;h1&gt;
  
  
  Let's review
&lt;/h1&gt;

&lt;p&gt;Find(), filter(), and map() can be daunting when you are first introduced to them, but I hope these "party game" examples have shown you that at their core they aren't very complicated and can be very helpful.&lt;/p&gt;

&lt;p&gt;So, the next time you use one of these methods, think back to the party games we talked about here and reference the notes below if you need to!&lt;/p&gt;

&lt;h4&gt;
  
  
  find()
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Find the first red balloon&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  filter()
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Collect all the red balloons&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  map()
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Pop all the balloons&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>If_Else If Statements: The Quick Guide For Beginners</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Thu, 10 Aug 2023 15:00:00 +0000</pubDate>
      <link>https://dev.to/deborah/ifelse-if-statements-the-quick-guide-for-beginners-2oof</link>
      <guid>https://dev.to/deborah/ifelse-if-statements-the-quick-guide-for-beginners-2oof</guid>
      <description>&lt;p&gt;&lt;em&gt;(See The Header Image For How An If_Else If Statement Is Basically Written In A Function)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript likes to "solve" If_Else If Statements like flipping through a catalog...
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;...in search of something to buy - "If you don't want thing1, move on to thing2, and if you don't want thing2, move on to thing3, etc. Finally, if nothing else works, just get the basic model". Let's explore this a little more:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Our function starts out by checking the condition with the block of code to the right of the "if". This is our starting place, so it get's the special "if" name. If the condition is true (it matches exactly with the thing we've defined to the right of the ===), the function will return something from this block of code, then exit the function.&lt;/p&gt;

&lt;p&gt;BUT, if the condition is FALSE, (it DOESN'T match exactly with the thing we've defined to the right of the ===), the function will move on and run the "else if" part of the code. If the condition is true (it matches exactly with the thing we've defined to the right of the ===), the function will return something from that block of code, then exit the function.&lt;/p&gt;

&lt;p&gt;We can have multiple "else if" parts of the code - one for each option we want - and if the previous "else if" statement returns false, the next "else if" statements will be checked. This will continue until either 1)one of the "else if" statements returns true, or 2)there are no more "else if" statements left, in which case we move on to the last part of our code:&lt;/p&gt;

&lt;p&gt;If nothing has returned true by the time we reach "else" (notice, this is the last part of the code and is NOT written "else if"), then the "else" part of the code will run and return something from that block of code.&lt;/p&gt;

&lt;p&gt;It is important to remember that SOMETHING will be run: we have many options for what to run &lt;em&gt;if&lt;/em&gt; something matches our "if" or "else if" parts of the code... but if nothing matches these parts, the "else" part of the code WILL run.&lt;/p&gt;

&lt;h2&gt;
  
  
  You can remember when to use If_Else If Statements like this:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;If not Option1 or Option2 or Option3 (etc.), do the Final Option.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>If_Else Statements: The Quick Guide For Beginners</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Thu, 03 Aug 2023 14:00:00 +0000</pubDate>
      <link>https://dev.to/deborah/ifelse-statements-the-quick-guide-for-beginners-509m</link>
      <guid>https://dev.to/deborah/ifelse-statements-the-quick-guide-for-beginners-509m</guid>
      <description>&lt;p&gt;&lt;em&gt;(See The Header Image For How An If_Else Statement Is Basically Written In A Function)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript likes to "solve" If_Else Statements like a simple "do thing 1 OR do thing 2" scenario:
&lt;/h2&gt;

&lt;p&gt;Our function starts out by checking the condition with the block of code to the right of the "if". If the condition is true (it matches &lt;strong&gt;exactly&lt;/strong&gt; with the thing we've defined to the right of the ===), the function will return something from this block of code, then exit the function.&lt;/p&gt;

&lt;p&gt;BUT, if the condition is FALSE, (it DOESN'T match &lt;strong&gt;exactly&lt;/strong&gt; with the thing we've defined to the right of the ===), the function will move on and run the ELSE part of the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We're essentially saying:&lt;/strong&gt; Check the "if" part of the code, and if it matches, run the "if" part of the code. But if it doesn't match, run the "else" part of the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  You can remember when to use If Statements like this:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;If Thing 1 is TRUE, do Thing 1. If Thing 1 is FALSE, do Thing 2.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>If Statements: The Quick Guide For Beginners</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Fri, 28 Jul 2023 01:07:55 +0000</pubDate>
      <link>https://dev.to/deborah/if-statements-the-quick-guide-for-beginners-35bc</link>
      <guid>https://dev.to/deborah/if-statements-the-quick-guide-for-beginners-35bc</guid>
      <description>&lt;p&gt;&lt;em&gt;(See The Header Image For How An If Statement Is Basically Written In A Function)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript likes to "solve" If Statements like a simple true or false scenario:
&lt;/h2&gt;

&lt;p&gt;If the condition we give to a function is true (it matches &lt;strong&gt;exactly&lt;/strong&gt; with the thing we've defined to the right of the ===), the function will return something.&lt;/p&gt;

&lt;p&gt;If the condition we give to a function is false (it DOESN'T match &lt;strong&gt;exactly&lt;/strong&gt; with the thing we've defined to the right of the ===), the function does NOT return anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  You can remember when to use If Statements like this:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;If TRUE, do something. If FALSE, do nothing. That's it.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The Quick Guide To Writing A Function For Beginners</title>
      <dc:creator>Deborah Kurz</dc:creator>
      <pubDate>Tue, 27 Jun 2023 20:15:53 +0000</pubDate>
      <link>https://dev.to/deborah/the-quick-guide-to-writing-a-function-for-beginners-lp8</link>
      <guid>https://dev.to/deborah/the-quick-guide-to-writing-a-function-for-beginners-lp8</guid>
      <description>&lt;p&gt;As beginners to the world of JavaScript and Software Engineering, Functions are often one of the first things we learn how to write.&lt;br&gt;
Below, we will discuss the basic anatomy of a function (the bare bones that act as a framework for building more complex functions).&lt;/p&gt;

&lt;p&gt;As a refresher: Functions are something we can make once, then call on many times later in our code. This means we need to build both the function to act as a "container" and put the action we want repeated inside of it.&lt;/p&gt;

&lt;p&gt;First let's look at a function's most basic format, then discuss what is on each line:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function nameOfFunction (valuesGoHere) {&lt;br&gt;
     return (values, strings, or interpolation);&lt;br&gt;
}&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing the first line of our function:&lt;/strong&gt;&lt;br&gt;
First we use the &lt;strong&gt;function&lt;/strong&gt; keyword...&lt;br&gt;
Next we choose a descriptive name for our function...&lt;br&gt;
We'll follow this with () parenthesis. The values we put inside the parenthesis are determined by what kind of function we are making, and what we want our function to do.&lt;br&gt;
We'll end this line with a { opening curly bracket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing the "middle" line(s) of our function:&lt;/strong&gt;&lt;br&gt;
This is where we will put the code that will be used when we call the function later.&lt;br&gt;
First, we will either use the keyword &lt;strong&gt;console.log();&lt;/strong&gt; (for our own developer use) or &lt;strong&gt;return();&lt;/strong&gt; (to have the function return something).&lt;br&gt;
Inside the () parenthesis, we will now put a &lt;strong&gt;string&lt;/strong&gt; or &lt;strong&gt;value&lt;/strong&gt; or even use &lt;strong&gt;interpolation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing the last line of our function:&lt;/strong&gt;&lt;br&gt;
Lastly, to fully contain the function, we must end with a } closing curly bracket.&lt;/p&gt;

&lt;p&gt;This framework is the basic building block on which more complex functions can be built.&lt;/p&gt;

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