<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: aiodell</title>
    <description>The latest articles on Forem by aiodell (@aiodell).</description>
    <link>https://forem.com/aiodell</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%2F889545%2Fa687d6b5-bcd5-4be6-9983-fb2fce31b262.jpg</url>
      <title>Forem: aiodell</title>
      <link>https://forem.com/aiodell</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aiodell"/>
    <language>en</language>
    <item>
      <title>Java: Display Simple Arrays</title>
      <dc:creator>aiodell</dc:creator>
      <pubDate>Sat, 22 Oct 2022 15:46:49 +0000</pubDate>
      <link>https://forem.com/aiodell/java-display-simple-arrays-5b08</link>
      <guid>https://forem.com/aiodell/java-display-simple-arrays-5b08</guid>
      <description>&lt;h2&gt;
  
  
  Part 1 of my data structure journey.
&lt;/h2&gt;

&lt;p&gt;This is serving more as personal notes for myself to grasp my understanding of each topic on at least a basic level, but I am also sharing in case there are people who are going through the same learning process as I am!&lt;/p&gt;

&lt;p&gt;This is going to be something really simple and is not meant to be any more than creating and looping through an array to display no the console.&lt;/p&gt;

&lt;p&gt;All of my code and comments for myself are found in the &lt;a href="https://github.com/aiodell/java-array-practice"&gt;GitHub repo&lt;/a&gt;. My full notes are in the readme file.&lt;/p&gt;




&lt;p&gt;Arrays are the most basic data structure people can work with. They are defined by placing square brackets &lt;code&gt;[ ]&lt;/code&gt; after the data type, followed by the name of the variable. There are two different ways that I have created an array.&lt;/p&gt;

&lt;p&gt;For the examples, I will be creating an array of strings. One way to initialize an array is to state the length of the array 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;String[] names = new String[3];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this, I know my array is going to have a length of 3. I can manually add the information from there and specify which index  want to use for which information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;names[0] = "Avery";
names[1] = "Otto";
names[2] = "Emily";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another way I made an array was to immediately place data inside the array. It has the same length as the other one, but I placed everything in one line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String[] moreNames = {"Paul", "Fred", "Harold"};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I had wanted to select a specific element I would have used the array name and index. In this case I wanted to display the entire array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Display the Array
&lt;/h2&gt;

&lt;p&gt;For displaying the array, I will show two different ways: for-each and for loop. They give the same result, but I want to show the approaches to both.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For-each&lt;/strong&gt;&lt;br&gt;
Because of the simplicity of the array, I feel like I would personally use this loop to display what I need. The format for the for-each loop is 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(String name : names){
  System.out.println(name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A way I did to remember the for-each loop was to say &lt;em&gt;"for each name in the names array"&lt;/em&gt;. The array will print each element on separate lines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For Loop&lt;/strong&gt;&lt;br&gt;
The for loop was more tedious but was good to practice for when things become more complex in the future. The format is 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(int i = 0; i &amp;lt;= moreNames.length -1; i++){
  System.out.println(name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will result in the same thing. To explain the loop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first statement will execute one time before the execution of the code block.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second statement is the condition that determines if the loop ends or continues.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The third statement is executed every time after the code block. It will not execute once the condition is no longer true.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Extra: Integers and Array
&lt;/h2&gt;

&lt;p&gt;I thought it would be good to get a feel for iterating through integers rather than strings.&lt;/p&gt;

&lt;p&gt;I made this array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] nums = new int[]{1,5,7,8,3,4,0};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and created the for loop 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;for(int i = 0; i &amp;lt; nums.length; i++){
  nums[i] +=1;
  System.out.print(nums[i] + ", ");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wanted to do something a little extra with my looping by adding 1 to each element before printing. The for loop is the same, but has &lt;code&gt;nums[i] +=1;&lt;/code&gt; before the print statement. &lt;code&gt;+= 1&lt;/code&gt; is the same as &lt;code&gt;num[i] = i + 1;&lt;/code&gt;. so with each loop through the elements, i is increased by 1 and updated within the array.&lt;/p&gt;




&lt;p&gt;How about finding the sum of the integers? I will leave that to you! One thing to remember is that the array was already looped through once, and we did add 1 to each integer!&lt;/p&gt;

&lt;p&gt;Thank you for your time!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Personal Thoughts: Java Used To Intimidate Me But Not Anymore</title>
      <dc:creator>aiodell</dc:creator>
      <pubDate>Thu, 20 Oct 2022 01:33:55 +0000</pubDate>
      <link>https://forem.com/aiodell/personal-thoughts-java-used-to-intimidate-me-but-not-anymore-5b8f</link>
      <guid>https://forem.com/aiodell/personal-thoughts-java-used-to-intimidate-me-but-not-anymore-5b8f</guid>
      <description>&lt;p&gt;These last few months have been a roller coaster of emotions and mind explosions, but I am taking the time to reflect on what I have learned and experienced going through the Flatiron School Experience.&lt;/p&gt;

&lt;p&gt;One of the items I reflected on was my feelings about learning programming languages. As someone who took a temporary leave to pursue the software engineering bootcamp instead of a degree (I have no regrets doing that), I learned a lot more through the program than I did when I was taking the required university programming courses. &lt;/p&gt;

&lt;p&gt;Before completing the program, I was terrified of continuing my degree because I felt like I was going to get the degree and be completely lost. Honestly, I still feel a little lost but that is because of imposter syndrome setting in. However, I feel more confident now than I did before. I feel like I am going to continue and continue strong.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where does Java fall into this?
&lt;/h2&gt;

&lt;p&gt;I mentioned Java in the title, but not in the introduction. Though, I did mention it a little. It was the programming language that I was required to learn at University. I had to take two courses called programming 1 and programming 2. One focused on OOP and the other on data structures. I was so terrified of Java, and the assignments that were given did not seem clear on what they wanted me to do (clarification did not help either). &lt;/p&gt;

&lt;p&gt;I thought this was how the real world was going to be like. I could never remember anything and learned nothing (or so I thought). I wish I could have told past me it was going to be okay. It turns out, I am just a bad test taker.&lt;/p&gt;

&lt;p&gt;Not only am I a bad test taker, but reading 40+ pages a week on a programming language, and doing an assignment with code that has little to no comments is not the best approach for me. Speaking of comments, I was told comments were important to get other people to understand what you are doing with the code. I have experience getting confused on code because I have no comments to explain what is happening and I have to go through the code to see what I am working with.&lt;/p&gt;

&lt;p&gt;Because of this experience, I was afraid of learning any other language. However, Flatiron changed my views on that. I said it before and I will say it again! I feel like I can learn anything after going through the program. It helped me discover what the best learning method is for me, and now I can utilize it in my own learning.&lt;/p&gt;




&lt;h2&gt;
  
  
  I'm Coming Back To You, Java!
&lt;/h2&gt;

&lt;p&gt;I went back to the fundamentals of Java, as that is how I am used to starting my learning process. I realized that I did learn a lot, but somehow traumatized myself into thinking I knew nothing and suppressed that knowledge into an unknown place in my brain. Majority of it was review and me wondering why I thought even basic Java was difficult.&lt;/p&gt;

&lt;p&gt;I began my OOP journey with inheritance, encapsulation, polymorphism, and abstraction. I have no idea why I thought that was hard either. It is still something that I need more practice in, but it is not completely confusing to look at it anymore. In fact, I am more than happy to see that it makes organizing code easier and maximizes the functionality of my code.&lt;/p&gt;

&lt;p&gt;I am now beginning the data structure topics in my re-learning Java journey. Now that I am here, I feel like it is important for me to document my learning to reference and update it in case I forget or want to make the code that I will be creating for myself better.&lt;/p&gt;

&lt;p&gt;The future blogs will be a combination of personal thoughts and specific topics I learn throughout my data structure journey. They will be based on my own understanding of the topic, so if there is a better way to tackle the topics I discuss, I will always be all ears!&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Setting Up An Environmental Variable</title>
      <dc:creator>aiodell</dc:creator>
      <pubDate>Tue, 04 Oct 2022 13:28:14 +0000</pubDate>
      <link>https://forem.com/aiodell/setting-up-an-environmental-variable-2ojk</link>
      <guid>https://forem.com/aiodell/setting-up-an-environmental-variable-2ojk</guid>
      <description>&lt;p&gt;When I was searching for action mailer setups, everyone talked about environmental variables, but I never saw them showing people how they are set up. They usually mentioned something along the lines of “If you can figure out environmental variables, you are a step ahead of the game”. &lt;/p&gt;

&lt;p&gt;Personally, I think they should have been mentioned because these environmental variables are important in keeping your secrets safe from the public, especially when it comes to the username and password for the email you will be using for the mailer.&lt;/p&gt;




&lt;h2&gt;
  
  
  To set up the environmental variable
&lt;/h2&gt;

&lt;p&gt;In the initializers folder, create a ruby file named something that you will be able to identify as the place for your variables. In my case, I used &lt;code&gt;app_env_vars.rb&lt;/code&gt; or application environmental variables for short. In that folder, created the variables to look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M8YsJ90u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28w3ge60hwoslyvynd40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M8YsJ90u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28w3ge60hwoslyvynd40.png" alt="Environmental variables for a Gmail account" width="348" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These will replace the actual username and password of your account.  &lt;/p&gt;

&lt;p&gt;Now, if you were to create these variables and push these to a public repository, you would be exposing your secrets to everyone who checks out your code. Git Hub will surely send you and email and scold you in GitHub fashion to inform you that you have been exposing your secrets for (x) amount of minutes and the time will continue to increase until you make these changes. When in doubt, you can always utilize &lt;a href="https://www.gitguardian.com/"&gt;Git Guardian&lt;/a&gt; to your advantage. It will inform you of all exposed secrets.&lt;/p&gt;

&lt;p&gt;To make sure this does not happen, go into the &lt;code&gt;.gitignore&lt;/code&gt; file and paste the relative location of the environment file you have created. This means no one will be able to see your information. The only thing they will see are the names of the variables you created. Mine looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JHTqbqUn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtqvkzp2gaipl0xcw24e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JHTqbqUn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtqvkzp2gaipl0xcw24e.png" alt="example of a file being pasted into .gitignore" width="545" height="55"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;The environmental variables do not have to apply only to the creation of action mailer as I have shown. For example, if you were to use the &lt;code&gt;devise gem&lt;/code&gt; it contains a key within the comments that should become an environmental variable instead. Bottom line, if you are working on something that involves using a key or password or any kind of information that could create a security risk, create that environmental variable.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>todayisearched</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Time For That Decision!</title>
      <dc:creator>aiodell</dc:creator>
      <pubDate>Tue, 13 Sep 2022 13:05:42 +0000</pubDate>
      <link>https://forem.com/aiodell/time-for-that-decision-kn</link>
      <guid>https://forem.com/aiodell/time-for-that-decision-kn</guid>
      <description>&lt;p&gt;Looking at Software Engineering jobs, I started to question myself on what path I really wanted to take. There have been moments where I have thought to only go front-end, and there have been many times where I have been wanting to just stay in the back end.&lt;/p&gt;

&lt;p&gt;These are my thought processes as I continue to look through the job postings!&lt;/p&gt;




&lt;h2&gt;
  
  
  The Front-End
&lt;/h2&gt;

&lt;p&gt;Having to only worry about what is happening on a visual level sound like an easy thing to handle since there is the design aspect that I have always enjoying doing when creating not only applications, but other projects outside of this, such as interior decorations, banners, logos, etc. It seemed straight forward, and everyone would give me credit for creating something that looks really appealing to the eyes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I realized:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Having an attractive user interface is what will keep the customer engaged in viewing the page, especially since the average attention span for a user looking at website is about 8 seconds. I can relate to this, especially when I am looking for specific information.&lt;/p&gt;

&lt;p&gt;It is the front-end developer’s responsibility to not only communicate with the back end, but also get the user’s attention within that eight second time frame. This is important if you are working for a company that wants to market a product to the consumer. If they are not happy with the way the application looks, they are going to move on to find something that looks more appealing. &lt;/p&gt;

&lt;p&gt;You need to find the balance between creating a functional product and an aesthetically pleasing website. You need to make sure it is compatible to display on all devices and is optimized with accessibility in case you come across someone who cannot read the smaller text in some places or if their computer cannot render the image. &lt;/p&gt;

&lt;p&gt;There are also the frameworks that make it easier to create an application, such as React and Angular. At first, they seem intimidating because they are something that needs to be learned again. After going through a bootcamp and learning everything that I have, I feel like I can learn anything at this point.&lt;br&gt;
In the end, I concluded that despite the stress that comes from knowing that other people are going to see what you create, that physical product will always be there and &lt;/p&gt;




&lt;h2&gt;
  
  
  The Back-End
&lt;/h2&gt;

&lt;p&gt;I had mixed feelings for the back-end because I was not going to be able to make anything look attractive. I was used to seeing everything while I was making it. I thought I was going to become frustrated at the idea of working in the back-end. I also thought no one was going to pay attention to how I am manipulating the data in the back-end to send to the front end except for other programmers and the people who requested the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I realized:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The back-end gave me the ability to manipulate the data that I wanted, and when I was introduced to rails, my inner thoughts timed out for a little bit based on the processing I was doing on how easy everything had become in not just the back-end, but also the front-end. I still got to see it after the data was sent up. The front-end was also sending information back into the database, so it was important for me to make sure my actions worked correctly for the CRUD. &lt;br&gt;
The main thing that got me hooked onto the back-end was the ability to choose what data I was going to send to the front end. The way it was handled in the front end when I had no idea how the back-end worked was a very irritating experience (that is me being nice), especially pulling from the external APIs. I can say am I that person that will continuously get excited when I see the front-end send data to the back-end and have it posted in the database.&lt;/p&gt;




&lt;h2&gt;
  
  
  So Full-Stack?
&lt;/h2&gt;

&lt;p&gt;After working with both the front-end and back-end, I have realized how fun it is to be able to create the back-end, and then move to the front end to see what I created come to life. I also enjoy seeing things that once were taken care of only in the front-end when I was just beginning, to be now taken care of in the back-end and taking the load off of the front end. It can now be focused on how things will look for the user.&lt;/p&gt;

&lt;p&gt;Since I have the knowledge to be full-stack, I might as well go full-stack. At least, that is how I am thinking it…&lt;/p&gt;

&lt;p&gt;I think I’m more valuable to the market since I know both ends as opposed to one end. Money is great and all but knowing that I can always go from one to the other makes me feel like I will be able to be as involved in a project as possible. Not only that, I think I would be able to help others out more who are just back-end or just front-end. That might be me living in a fantasy world, but I will never know until I find out or someone tells me otherwise! &lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>career</category>
    </item>
    <item>
      <title>Migrations and Changes</title>
      <dc:creator>aiodell</dc:creator>
      <pubDate>Tue, 23 Aug 2022 01:44:00 +0000</pubDate>
      <link>https://forem.com/aiodell/active-record-migrations-and-changes-144k</link>
      <guid>https://forem.com/aiodell/active-record-migrations-and-changes-144k</guid>
      <description>&lt;p&gt;Migrations are just a small part of what Active Record is capable of. Once you get used to how migrations work, your speed in creating these migrations will increase and become muscle memory like all the other lines of code you have created within Ruby. &lt;/p&gt;




&lt;h2&gt;
  
  
  Creating a Migration
&lt;/h2&gt;

&lt;p&gt;In order to create a migration, we need to enter code that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bundle exec rake db:create_migration NAME=create_my_friends_table&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If done correctly, this should have appeared in the terminal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;db/migrate/20220822163630_create_my_friends_table.rb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bundle exec&lt;/code&gt; is used to ensure that there are no compatibility issues between versions. It is a safer practice to use bundle exec even if you are certain your version is working.&lt;/p&gt;

&lt;p&gt;After migrating, you create your table like so:&lt;/p&gt;

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

&lt;p&gt;You add your migration to the database with &lt;code&gt;bundle exec rake db:migrate&lt;/code&gt;. It will give you another message that states that the migration was successful. If you did it correctly, you should have seen a message appear showing that the migration was added into the db folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0xGTDAdB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg3bv5pcg2fc5lxlavw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0xGTDAdB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg3bv5pcg2fc5lxlavw8.png" alt="migration success" width="567" height="38"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the migration, you should be able to see that a schema has been created for you within the same &lt;code&gt;db&lt;/code&gt; directory and can be viewed. You can check the status of your migration with &lt;code&gt;bundle exec db:migrate:status&lt;/code&gt;. It will show you which migrations status on whether they are active or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UGuB0TLH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq5be2diurq3n8h0coks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UGuB0TLH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq5be2diurq3n8h0coks.png" alt="migration up status" width="355" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are numbers found in the name of the migration that should not be changed or else you will receive errors. They identify that specific migration and also logs when that migration is made. It becomes useful when working with multiple people who need to trace back to a certain migration.&lt;/p&gt;




&lt;h2&gt;
  
  
  Making Changes After Running the Migration
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Rolling back&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you migrated your table, but realized you need to make a change, you can rollback to the previous migration with &lt;code&gt;bundle exec rake db:rollback&lt;/code&gt;. This will change the status from up to down and enable you to make a change. If there were multiple migrations, the most recent one would be removed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7gFtLpb7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2nustmz2ir6c0f29ipy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7gFtLpb7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2nustmz2ir6c0f29ipy.png" alt="rollback example" width="363" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, I did not feel the need to have whether or not I have been talking to my friends. I also decided that there was no need to think about the years of friendship and wanted to add age instead. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dDklKFLL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfsix324cma3qeotu25b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dDklKFLL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfsix324cma3qeotu25b.png" alt="updated table" width="453" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When migrating again, we will be able to see the changes go through under the same migration name. The schema also shows this change.&lt;/p&gt;

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

&lt;p&gt;**Note: **If you wanted to rollback multiple migrations, you could use &lt;code&gt;bundle exec db:rollback STEP=&amp;lt;number of migrations back&amp;gt;&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Migration methods
&lt;/h2&gt;

&lt;p&gt;Active Record was kind enough to provide us with methods to make changes to our database without needed to completely drop our table and start over again. Here are a list of a few of them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add_column&lt;/li&gt;
&lt;li&gt;add_index&lt;/li&gt;
&lt;li&gt;change_column&lt;/li&gt;
&lt;li&gt;change_table&lt;/li&gt;
&lt;li&gt;create_table&lt;/li&gt;
&lt;li&gt;remove_column&lt;/li&gt;
&lt;li&gt;remove_index&lt;/li&gt;
&lt;li&gt;rename_column&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We already have seen create_table when we created our table. I will not go over all of them here in detail, but I want you to be aware of the others that exist. For every example, there will be a new migration. Personally, I like being able to create migrations because I want to keep track of all changes I made to my database. I would rather rollback to a previous migration than have to redo an entire table&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding Columns&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If I wanted to add a column to my current migration without rolling back, I would go through the process of creating a new migration, and name that migration based on what I was doing. Therefore, it would be &lt;code&gt;bundle exec rake db:create_migration NAME=add_phone_number_to_friends_table&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After that, the syntax within the migration will be different because we are adding a column. It will be &lt;code&gt;add_column :table_name, :column_name, :data_type&lt;/code&gt;. Naming the migration after the table that is being changed helps with organization. I would not want to open up migrations to see where the name of the table appears.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s-HohR_h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et41so1dk99953anhv34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s-HohR_h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et41so1dk99953anhv34.png" alt="adding column" width="500" height="96"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you run the migration, it will be updated in the schema and should be placed as the last column.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Removing Columns&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If my friends were upset that I placed their age (they would never be they are happy I remember their age but this is just a scenario), I would create another migration to remove the column. I'm sure we know the process of creating migrations for now, so I will show the syntax for removing a column &lt;code&gt;remove_column :table_name, :column_name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b2yOG8Ab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkq26mwkr6xlpvbzbzlf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b2yOG8Ab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkq26mwkr6xlpvbzbzlf.png" alt="remove_column syntax" width="512" height="96"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If things have been working out for you, the schema will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rjx3Hth9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ryatejxqwodj0sedl6l4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rjx3Hth9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ryatejxqwodj0sedl6l4.png" alt="updated schema without age and with phone number" width="463" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Changing Columns&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even more changes need to be made! We realized we don't want an integer for our phone number, we want a string! We need to once again (and I promise this is the last migration) create a new migration. At least you now are an expert at creating these things! The syntax for this is just as simple as removing and adding columns. &lt;code&gt;change_column :table_name, :column_name, :new_data_type&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mZf9aKYp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cv4w2mtboqsm42mdu8qg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mZf9aKYp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cv4w2mtboqsm42mdu8qg.png" alt="change columns" width="633" height="98"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final schema should look like this:&lt;/p&gt;

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

&lt;p&gt;If you noticed, the methods all began the same way: identifying the table. This is because without identifying a table, it would not know what table you are looking at. Just because the name of the migration is supposedly linked to the table you are making changes to, it does not mean you are automatically linking the table to the migration. A name is just a name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Just Drop It&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There might be cases where a migration made something irreversible. For instance, you might have destroyed data that no longer can be retrieved, you can always drop the table with &lt;code&gt;bundle exec rake db:drop&lt;/code&gt; to drop the entire database and run migrations all over again to the version you desire. However this might become troublesome with larger databases and is probably best to be avoided if possible.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Note on the other methods:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;remove_index&lt;/code&gt; will remove the given index from the table&lt;/p&gt;

&lt;p&gt;&lt;code&gt;add_index&lt;/code&gt; will add a new index to the table. Unless you specify, the index will be named after the table and the column name or names (if you added multiple columns each name would be included)&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;rename_column&lt;/code&gt; method is in a similar format as change column, but where the datatype is located at is now replaced with the new column name: &lt;code&gt;rename_column :table_name, :column_old_name, :column_new_name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;change_table&lt;/code&gt; creates a block to change columns in a table like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L7fXn1R6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s12ih7jrs6gc6t6ra3t8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L7fXn1R6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s12ih7jrs6gc6t6ra3t8.png" alt="change table example" width="307" height="94"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://apidock.com/rails/v4.0.2/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index"&gt;apidock.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/v3.2/migrations.html"&gt;Migrations&lt;/a&gt;&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>beginners</category>
      <category>ruby</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Simply { useState }</title>
      <dc:creator>aiodell</dc:creator>
      <pubDate>Tue, 02 Aug 2022 15:56:00 +0000</pubDate>
      <link>https://forem.com/aiodell/simply-usestate--1pa6</link>
      <guid>https://forem.com/aiodell/simply-usestate--1pa6</guid>
      <description>&lt;p&gt;For all beginners, useState can seem intimidating. For one, you must import it to use it. Thankfully, we do not have to worry about looking deep into what is inside of that import or everyone’s beginner minds would explode. I am here to (hopefully) provide a simple example and a little explanation on what is going on with this react hook useState.&lt;/p&gt;




&lt;h2&gt;
  
  
  Knowing the rules
&lt;/h2&gt;

&lt;p&gt;First, remember that useState is a React hook. Therefore, it needs to follow these rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They can only be called within React function components&lt;/li&gt;
&lt;li&gt;They can only be called at the top level of a component&lt;/li&gt;
&lt;li&gt;They cannot be conditionals&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Second, it is not to be confused with props. Some facts about state:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State can hold data and change over time&lt;/li&gt;
&lt;li&gt;The stored data is within components that need to be re-rendered &lt;/li&gt;
&lt;li&gt;State can only be used in function components&lt;/li&gt;
&lt;li&gt;States can be updated with event handlers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;DO NOT&lt;/strong&gt; forget to import useState into the component before using it like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import React, { useState } from “react”;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Do not worry about creating a new line to import it if you are already importing from React. You can separate with a comma, but do not forget the curly brackets or it will not recognize it!&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating A useState
&lt;/h2&gt;

&lt;p&gt;Create the useState under the component that will be changing states. While you can name the variable anything, it is helpful to name it based on the state that will be changing. In this example, the state change will be the changing colors based on what is checked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UssS2UBn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15xaonkagv1zodcr9d3t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UssS2UBn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/15xaonkagv1zodcr9d3t.png" alt="useState import" width="389" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before thinking about changing const to let, const will be a reminder that the value should never be reassigned in the code. There will always be a single state each change, so there will never be multiple values involved with the state.&lt;/p&gt;

&lt;p&gt;The first value, &lt;code&gt;color&lt;/code&gt;, is the current state. The second value &lt;code&gt;setColor&lt;/code&gt; can also be seen as &lt;code&gt;setColor()&lt;/code&gt; since it is the function that will be used for changing the state. The code &lt;code&gt;useState(" red ")&lt;/code&gt; is saying that the initial state is going to be the color red. You will see how this works further down.&lt;/p&gt;

&lt;p&gt;One more detail about the line of code: Do not worry about having only type string in the initial state. It can be what suits your needs – empty &lt;em&gt;string&lt;/em&gt;, &lt;em&gt;Boolean&lt;/em&gt;, &lt;em&gt;integer&lt;/em&gt;, etc. The main thing to remember is that the result will always be the same type. Do not mistype your state.&lt;/p&gt;




&lt;h2&gt;
  
  
  Using useState
&lt;/h2&gt;

&lt;p&gt;One example we can use useState is by changing the color of text based on if a button is clicked. This will involve two states. We want the state to change when the button itself is clicked, not the text. This is a perfect example of two different types being used as initial states!&lt;/p&gt;

&lt;p&gt;The new state we created is named &lt;code&gt;isClicked&lt;/code&gt;because we are going to make the color change between green and red each time it is clicked. From a user perspective, they click once and the button changes. They click another time, and it changes to the other color. &lt;/p&gt;

&lt;p&gt;From a programmer perspective, we are finding a way to switch between states. In this case, the button is clicked and considered clicked, while the other click determines that it is not (even though we are technically clicking both times). The &lt;em&gt;Boolean&lt;/em&gt; allows us to switch between the two states or true and false to make this change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xKCA-5Bh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10j2z74zkrmr90ct8gmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xKCA-5Bh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10j2z74zkrmr90ct8gmt.png" alt="click and color states" width="401" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When including the rest of code, it will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BgMU0IDj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hk2kk6pob8ccpi9wyzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BgMU0IDj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hk2kk6pob8ccpi9wyzh.png" alt="base code example" width="448" height="327"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Since the button is going to be used to change the text, the button should hold the function. As with the state, we need to name the function based on what the button will be doing. Since it handles the change of colors, we will name is &lt;code&gt;handleColorChange&lt;/code&gt;. This will go inside of the component, not outside because it will not be recognized by either the &lt;em&gt;TextColor&lt;/em&gt; component or its parent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qoqsw5uA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgrruwfn12cqh9ad2jkm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qoqsw5uA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgrruwfn12cqh9ad2jkm.png" alt="handle clicked function" width="419" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the text is first shown, it will be the state that is the initial state of &lt;code&gt;isClicked&lt;/code&gt; which is red. We should see a red text when it is rendered. When clicked, the &lt;code&gt;setClicked&lt;/code&gt; function will change &lt;code&gt;isClicked&lt;/code&gt; to &lt;em&gt;true&lt;/em&gt;, which results in green. Every time the button is clicked, it will switch between the two states. Because &lt;code&gt;setColor&lt;/code&gt; is linked to either true or false, this causes the color to switch between the red and green states.&lt;/p&gt;

&lt;p&gt;Note: It is important that the &lt;em&gt;if true&lt;/em&gt; is set to the same state as what is in the initial state, or it will need to be clicked twice for you to see the change rendered. Because red is false, you are changing the state to true and linking true to red and green to false. This is a small but important detail to remember when making true and false state changes.&lt;/p&gt;




&lt;p&gt;Adding into the code. This is where the color variable resides within the code. As the button is clicked, the style will change to the color of the current state. Make sure the event appears on the button and not the text. Otherwise, you will be clicking the text and wondering why the button does not work!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YHhxCoTg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgybz3yn8qjio3st3ng6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YHhxCoTg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgybz3yn8qjio3st3ng6.png" alt="return statement" width="624" height="140"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;If everything went according to plan, then you should have a basic understanding of how useState functions within a component. It is versatile and easy to use once you get passed the intimidation phase. You can use it alongside other React hooks, but I will save that for another day.&lt;/p&gt;

&lt;p&gt;Enjoy using states!&lt;/p&gt;




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

&lt;p&gt;&lt;a href="https://www.w3schools.com/react/react_hooks.asp"&gt;React Hook Basics&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/react/react_usestate.asp"&gt;useState Basics&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.simplilearn.com/tutorials/reactjs-tutorial/reactjs-state"&gt;React useState&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Scope Matters</title>
      <dc:creator>aiodell</dc:creator>
      <pubDate>Tue, 12 Jul 2022 22:53:07 +0000</pubDate>
      <link>https://forem.com/aiodell/scope-matters-2g71</link>
      <guid>https://forem.com/aiodell/scope-matters-2g71</guid>
      <description>&lt;p&gt;I imagine all of us have encountered this kind of scenario:&lt;/p&gt;

&lt;p&gt;We create some variables in the beginning of our program because we know that we are going to need to use them later in the program 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;const num1 = 10;
const num2 = 5;
let num3 = num1 + num2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great start!&lt;/p&gt;

&lt;p&gt;It is time to make the first function for our program. We need to use one of the variables we created as an argument to pass through. We also know that we want to return a specific value and need some extra variables inside the function to help with that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction(num1, num2) {
 let product = num1 * num2;
 let ans = product + num3;
 return(ans);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we invoke the function and run it through &lt;em&gt;console.log()&lt;/em&gt;, we get this result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("It returns: " + myFunction(num1, num2));
// It returns: 65
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Everything works perfectly! Now what if we wanted to take the &lt;em&gt;ans&lt;/em&gt; variable and use it again?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;num3 = ans + num1;
// ReferenceError: ans is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error occurs because the variable ans is only in the scope of the function, not the global scope like &lt;em&gt;num1&lt;/em&gt;, &lt;em&gt;num2&lt;/em&gt;, and _num3 _ are. When a variable is only in the scope of a function, it cannot be used anywhere else. In fact there are three different types of scope in JavaScript to keep in mind when programming.&lt;/p&gt;




&lt;h2&gt;
  
  
  So, What Exactly Is Scope?
&lt;/h2&gt;

&lt;p&gt;To make it simple, scope is a fancy term that describes what code can and not see. In the scenario above, the function could see both inside and outside of its code, but the expression outside of the function what was inside of the function. Let me explain in more detail.&lt;/p&gt;

&lt;p&gt;In JavaScript, there are three types of scope: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Block scope&lt;/li&gt;
&lt;li&gt;Function scope&lt;/li&gt;
&lt;li&gt;Global scope&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Block Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A block scope contains variables that are declared within curly brackets &lt;strong&gt;{ }&lt;/strong&gt;. This is seen within if statements and loops, such as for and while. In short, if you see variables declared within curly brackets, they are in block scope. They &lt;em&gt;cannot&lt;/em&gt; be accessed by anything outside of the brackets. It will result in the same error we saw above. We can declare a variable with the same name – one within the block and one outside the block and have them not interfere with one another.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(num1 != num2){
 let fact = "yes";
 console.log(fact);
}
// yes

const fact = "This is fine."
console.log(fact);
// this is fine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, there is an exception. If a variable is declared with the &lt;em&gt;var&lt;/em&gt; keyword, it can be accessed from outside the block. This would mean you cannot declare a new variable with that same name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(num1 != num2){
 var fact = "yes";
 console.log(fact);
}
// yes

const fact = "This is fine."
console.log(fact);
// SyntaxError: Identifier 'fact' has already been declared
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Function Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A function scope is what we saw in the beginning, so there is not much left to add. The variables declared within the function are considered local variables to the function they are declared in and can only be used within the function. Unlike block scope, this pertains to &lt;em&gt;all&lt;/em&gt; keywords, not just let and &lt;em&gt;const&lt;/em&gt; keywords.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Global Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Variables declared within the global scope can be use throughout the entire program since everything has access to it. For example, the three &lt;em&gt;num&lt;/em&gt; variables that were previously declared can be used in block scopes or function scopes. &lt;/p&gt;

&lt;p&gt;It is useful when you know what variables you want to have as a global variable. For instance, I can have a &lt;em&gt;let&lt;/em&gt; keyword for &lt;em&gt;num3&lt;/em&gt; and have it returned by a function. The original value of &lt;em&gt;num3&lt;/em&gt; becomes overridden and remains the same until it changes again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const num1 = 5;
const num2 = 10;
let num3 = 15;

console.log(num3)
// 15

function add(num1, num2){
 num3 = num1 * num2;
 return num3;
 // 50
}

console.log(num3);
// 50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This also means that if there is a global variable declared as a const, you cannot change the value. If you feel like you are going to be changing the value of a global variable, declare it with the let keyword instead of the &lt;em&gt;const&lt;/em&gt; keyword. Better safe than annoyed later!&lt;/p&gt;

&lt;p&gt;Now, if you were to declare a new variable inside of a block scope or a function scope, then you would be able to use the same name as the global variable without getting an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const num1 = 5;
const num3 = 50;

if(num3 === 50){
 const num1 = "I am my own variable"
 console.log(num1)
// I am my own variable
// ignores the global const variable previously declared
}

console.log(num1)
// 5

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

&lt;/div&gt;



&lt;p&gt;When working with the same variable names within different scopes, remember which value you are working with, or you will become confused! If possible, keep the names related to what you are doing with the variable and only repeat the name in a different scope if it is necessary.&lt;/p&gt;




&lt;h2&gt;
  
  
  Some Final Advice
&lt;/h2&gt;

&lt;p&gt;If you ever end up in a situation where you see &lt;em&gt;ReferenceError&lt;/em&gt; and explains that your variable is not declared, think about what scope it is in within the program. Chances are, you simply declared it in an area with limited scope. &lt;/p&gt;

&lt;p&gt;The best approach to take it this: If you feel like you will be using a variable in multiple places, it is best to declare it as a global scope to ensure it will be seen by the entire program. Most importantly, do not be discouraged! There is always a solution to the problem!&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/js/js_scope.asp"&gt;W3Schools: JavaScript Scope&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope"&gt;MDN: Scope&lt;/a&gt;&lt;/p&gt;

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