<?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: Kevin Downs</title>
    <description>The latest articles on Forem by Kevin Downs (@kjdowns).</description>
    <link>https://forem.com/kjdowns</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%2F420003%2Fe842f591-d7ff-49cb-b5bf-026ecc2dee69.jpeg</url>
      <title>Forem: Kevin Downs</title>
      <link>https://forem.com/kjdowns</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kjdowns"/>
    <language>en</language>
    <item>
      <title>Creating a User Login System - Ruby on Rails</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 01 Dec 2020 04:32:27 +0000</pubDate>
      <link>https://forem.com/kjdowns/creating-a-user-login-system-ruby-on-rails-2kl2</link>
      <guid>https://forem.com/kjdowns/creating-a-user-login-system-ruby-on-rails-2kl2</guid>
      <description>&lt;p&gt;The last few weeks I have been chronicling my experience creating an application using Rails and React with the &lt;code&gt;react-rails&lt;/code&gt; gem. One of the more general parts of an application that is applicable to a wide variety of projects is a user login. While building out the skeleton of a user login system for the trivia app I am currently working on, I thought it might be an opportunity to share my approach for others that might be looking to build out a similar system. &lt;/p&gt;

&lt;p&gt;Small note about this post: there are so many different ways one could approach building a login system for their applications. If you would like to follow along and build your own, feel free to play around with things and make them your own. To keep things as simple as possible, the system I will be building out here will be pretty bare bones. For this post, I am also working with Rails 6 and not utilizing the React functionality of the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Get Started!
&lt;/h2&gt;

&lt;p&gt;The first thing we are going to need is a Rails project to work in. I already have one built, but if you are looking to just play around or create your own new project for whatever reason, you can do so by using the &lt;code&gt;rails new &amp;lt;app-name&amp;gt;&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;For our login system, the goal is to have a Sign Up page where users can create an account and a Login page that will allow them to log into their account. To be able to test this, we will also have a "profile" page that will be visible after a user is logged in. Let's look at a breakdown of the things we will need to make this happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User Model &amp;amp; Database Migration&lt;/li&gt;
&lt;li&gt;Sessions&lt;/li&gt;
&lt;li&gt;Helpers&lt;/li&gt;
&lt;li&gt;Routes&lt;/li&gt;
&lt;li&gt;Controllers&lt;/li&gt;
&lt;li&gt;Views&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  User Model
&lt;/h2&gt;

&lt;p&gt;The first thing we will need to do is create a Model for our User data. For security, we will be using the &lt;code&gt;bcrypt&lt;/code&gt; gem to salt and hash our user's passwords. To add it, simply go to your gemfile and add &lt;code&gt;gem 'bcrypt'&lt;/code&gt;. Rails may have automatically added this gem when creating a new project, in that case you just have to remove the comment. After making sure it is included in your gemfile, simply run &lt;code&gt;bundle install&lt;/code&gt; to install the gem in your project.&lt;/p&gt;

&lt;p&gt;We are ready now to create our actual model. You could just create a new file in the &lt;code&gt;models&lt;/code&gt; folder, but we are going to use a generator for this. The benefit of using the generator is that not only will rails automatically create the model file for us, but it will also create a migration for us so that we can easily create a Users table in our database. &lt;/p&gt;

&lt;p&gt;For our user model we will be keeping things simple and requiring just a &lt;code&gt;username&lt;/code&gt; and a &lt;code&gt;password&lt;/code&gt; field. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g model User username:string password_digest:string&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With this command Rails will generate a User model for us as well as a migration for that model. The migration will create a User table with a username and password column as well as standard id and timestamp columns. The reason we are using password_digest here is because of bcrypt. When we connect our model to the gem a little later, this column will give us access to a password and password_confirmation field and allow us to authenticate our user's password.&lt;/p&gt;

&lt;p&gt;To use bcrypt with our model is very simple. Let's just go to the &lt;code&gt;model.rb&lt;/code&gt; file and add the helper &lt;code&gt;has_secure_password&lt;/code&gt; inside the class definition. It should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feu3iay7h4zil1r8sjplw.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feu3iay7h4zil1r8sjplw.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have everything connected, the last step is to load our migration. If you haven't already, create your database by using the &lt;code&gt;rails db:create&lt;/code&gt; command. After the database is created we can simply run our migration with the &lt;code&gt;rails db:migrate&lt;/code&gt; command. We should now have a working User model and database table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sessions
&lt;/h2&gt;

&lt;p&gt;We will be taking advantage of the sessions hash that Rails provides for us to keep track of the logged in user. I don't want to go into too much detail here, but essentially this provides us a way to persist the current user through our application so that they don't have to continually log in. Just know that when you see us using &lt;code&gt;session[:user_id]&lt;/code&gt; this is what we are accessing. If you'd like to get a more detailed understanding, feel free to read more about it &lt;a href="https://www.theodinproject.com/courses/ruby-on-rails/lessons/sessions-cookies-and-authentication" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Helpers
&lt;/h2&gt;

&lt;p&gt;This next part is optional, but I find it useful when developing my own applications. We are going to set up some helper functions that will allow us to DRY up our code a little bit. Specifically we will be creating a &lt;code&gt;logged_in?&lt;/code&gt; helper that will return true or false based on whether or not there is an active &lt;code&gt;session[:user_id]&lt;/code&gt; and a &lt;code&gt;current_user&lt;/code&gt; helper that will return to us the database model for the currently logged in user. We will place the code for these two in &lt;code&gt;app/helpers/application_helper.rb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5mfpvj1ea966f18tsj70.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5mfpvj1ea966f18tsj70.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Routes
&lt;/h2&gt;

&lt;p&gt;The last bit we need to set up before getting into the nitty gritty of Controllers and Views is setting up our routes. We are going to have two Controllers we need to create routes for: &lt;code&gt;session&lt;/code&gt; and &lt;code&gt;users&lt;/code&gt;. We will be using the sessions to handle the session actions of logging in and out. The users will be in charge of the actual user data for things like creating and editing new users. &lt;/p&gt;

&lt;p&gt;For users we can use the &lt;code&gt;resources&lt;/code&gt; helper and specify the routes we want Rails to make available for us. For the sessions routes, I prefer creating them myself so that I can customize the paths.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnachp0lpppj7c3j7xj0c.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fnachp0lpppj7c3j7xj0c.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Controllers
&lt;/h2&gt;

&lt;p&gt;The controllers will allow us to perform some logic as well as pass information on to our views. When the user requests a specific endpoint, the controller will execute the corresponding code and make certain variables available to our view for whatever we may need. I prefer to create my controller and view files individually, but you can also use a rails generator to do both automatically. If you are creating your own Controller like I am, just create a new file in the &lt;code&gt;app/controllers&lt;/code&gt; folder using the plural like this: &lt;code&gt;sessions_controller.rb&lt;/code&gt;. The naming convention is important here so that Rails can link your models, views, and controllers together.&lt;/p&gt;

&lt;p&gt;For the Sessions controller we will only need a create action. Since our &lt;code&gt;login&lt;/code&gt; route will just render our login view, Rails will automatically handle that for us. In our create action we will search our database for the user entry that matches the username provided in the login form, authenticate using bcrypt and the provided password, and then setting the &lt;code&gt;session[:user_id]&lt;/code&gt; and redirecting the user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd1z4xsvuqyulwzjqzjvf.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd1z4xsvuqyulwzjqzjvf.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Users Controller is a little more involved. We will need a new action that will give our view access to a new User instance. &lt;/p&gt;

&lt;p&gt;Our create action will be very similar to our login action, with the exception that we will be creating and saving a new user database entry. Lastly we will have a show action that will give us access to the data for the currently logged in user.&lt;/p&gt;

&lt;p&gt;We will also be enforcing strong params which you can learn more about &lt;a href="https://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhmia2vsq7akzw3j4qsy2.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhmia2vsq7akzw3j4qsy2.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Views
&lt;/h2&gt;

&lt;p&gt;The last step is to create some views that we can actually interact with. This is the bit that is most customizable, as you can create whatever variety of UI you would like for this. As mine are also rather basic just to get things working, I will just give an overview of what is needed here.&lt;/p&gt;

&lt;p&gt;We will need to first create a &lt;code&gt;users&lt;/code&gt; and &lt;code&gt;sessions&lt;/code&gt; folder within the &lt;code&gt;app/views&lt;/code&gt; folder. There we can create our view files for each of our controllers. Using the naming convention of &lt;code&gt;&amp;lt;action_name&amp;gt;.html.erb&lt;/code&gt; Rails will automatically render the corresponding views to our controller actions. For example, our login view should be &lt;code&gt;app/views/sessions/login.html.erb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For our sessions, we will need a &lt;code&gt;login&lt;/code&gt; view, and for our users we will need a &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;show&lt;/code&gt; view. &lt;/p&gt;

&lt;p&gt;For the login and new pages, we will need at minimum a form for the user to input a username and password and submit that data to our controller for handling. Below you can see my bare bones implementation of these pages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqef9xz5gmz1mnglit2vn.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqef9xz5gmz1mnglit2vn.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fleiaoeoyagy5nyp03h9n.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fleiaoeoyagy5nyp03h9n.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final show page can really display any information you would like. Here we have access to the data for the logged in user so something like a profile page that displays the user's information, with buttons to edit or delete data might be a good idea.&lt;/p&gt;

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

&lt;p&gt;And that's how I get started with pretty much every Rails project I create to have a bare-bones functional login system. I hope this was helpful in some way if you're still here. Happy Coding!&lt;/p&gt;

</description>
      <category>rails</category>
    </item>
    <item>
      <title>Personal Project - Trivia App</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 24 Nov 2020 03:20:59 +0000</pubDate>
      <link>https://forem.com/kjdowns/personal-project-trivia-app-1nck</link>
      <guid>https://forem.com/kjdowns/personal-project-trivia-app-1nck</guid>
      <description>&lt;p&gt;As Covid-19 continues to change the way we interact with the world, we are all discovering new ways to connect - mainly in online spaces. One of the activities my friends and I started to make a regular occurrence was weekly Trivia Nights on Zoom. As someone learning software development, I always wanted to find a way to make this activity a more pleasant and enjoyable experience. Currently we were working on a simple editable slideshow with categories and point vales, with individual players having the responsibility of keeping track of and actually reading their questions. My goal was to create an application that would allow players to submit categories and questions to a central game, that the game runner would then be able to execute like a browser game. &lt;/p&gt;

&lt;p&gt;The only thing I had been struggling with was finding an appropriate stack or framework to work with. After my post last week on using React with Rails, I thought this would be a great way to dive deeper into using those two technologies together as it seemed like the perfect fit for what I wanted to do. As this project is something more complicated than I've worked on before I wanted really take the time to design things properly. I also thought this would be a good opportunity to do a different type of post this week, talking more about my process in designing the structure of the application. Let's get to it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Models
&lt;/h2&gt;

&lt;p&gt;The most important thing to plan out for me are the models that my application will be using. Since these will form the basis of how the application flows and what features are available to users. &lt;/p&gt;

&lt;p&gt;Obviously my first model would be a User as everything else in the application will be user based for the most part. My vision was to have players able to create Games as well as add categories to both their own games as well as other created games. So we need a Game and Category model as well.&lt;/p&gt;

&lt;p&gt;Lastly, I wanted each category to consist of five questions, so we will need a Questions model as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Users
&lt;/h3&gt;

&lt;p&gt;The Users model will be the most simple out of all of them. Just starting I will only need to have a &lt;code&gt;username&lt;/code&gt; and a &lt;code&gt;password&lt;/code&gt; attribute to allow a User to register and login.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Attribute&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;username&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;password&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Game
&lt;/h3&gt;

&lt;p&gt;The second model and the one that we will access most of our information from will be &lt;code&gt;Game&lt;/code&gt;. Each &lt;code&gt;Game&lt;/code&gt; will belong to a specific user and so we will definitely want to have a &lt;code&gt;user_id&lt;/code&gt; attribute. Each Game should also probably have a &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will also want users to be able to link categories to a specific game that may not be theirs. My thought process on this was to generate a unique code for each game that a player could share or input in order to gain access to that game. We will call this something like &lt;code&gt;game_code&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lastly each Game will have two teams that will be playing against each other. The implementation for this might change in the future but for now I've gone with a &lt;code&gt;team&lt;/code&gt; and &lt;code&gt;team_score&lt;/code&gt; field for each.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Attribute&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;user_id&lt;/td&gt;
&lt;td&gt;integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;name&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;game_code&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;team_a&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;team_a_score&lt;/td&gt;
&lt;td&gt;integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;team_b&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;team_b_score&lt;/td&gt;
&lt;td&gt;integer&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Categories
&lt;/h3&gt;

&lt;p&gt;The next model to work on is the &lt;code&gt;Category&lt;/code&gt; model. I'm still not 100% sold on the implementation of this one, but for now I wanted to have each category link to a specific game and be a container of sorts for our questions. To link to our game, we will need to have a &lt;code&gt;game_id&lt;/code&gt; attribute, and we want each category to have a name as well.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Attribute&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;game_id&lt;/td&gt;
&lt;td&gt;integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;name&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Questions
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Question&lt;/code&gt; model is the last one! We want to link each question with a specific category so a &lt;code&gt;category_id&lt;/code&gt; attribute is a must. Next we need the actual &lt;code&gt;question&lt;/code&gt; and &lt;code&gt;answer&lt;/code&gt; values for the actual question. To keep track of what each question is worth we will also need a &lt;code&gt;point_value&lt;/code&gt; attribute, and lastly a boolean to track whether or not the question has been answered.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Attribute&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;category_id&lt;/td&gt;
&lt;td&gt;integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;question&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;answer&lt;/td&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;point_value&lt;/td&gt;
&lt;td&gt;integer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;answered&lt;/td&gt;
&lt;td&gt;boolean&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;And those are the basics as to how I'm organizing my application! I hope if you're trying to figure out how to plan your own application out this might give you some ideas on how things can be organized. I think one of the most important things to keep development simple is to have a good scaffold or plan going into things. It's much easier to anticipate changes or tweak things with good planning!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using React and Rails Together</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 17 Nov 2020 02:01:07 +0000</pubDate>
      <link>https://forem.com/kjdowns/using-react-and-rails-together-3ja3</link>
      <guid>https://forem.com/kjdowns/using-react-and-rails-together-3ja3</guid>
      <description>&lt;p&gt;In my time at Flatiron School we learned a lot about Ruby on Rails and React, building complete projects with both. Thus far I had only handled using these two technologies together in one way: create a React front-end that is fed data from a Rails API. This is certainly a great way to handle things, but as I started building my own projects I found myself wanting for a more elegant solution than splitting things up into two separate projects that were just able to talk to each other. I found myself thinking why isn't there a way to take advantage of Rails' MVC pipeline and use React directly within the Rails views. After some searching and googling I found a solution that seems to do exactly that: the &lt;code&gt;react-rails&lt;/code&gt; gem.&lt;/p&gt;

&lt;p&gt;I'd like to use this post as an opportunity to walk anyone who may have been in a similar boat as myself through the process of setting everything up in a single project. If you are someone who likes to go straight to documentation and play around with things yourself, I encourage you to check those out &lt;a href="https://github.com/reactjs/react-rails"&gt;here&lt;/a&gt;. The setup instructions that come with them are fantastic, and for the most part I will be walking through those instructions getting my own project set up. For reference, my project will be using Webpacker with Rails 6x.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Getting started with &lt;code&gt;react-rails&lt;/code&gt; is fairly simple and straightforward. If you have ever used Rails' built in generator tools a lot of what we will be doing will look very familiar. There are just a few configurations we will have to set up to get React hooked into our Rails project correctly. &lt;/p&gt;

&lt;p&gt;The first thing we will want to do is create a new Rails project. This can be done most easily using the &lt;code&gt;new&lt;/code&gt; Rails command in the terminal which will also allow you to set up some additional configuration if you'd like.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails new my-app -T&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I like to use the &lt;code&gt;-T&lt;/code&gt; flag when building personal projects as it tells Rails not to initialize the project with auto generated test files. I find they're generally unnecessary for personal projects, and it also leaves you with the option to use testing tools that aren't Rails default later on if you choose. &lt;/p&gt;

&lt;p&gt;You can also use the &lt;code&gt;-d&lt;/code&gt; flag to set your project up with a different database tool, as well as any other configuration flags you might want.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Gems
&lt;/h2&gt;

&lt;p&gt;At this point there are two gems that we want to set up in our project: &lt;code&gt;webpacker&lt;/code&gt; and &lt;code&gt;react-rails&lt;/code&gt;. Go ahead and add those two to your Gemfile to include them as dependencies within your project. &lt;code&gt;webpacker&lt;/code&gt; may already be included from using the project generator (it was in my case), so just make sure that both gems are there and you're good to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and Setup
&lt;/h2&gt;

&lt;p&gt;Next we're going to make sure that any gems that we added to the Gemfile in the above step get installed properly. Go ahead and run &lt;code&gt;bundle install&lt;/code&gt; in the terminal to do so.&lt;/p&gt;

&lt;p&gt;After we make sure everything is good with bundler and our gems, we're going to run a series of Rails commands to get everything hooked up properly. A command or two may already be configured from the initial project generation, but it doesn't hurt to run all of them just to make sure we have everything set up properly. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails webpacker:install&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails webpacker:install:react&lt;/code&gt;&lt;br&gt;
&lt;code&gt;rails generate react:install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Running the above three commands might take a little bit of time to get everything configured, but after the have successfully run Rails should now be ready to start accepting our React components through it's pipeline.&lt;/p&gt;

&lt;p&gt;We should now have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an &lt;code&gt;app/javascript/components/&lt;/code&gt; directory&lt;/li&gt;
&lt;li&gt;ReactRailsUJS setup in &lt;code&gt;app/javascript/packs/application.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;an &lt;code&gt;app/javascript/packs/server_rendering.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;components&lt;/code&gt; directory is where all of our React components will live. ReactRailsUJS makes our Javascript available to the window and allows us access to event handling, lifecycle methods, control over mounting and unmounting, and much more. Finally, the &lt;code&gt;server_rendering.js&lt;/code&gt; file is for server side rendering.&lt;/p&gt;

&lt;p&gt;The last thing we will need to do is link the JavaScript pack. If you are using Rails 6, this will have been added by default so you can skip this step. For Rails versions below 6, you will simply place the helper below in the head tag of your &lt;code&gt;application.html.erb&lt;/code&gt; file below turbolinks:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;%= javascript_pack_tag 'application' %&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Component Time
&lt;/h2&gt;

&lt;p&gt;Now that we have all of the setup and configuration set up, we can move on to finally making and rendering some components! &lt;code&gt;react-rails&lt;/code&gt; provides us with an easy to use generator similar to what you might be used to while creating Rails controllers or models. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g react:component HelloWorld greeting:string&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The generator follows standard Rails convention. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first part &lt;code&gt;rails g&lt;/code&gt; denotes we are using a rails generator. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;react:component&lt;/code&gt; tells Rails we want to generate a new React component. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In this example &lt;code&gt;HelloWorld&lt;/code&gt;is the name of our component. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally &lt;code&gt;greeting:string&lt;/code&gt; tells Rails that the component should have one prop called &lt;code&gt;greeting&lt;/code&gt; that is of type &lt;code&gt;string&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By default, components will be added to the &lt;code&gt;app/javascript/components/&lt;/code&gt; directory. You can also specify a subdirectory for the component to be added to instead by appending it to the component name in the generator command like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g react:component my_subdirectory/HelloWorld greeting:string&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now all that is left is to render our component in a view. For the purposes of testing everything to make sure it works, I just set my &lt;code&gt;root&lt;/code&gt; route to an index action within the default &lt;code&gt;ApplicationController&lt;/code&gt; with a matching &lt;code&gt;index.html.erb&lt;/code&gt; file to play around with rendering. Regardless of what view you use, rendering your component should be the same. We will use the &lt;code&gt;react_component&lt;/code&gt; helper in our view file like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;react_component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"HelloWorld"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;greeting: &lt;/span&gt;&lt;span class="s2"&gt;"Hello from react-rails."&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helper takes two arguments. The first is the name of the component we want to render as a string, the second is an object with key value pairs corresponding to prop name and values to be passed to our component. If we start our server with &lt;code&gt;rails s&lt;/code&gt; and navigate to whichever page we rendered our component to, we should be greeted with the message "Hello from react-rails.".&lt;/p&gt;

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

&lt;p&gt;That's all there is to it. We now have a Rails project in which we can use React components directly in our views. There are many things we can do from here, but this setup now provides us with the ability to create and use whatever components we would like for our project.&lt;/p&gt;

&lt;p&gt;If you'd like to get started creating your own &lt;code&gt;react-rails&lt;/code&gt; project check out some of the resources below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/reactjs/react-rails"&gt;&lt;code&gt;react-rails&lt;/code&gt; documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/quick-code/simple-rails-crud-app-with-react-frontend-using-react-rails-gem-b708b89a9419"&gt;Simple Rails CRUD app with React Frontend, using ‘react-rails’ gem&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-ruby-on-rails-project-with-a-react-frontend"&gt;How To Set Up a Ruby on Rails Project with a React Frontend&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>rails</category>
      <category>gem</category>
    </item>
    <item>
      <title>Building A Basic Discord Bot</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 10 Nov 2020 01:58:30 +0000</pubDate>
      <link>https://forem.com/kjdowns/building-a-basic-discord-bot-5fgf</link>
      <guid>https://forem.com/kjdowns/building-a-basic-discord-bot-5fgf</guid>
      <description>&lt;p&gt;If you have a gaming hobby like me, chances are you have heard of or use the service Discord. For those not in the know, Discord is a voice-chat, messaging, and video streaming application. I have been using Discord for a few years now to chat and hang out with friends while gaming, but I was always intrigued by the bots that you can invite to your servers. These bots are programmed to perform specific actions in response to certain messages typed in text channels. &lt;/p&gt;

&lt;p&gt;I always wanted to learn how to make my own, and now that I have the coding knowledge to do so, I decided to take a crack at it. In this post, I'll be walking through the process of setting up my very first Discord bot: demo-bot (original, I know). Granted, this is a very simple bot that just responds to specific messages with a random reply - but it's a great starting point for learning how coding a bot works. This walkthrough will be using JavaScript, specifically the &lt;code&gt;discord.js&lt;/code&gt; library. You will need to have Node installed if you'd like to code along.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Before we can get into actually coding, there are a few things we will need to set up first. The first thing you will need is a Discord account. If you don't have one, it's easy to sign up for free &lt;a href="https://discord.com/"&gt;here&lt;/a&gt;. After creating an account and logging in, you will need to navigate to the &lt;a href="https://discord.com/developers/applications"&gt;developer portal&lt;/a&gt; at the bottom of the page.&lt;/p&gt;

&lt;p&gt;Here you will find documentation as well as an applications tab. To create a new application, simply click on the "new application" button! You will be prompted to enter a name for your bot, and once you click save you should be able to access various settings for our new application.&lt;/p&gt;

&lt;p&gt;There are a lot of settings here that you can feel free to play around with, but the first thing we want to do is add the actual bot to the application. You can do this by clicking the bot tab, then the create bot button, and confirm. You will see a hidden token that is very important for connecting to our bot in our code. We can grab this in a little bit. You will also want to navigate back to the "General Information" tab and copy the client id there. We will be using this to invite our bot to our server.&lt;/p&gt;

&lt;p&gt;There is one last step before we get to coding - we have to invite the bot to a server! For this I recommend creating your own test server so that you don't clog up a shared server when testing and debugging. To invite just pop the following url in your browser, substituting your own Client ID. Select the server you want to invite to and hit ok!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&amp;amp;scope=bot&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Get to Coding!
&lt;/h2&gt;

&lt;p&gt;The first thing we have to do is set up our environment. Run the command &lt;code&gt;npm init&lt;/code&gt; to generate your &lt;code&gt;package.json&lt;/code&gt; file. We will also need an &lt;code&gt;index.js&lt;/code&gt; file - this is where we will be writing all of our bot code. &lt;/p&gt;

&lt;p&gt;At this point I would also recommend creating a &lt;code&gt;.gitignore&lt;/code&gt; and &lt;code&gt;.env&lt;/code&gt; file as well as installing &lt;code&gt;dotenv&lt;/code&gt; by running &lt;code&gt;npm install dotenv&lt;/code&gt;. If you are working on your own local environment for testing purposes this isn't necessary, but it is best practice to hide sensitive variables like our Bot ID - especially if you are backing up your work on github.&lt;/p&gt;

&lt;p&gt;The last package we will need is &lt;code&gt;discord.js&lt;/code&gt; which will provide us an easy way to interface with the Discord API using JavaScript. Just run &lt;code&gt;npm install discord.js&lt;/code&gt; to install the package.&lt;/p&gt;

&lt;h2&gt;
  
  
  OK, Can We Actually Code Now?
&lt;/h2&gt;

&lt;p&gt;Alright, the time has finally come! Open up that &lt;code&gt;index.js&lt;/code&gt; file and lets start getting things set up. The first thing we want to do is get &lt;code&gt;discord.js&lt;/code&gt; hooked up to our index file as well as create a &lt;code&gt;client&lt;/code&gt; variable that we can operate on and use to connect to our bot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Discord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;discord.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Discord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I like to set up my global variables at the top as well, so under the above code I am also going to create a &lt;code&gt;replies&lt;/code&gt; variable that will be an array containing all of the replies that the bot can send.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Variables&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;replies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="err"&gt;Aye Aye, Captain! 🦀🦀🦀&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="err"&gt;I can't hear youuuuu! 🦀🦀🦀 &lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="err"&gt;Who lives in a 🍍 under the sea?&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we are going to write some setup code that needs to run before anything else. We are first going to run our &lt;code&gt;dotenv&lt;/code&gt; config so that we can access the variables in our &lt;code&gt;.env&lt;/code&gt; file. Then we are going to call the &lt;code&gt;login()&lt;/code&gt; method on our &lt;code&gt;client&lt;/code&gt; variable to start our bot. This method will take our secret Bot Token as an argument so that it knows which bot to connect to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Setup&lt;/span&gt;
&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dotenv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;BOT_TOKEN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point we are ready to run our bot! Granted it doesn't do anything yet, but we can still run it nonetheless. If you type &lt;code&gt;node index.js&lt;/code&gt; into your terminal it will launch the bot. You might notice that nothing is happening - let's go ahead and close the bot using &lt;code&gt;ctrl + c&lt;/code&gt; and add in an action that will let us know for sure our bot is ready.&lt;/p&gt;

&lt;p&gt;For responding to events, we will be using the &lt;code&gt;on()&lt;/code&gt; method with our &lt;code&gt;client&lt;/code&gt; variable. This method is similar to event handlers in JavaScript and takes two arguments. The first is a string that is the type of event we will be responding to and the second is a callback that will be executed in response to the event. &lt;/p&gt;

&lt;p&gt;Let's set it up so that when the bot is connected and ready we print out "Ready!" to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ready&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ready!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we try &lt;code&gt;node index.js&lt;/code&gt; again, after a second we should see "Ready!" pop up in the terminal!&lt;/p&gt;

&lt;p&gt;The second action the we need to be concerned with is "message". This event is triggered when a message is received in a text channel. it will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;replyMessage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"message" here is the event we will be responding to and &lt;code&gt;replyMessage&lt;/code&gt; is the callback we will be passing in that will execute when the "message" event is triggered. This is the main functionality of our bot so I'm going to provide the function code and then walk through what it is doing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Functions&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;replyMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;TEST_CHANNEL&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;arrr you ready kids?!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;replies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;replies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we can see that the function takes in a &lt;code&gt;msg&lt;/code&gt; object as an argument. This object contains a ton of metadata, and I suggest console logging it to get a better look at its properties, but for now we are only going to be concerned with two: &lt;code&gt;channel.id&lt;/code&gt; and &lt;code&gt;content&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The content property is pretty self explanatory, it is just the text of the message. We are using &lt;code&gt;toLowerCase()&lt;/code&gt; to make the check case insensitive and then checking in our if statement if it matches the message we want to respond to. &lt;/p&gt;

&lt;p&gt;The channel.id property is optional, but in this case our if statement is also checking that the message came from a specific channel. We are doing this in this example to restrict the bot to responding only to a specific channel. If you have developer mode turned on in your Discord application, you can grab a channel's ID by right clicking on the channel name and selecting "copy id".&lt;/p&gt;

&lt;p&gt;Now that we have checked to make sure the message that we have is the one we want our bot to respond to, we can dive into actually making the bot respond. First we create an index variable that will pick a random number within the range of the length of our &lt;code&gt;replies&lt;/code&gt; array. To reply, we simply call &lt;code&gt;reply()&lt;/code&gt; on our &lt;code&gt;msg&lt;/code&gt; object and pass in the message to respond with. In our case we will be passing in the index of our array equal to our randomly generated number. &lt;/p&gt;

&lt;p&gt;This will send an @ reply to the user that posted the message with our bots response text. You can also do this without the @ reply, just sending a regular message to the chat. Instead of &lt;code&gt;reply&lt;/code&gt; we would use &lt;code&gt;send&lt;/code&gt; and it would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;replies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will simply send the reply to the same channel from which the bot received the message.&lt;/p&gt;

&lt;h2&gt;
  
  
  TaDa! Beep Boop 🤖
&lt;/h2&gt;

&lt;p&gt;That's all there is to it! Granted this was a very simple "hello World" level application, but it covers the basics of getting started and can help you get started. There are so many more possibilities for building really robust bots that you can use within your own communities, or even publish for the public to invite to their servers. I hope you enjoyed, Happy Coding!&lt;/p&gt;

&lt;p&gt;For a deeper look, check out the official documentation:&lt;br&gt;
&lt;a href="https://discord.com/developers/docs/intro"&gt;Discord&lt;/a&gt;&lt;br&gt;
&lt;a href="https://discord.js.org/#/docs/main/stable/general/welcome"&gt;discord.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should also check out a great tutorial series that helped me get started:&lt;br&gt;
&lt;a href="https://www.youtube.com/playlist?list=PLRqwX-V7Uu6avBYxeBSwF48YhAnSn_sA4"&gt;The Coding Train&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>bot</category>
      <category>discord</category>
      <category>gaming</category>
    </item>
    <item>
      <title>Creating my first Chrome Extension</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 03 Nov 2020 02:31:32 +0000</pubDate>
      <link>https://forem.com/kjdowns/creating-my-first-chrome-extension-2mhb</link>
      <guid>https://forem.com/kjdowns/creating-my-first-chrome-extension-2mhb</guid>
      <description>&lt;p&gt;Last week, after doing some research and learning a bit about how Chrome extensions work, I posted a write up on what the Manifest File is and how to use it. This week, I expanded on that and created my very first (very simple) Chrome extension. This post will go through the process of creating this extension, and talk about what I wanted to create and how I went about doing so. Since I already did a write up on the Manifest File, I will not be going too in depth on what that is. Feel free to check out my post from last week &lt;a href="https://dev.to/kjdowns/chrome-extensions-manifest-file-47he"&gt;here&lt;/a&gt; if you want to learn more about that specifically. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why and What Did I Create?
&lt;/h2&gt;

&lt;p&gt;I had what I felt like was a great interview last week, but it seemed like after a week without an update that I was most likely being ghosted. I decided that I would reach out to the people that I interviewed with for an update. I didn't want to just ask for an update though, I also wanted to show them how dedicated I was and that hiring me should be an obvious decision. I took some time to think what I could create that would leave an impression but also be done relatively quickly.&lt;/p&gt;

&lt;p&gt;The company was a relatively large marketing company, so I decided on two features that could possibly be split into two extensions and expanded on in the future. One was for the consumer side that changed the plain text names of their brands into hyperlinks that would take you to the website for that brand. The other feature was imagined more for employees or account managers and consisted of a popup quick menu that provided one click access to client sites. I imagined this could be expanded into a quick access portal for a client information hub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Manifest File
&lt;/h2&gt;

&lt;p&gt;Let's get into the code! The first and most important thing you need to worry about when creating an extension is the Manifest File. This contains all of the information that Chrome needs to make sure all the parts of your extension work together. This is a file that every extension, at minimum, must have. &lt;/p&gt;

&lt;p&gt;You can see what my Manifest looks like in its totality below. I am presenting it all at once in its final iteration for simplicity, but when creating your own you may find yourself adding to or removing from this file as you are working. I have removed company specific information as it's not particularly relevant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Chrome Extension&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Demo extension that linkifies brands on the web and provides a quick bookmark to brand sites.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;manifest_version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;browser_action&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;default_popup&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;popup.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;content_scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;matches&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;all_urls&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;content.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first four fields are required. They provide a name and description of the extension, a version for facilitating updates, and the version of the manifest file we are using. (As of now this should almost always be 2)&lt;/p&gt;

&lt;p&gt;Since I wanted this extension to work across the entire web, I chose to use the &lt;code&gt;browser_action&lt;/code&gt; field, inside which I specify which file to be used for the popup feature. Since this was just a prototype project, I opted not to include &lt;code&gt;icon&lt;/code&gt; information. The default letter icon worked for me, but you may want to think about including that information in your own project.&lt;/p&gt;

&lt;p&gt;For the web page functionality portion, I used the &lt;code&gt;content_scripts&lt;/code&gt; field to specify which JS file to uses when pages load. Inside &lt;code&gt;content_scripts&lt;/code&gt;, the &lt;code&gt;matches&lt;/code&gt; field specifies which pages you want your JS to run on. In this case I wanted it to run on all urls. The documentation has great resources on how to include and exclude specific sites if you want to do something different. Lastly, the &lt;code&gt;js&lt;/code&gt; tag just tells the file where to find my JS file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Popup Menu
&lt;/h2&gt;

&lt;p&gt;As it was the more simple feature to implement, let's talk about the Popup Menu. My idea for this feature was a quick access popup menu with clickable buttons. The buttons would provide one click access to whatever information was needed. For this demo, I chose individual brand websites. &lt;/p&gt;

&lt;p&gt;Creating a popup menu for your extension is as simple as creating an &lt;code&gt;html&lt;/code&gt; file. Provide the &lt;code&gt;html&lt;/code&gt; file that you want to the Manifest using the &lt;code&gt;default_popup&lt;/code&gt; field and Chrome will automatically display your html file as a popup when the extension's tray icon is clicked. You are also able to link CSS and JS files in the HTML file head like a regular HTML file.&lt;/p&gt;

&lt;p&gt;As I wanted to keep things simple, I decided to link a single CSS file to handle styling. The contents were a simple heading, brief description, and then a list of icons wrapped in hyperlink tags. I've provided a snippet below of what my code looks like with just a few links.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;Linker Extension&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;em&amp;gt;&lt;/span&gt;Quick links to Brand websites!&lt;span class="nt"&gt;&amp;lt;/em&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"links-wrapper"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"links-list"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"links-item"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"link-container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;LINK_HERE&amp;gt;"&lt;/span&gt; &lt;span class="na"&gt;target=&lt;/span&gt;&lt;span class="s"&gt;"blank"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;ICON_FILE&amp;gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"links-item"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"link-container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;LINK_HERE&amp;gt;"&lt;/span&gt; &lt;span class="na"&gt;target=&lt;/span&gt;&lt;span class="s"&gt;"blank"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;ICON_FILE&amp;gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Text Replacement
&lt;/h2&gt;

&lt;p&gt;The second feature, and the one that took me the longest time to figure out how to implement, was replacing plain text instances of brand names on web pages with a link to that specific brand's website. This feature went through a few iterations until I found the way that worked for me, but let's talk a bit about what I wanted to happen and how I decided to do it.&lt;/p&gt;

&lt;p&gt;When you visit a page with an extension enabled, after loading the DOM, Chrome then runs whatever JS file is responsible for the extension's behavior. In my case, I needed to parse the text within the DOM for instances of the brands I was working with, generate a hyperlink with the matching text and target site, and then replace that bit of text in the DOM with my new hyperlink.&lt;/p&gt;

&lt;p&gt;Since I only had a handful of brands to worry about, I decided to create an array of objects - one for each brand. They would have a name and url key that I could use to easily find and use the information that I needed. The final result looked something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;brands&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;BRAND_NAME_1&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;BRAND_URL_1&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;BRAND_NAME_2&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;BRAND_URL_2&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I needed to figure out how to grab all of the elements on the page that had text in them. I wanted to keep it simple and not really have to worry about breaking things on the page, so I decided to only grab &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;span&lt;/code&gt; tags as they were the most likely to contain the text I wanted to grab without worrying about breaking existing styling or navigating nested elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nodes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p,span&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script contains one reusable function which I used to generate the link tag that we will be inserting into the DOM. Since I already had all of the brand information I needed as objects, the function accepts a brand object as an argument and returns a string that will serve as our hyperlink element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;generateLinkElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brandObject&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;a href="&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;brandObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;" target="blank"&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;brandObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, I needed to implement the actual search and replace operation that will be the basic functionality. It is a simple loop through all of the elements that I grabbed from the DOM. For each one, I then loop through each brand object in my &lt;code&gt;brands&lt;/code&gt; array. If the brand name is included, it replaces the text with the link element generated by our &lt;code&gt;generateLinkElement()&lt;/code&gt; function. If not, it continues down the chain until all brands have been checked against each element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;brands&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replaceAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;generateLinkElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;brand&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;While this is certainly a simple project, and there are probably more optimal/ better ways to implement it, I definitely learned a lot. It was quite fun to challenge myself to learn something new in a short time frame. If you are interested in creating your own Chrome extension or learning more about how to do it, I highly recommend checking out the official &lt;a href="https://developer.chrome.com/extensions"&gt;docs&lt;/a&gt;. They are really quite thorough and do a great job of breaking down what you need.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Chrome Extensions - Manifest File</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 27 Oct 2020 03:01:26 +0000</pubDate>
      <link>https://forem.com/kjdowns/chrome-extensions-manifest-file-47he</link>
      <guid>https://forem.com/kjdowns/chrome-extensions-manifest-file-47he</guid>
      <description>&lt;p&gt;Recently, I have been learning and playing around with creating browser extensions with Google Chrome. For those not familiar, a browser extension is a small program that extends the functionality of the browser. This can add additional functionality or behavior to the browser based on an individuals needs or preferences. Using the Chrome Web Store, developers can write their own extensions and upload them for use by anyone who wants to download and use. Creating an extension for Chrome is pretty simple, anyone with knowledge of HTML, CSS, and JavaScript can do so.&lt;/p&gt;

&lt;p&gt;In this post, I would like to talk about and walk through some of the details of the Manifest File. This is the file that all Chrome extensions start with and provides important information that the extension needs to work properly. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Manifest File
&lt;/h2&gt;

&lt;p&gt;The Manifest file is in a way, the blueprint for your extension. In many ways it is similar to the &lt;code&gt;package.json&lt;/code&gt; file that you may be familiar with in JavaScript that provides a list of all of your dependencies. This file is JSON format and includes all of the important information that your extension will need. In fact, every extension must have one of these files.&lt;/p&gt;

&lt;p&gt;The are many different fields within the manifest and we will be talking about a few of them shortly. There are a a few that are required, many that are recommended, and a whole load that are optional depending on what you want your extension to do. Let's take a look at what an example Manifest File would look like from the Chrome docs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;manifest.json&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Getting Started Example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Build an Extension!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"manifest_version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Required Fields
&lt;/h2&gt;

&lt;p&gt;There are three fields that are required for every Manifest file: &lt;code&gt;manifest_version&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;version&lt;/code&gt;. These are relatively simple and straightforward, but lets take a look at what each one is used for.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;manifest_version&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This field's value is an integer that specifies the version of the manifest file format required by your project. As of Chrome 18, you should specify a value of &lt;code&gt;2&lt;/code&gt;. While version 1 should be consider depreciated, version 2 is not yet required. However, Chrome has stated that they will stop supporting depreciated manifest versions soon. You can check out the differences in file format versions &lt;a href="https://developer.chrome.com/extensions/manifestVersion"&gt;here&lt;/a&gt; if you're curious.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;name&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This one is easy - &lt;code&gt;name&lt;/code&gt; is just a string value that serves as the primary identifier of the extension. It has a maximum character limit of 45 and is displayed in the install dialog, extension management UI, and Chrome Web Store. &lt;/p&gt;

&lt;p&gt;There is also an optional &lt;code&gt;short_name&lt;/code&gt; field that is a shortened version of the extension's &lt;code&gt;name&lt;/code&gt;. It has a max character count of 12 and is used in places where there is not enough space for the full &lt;code&gt;name&lt;/code&gt;. If this isn't specified, a likely truncated value of &lt;code&gt;name&lt;/code&gt; will be used.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;version&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This field's value is self explanatory, it is the version of the extension itself. The value consists of one to four dot separated integers and have a few rules: they much be between 0 and 65535, inclusive, and non-zero integers can't start with 0. If the published extension has a newer version string than the installed extension, it will automatically be updated to the newest version.&lt;/p&gt;

&lt;p&gt;There is another optional field similar to &lt;code&gt;short_name&lt;/code&gt; called &lt;code&gt;version_name&lt;/code&gt;. It can be set to a descriptive string and will be used for display purposes. An example would be &lt;code&gt;"version_name": "1.0 beta"&lt;/code&gt;. If this field is not present, &lt;code&gt;version&lt;/code&gt; will be used for display purposes as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recommended Fields
&lt;/h2&gt;

&lt;p&gt;There are also three fields in the Manifest documentation that are listed as "recommended" fields. These are &lt;code&gt;default_locale&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt;, and &lt;code&gt;icons&lt;/code&gt;. While Chrome will not stop you from leaving these three fields out of your file, in almost all situations you will want to include them.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;default_locale&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This field is used for internalization within your extension. This concept is a bit outside of this post, but if you are curious to learn more, check out Google's overview &lt;a href="https://developer.chrome.com/webstore/i18n"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Essentially, this field is used to support multiple different languages or locales within your extension. Inside of a &lt;code&gt;_locales&lt;/code&gt; directory, you are able to include a &lt;code&gt;messages.json&lt;/code&gt; for each language your extension will support. The extension will then be able to pick the proper messages to display to the user based on locale. This field allows you to specify which locales are supported in your extension.&lt;/p&gt;

&lt;p&gt;Note that this field becomes required if your extension contains a &lt;code&gt;_locales&lt;/code&gt; directory and must be absent if it does not.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;description&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The value of this field should be a plain text string. It has a limit of 132 characters and as I'm sure you can guess, serves as a description for the extension. It is used in both the browser's extension management UI and the extension's page in the Chrome Web Store.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;icons&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This field specifies (you guessed it) icons that represent the extension. The value should be formatted as an object with key value pairs of icon size and file name respectively. &lt;/p&gt;

&lt;p&gt;Per the docs, you should always provide a 128x128 icon as it is used during the installation of the extension and by the Chrome Web Store. You should also provide a 48X48 icon which will be used in the extension management page. Optionally, you can also provide a 16X16 icon to be used as a favicon. Note that you can provide any size icons you wish and Chrome will do its best, but it is recommended to use the stated sizes for the best results.&lt;/p&gt;

&lt;p&gt;Icons should also generally be in PNG format, but Chrome will accept any format supported by WebKit. Below is an example of what the icons field should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"icons"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"16"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"icon16.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
           &lt;/span&gt;&lt;span class="nl"&gt;"48"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"icon48.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"128"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"icon128.png"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pick one (or none)
&lt;/h2&gt;

&lt;p&gt;There are two other fields that are technically optional, but will almost always be used. However, you may only use one per extension and which one you use will depend on the intended functionality of what you are building. The two fields are &lt;code&gt;browser_action&lt;/code&gt; and &lt;code&gt;page_action&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;browser_action&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This field should be used when the functionality of your extension makes sense on most web pages. Think of this action as the go to for extensions that you generally want to be available at the browser level or on most web pages a user interacts with. Say you want to create an extension that will translate highlighted words on a webpage, that would be a prime candidate for a &lt;code&gt;browser_action&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This field will put an icon in the main toolbar, to the right of the address bar. The value should be provided as an object which can contain the fields &lt;code&gt;default_icon&lt;/code&gt;, &lt;code&gt;default_title&lt;/code&gt;, and &lt;code&gt;default_popup&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;default_icon&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;default_icon&lt;/code&gt; field is used to specify an icon to be displayed on the main toolbar. For best results it is recommended to use a 16 device independent pixel size icon. In order to display icons where screen pixel density is different than one, you can provide multiple sizes and Chrome will select the best fit.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;default_title&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This field is used to set a tooltip to be displayed for your extension. This field is pretty simple and the value should be a string.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;default_popup&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If this field is specified, a popup will appear when the user clicks on the toolbar icon. The value of this field should be the file name/ path of the HTML file that you wish to use for the popup. The popup can contain any HTML that you would like it to and is automatically sized to fit the contents of the file. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;browser_action&lt;/code&gt; Example
&lt;/h2&gt;

&lt;p&gt;If the above seems a bit confusing in text, take a look below at an example implementation of the &lt;code&gt;browser_action&lt;/code&gt; field from the Chrome docs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"My extension"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"browser_action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"default_icon"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"16"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"images/icon16.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"24"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"images/icon24.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"32"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"images/icon32.png"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"default_title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Google Mail"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"default_popup"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"popup.html"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;page_action&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;page_action&lt;/code&gt; field is the counterpart to &lt;code&gt;browser_action&lt;/code&gt;. It should be used for extensions where the functionality makes sense for only a few specific pages. This field will also create an icon to the right of the address bar and can make use of the same fields as &lt;code&gt;browser_action&lt;/code&gt;. The main difference with this field is that the icon can be grayed out on pages where the extension cannot be used. This can be toggled by using the &lt;code&gt;pageAction.show&lt;/code&gt; and &lt;code&gt;pageAction.hide&lt;/code&gt; methods. &lt;/p&gt;

&lt;h2&gt;
  
  
  Optional Fields
&lt;/h2&gt;

&lt;p&gt;The Manifest file can accept a large number of optional fields, and largely including these will be based on the individual functionality and needs of your extension. There are certainly way too many to include in a single blog post, but you should check out the Manifest File documentation &lt;a href="https://developer.chrome.com/extensions/manifest"&gt;here&lt;/a&gt; if you're curious to learn more.&lt;/p&gt;

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

&lt;p&gt;I hope this breakdown of the Manifest file used in Chrome extensions has helped you understand it's use a bit better. Happy Coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>chrome</category>
      <category>extension</category>
    </item>
    <item>
      <title>Iterating in JavaScript</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 20 Oct 2020 03:10:01 +0000</pubDate>
      <link>https://forem.com/kjdowns/iterating-in-javascript-24mh</link>
      <guid>https://forem.com/kjdowns/iterating-in-javascript-24mh</guid>
      <description>&lt;p&gt;Last week I decided to create a cheat sheet of sorts for iterating over Arrays in JavaScript. Its aim was to list and explain briefly all of the most commonly used built in methods for iterating or looping over Arrays. As I was thinking of what I should write about this week, I kept coming back to the concept of iterating or looping and thinking about how core of a concept it is in programming as a whole. &lt;/p&gt;

&lt;p&gt;Indeed, looping is one of the first concepts taught in almost any language, and is generally the first big concept one has to wrap their heads around before progressing in learning how to program. I decided this week would be a great time to step back and gear a post toward those that are where I was about a year ago and just trying to wrap their heads around this concept. In the following post, I will be discussing the concept of iteration as well as looking at the ways in which we use it in JavaScript specifically. &lt;/p&gt;

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

&lt;p&gt;So if we want to talk about iteration (also known as looping) we have to know what it is first, right? Plainly, iteration is the process by which you do something repeatedly, generally with some kind of condition that stops the process. We encounter this kind of looping all the time, though less explicit in the real world. &lt;/p&gt;

&lt;p&gt;For a real world example, think of meal prep. Say you are making mashed potatoes and the recipe tells you peel them all. Now we as humans can infer that the recipe means for you to take each potato and peel it. In this instance you are performing an action (peeling) until a certain condition is fulfilled (there are no more unpeeled potatoes).&lt;/p&gt;

&lt;p&gt;If we wanted to program this action we have to be a bit more specific. We can define an action &lt;code&gt;peelPotato()&lt;/code&gt; which tells the computer how to do what we want. Unfortunately, computers cannot infer like humans can and so if we tell it &lt;code&gt;peelPotato()&lt;/code&gt; it will do just that and peel exactly one potato. &lt;/p&gt;

&lt;p&gt;Now certainly we could count the number of potatoes that we have and then tell the computer &lt;code&gt;peelPotato()&lt;/code&gt; that number of times, but what happens if the number of potatoes changes? We would have to rewrite our entire recipe program. Not to mention the fact that our program would quickly become messy and unwieldy if we do this. This is where iteration and loops come in. We can define a condition (number of potatoes) and tell the computer that it should perform &lt;code&gt;peelPotato()&lt;/code&gt; for as long as that condition is true (we still have potatoes to be peeled). &lt;/p&gt;

&lt;p&gt;This example is a bit rough and lacking some detail, but I hope it manages to relay the idea well enough. So what are the various ways that we can loop in JavaScript? Let's go through them one by one below.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;for&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; statement is the most basic and generic of all the looping statements. It is created using the keyword &lt;code&gt;for&lt;/code&gt;, followed by three optional expressions (this is where the condition is specified), followed by a code block (this is where your action is). This might be a bit confusing without seeing it so lets look at the basic syntax below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's first look at the three optional expressions in parenthesis after &lt;code&gt;for&lt;/code&gt;. First we are declaring a variable &lt;code&gt;i&lt;/code&gt; and setting its initial value to &lt;code&gt;0&lt;/code&gt;. This is going to be our counter variable that we will use to check our exit condition. &lt;/p&gt;

&lt;p&gt;The second expression &lt;code&gt;i &amp;lt; 10&lt;/code&gt; is our exit condition. Every time our loop starts, JavaScript will check to see if our counter variable &lt;code&gt;i&lt;/code&gt; is less than &lt;code&gt;10&lt;/code&gt;. If it is, we will loop another time - if it is not, we will break out of our loop and continue to the rest of our program.&lt;/p&gt;

&lt;p&gt;The third expression &lt;code&gt;i++&lt;/code&gt; is pretty simple. It tells our program to add &lt;code&gt;1&lt;/code&gt; to the value of our counter at the end of every loop. This lets us keep track of how many times we have gone through the loop and provides and updated value for our condition to check against at the start of every loop.&lt;/p&gt;

&lt;p&gt;Finally we have the code block that is between &lt;code&gt;{}&lt;/code&gt;. This is just the code that we want to execute every time we loop. You can put whatever you want here, but for this example we are just console logging the value of &lt;code&gt;i&lt;/code&gt; on every loop. Let's see how we could use our potato example in a &lt;code&gt;for&lt;/code&gt; loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;potatoCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// number of potatoes we have&lt;/span&gt;

&lt;span class="c1"&gt;// as long as our counter is less than our potatoes, peel away!&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;potatoCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;peelPotato&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep these basic concepts in mind as we continue. Many of the other methods utilize these same principles in different ways or for different situations.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;while&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A while statement is very similar to the &lt;code&gt;for&lt;/code&gt; statement. The biggest difference is that it only takes a single condition which it will check before every loop. If the condition evaluates to true, the loop will continue - if false, it will exit. &lt;/p&gt;

&lt;p&gt;You can even create a &lt;code&gt;for&lt;/code&gt; loop using a &lt;code&gt;while&lt;/code&gt; by creating your own counter variable - you just have to make sure to increment it yourself. The biggest advantage to using &lt;code&gt;while&lt;/code&gt; is that it allows you to use &lt;code&gt;boolean&lt;/code&gt; (true and false) values as a condition. Let's take a look at some examples!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// basic while loop with boolean&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;loop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;We're looping!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// mimicking a for loop&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// potato example&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasPotatoes&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;peelPotato&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;do...while&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;do...while&lt;/code&gt; loop is almost identical to the &lt;code&gt;while&lt;/code&gt; loop. The only difference is that the code to be executed comes first and is run before the condition is checked. This means that the code will always execute at least once, even if the condition starts false. This loop is really great for when you have some code that you know needs to run at least once, but might need to run more than once. Example time!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// basic do...while&lt;/span&gt;
&lt;span class="c1"&gt;// even though i starts not greater than 0, this code will still &lt;/span&gt;
&lt;span class="c1"&gt;// run one time&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// potato example&lt;/span&gt;
&lt;span class="c1"&gt;// careful, this will peel at least one time even if we have no &lt;/span&gt;
&lt;span class="c1"&gt;// potatoes!&lt;/span&gt;
&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;peelPotato&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;hasPotatoes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;There you have it, the three most common ways in which we are able to iterate in JavaScript! I hope that if iteration is a concept that you have been struggling with as start your programming journey, that this explanation helps you understand a bit better. While you might be new to programming, never be afraid to take a look at some documentation. If you want to learn more you should check out the docs &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements"&gt;here&lt;/a&gt;. The iterators I talk about here are under the &lt;code&gt;Iterations&lt;/code&gt; section, and you'll even find some more advanced ones that I didn't cover. Good luck and happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Array Methods for Iteration in JavaScript</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 13 Oct 2020 01:48:04 +0000</pubDate>
      <link>https://forem.com/kjdowns/array-methods-for-iteration-in-javascript-37kk</link>
      <guid>https://forem.com/kjdowns/array-methods-for-iteration-in-javascript-37kk</guid>
      <description>&lt;p&gt;If you have worked with Arrays in JavaScript before, you know that you will often have to loop through or iterate over the data in the Array. This can be done relatively simply with a &lt;code&gt;for&lt;/code&gt; loop or a &lt;code&gt;for...of&lt;/code&gt; loop, but outside of relatively simple operations this can get quite messy. Having to keep track of counter variables and making sure the increments are correct can quickly bog down your code and make things unnecessarily complicated. Luckily JavaScript has a bunch of built in methods that automatically loop over every element in an Array, and many of them are custom tailored to a specific frequently used operation. This week, I'd like to create a quick reference guide for some of the most commonly used built in Array methods for iteration.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;forEach&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;forEach()&lt;/code&gt; method is the most general built in method. It takes a callback function as an argument and executes it on each element in the array. There are a few optional arguments for this method, but we're going to keep it simple for now. &lt;/p&gt;

&lt;p&gt;For this method and all of the ones we will be talking about, arrow functions allow us to define the callback directly in the argument. The callback should take an array element as an argument. If this sounds confusing, take a look at the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;map&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;map()&lt;/code&gt; method is useful when you would like to modify all of the items in an Array in some way. Like &lt;code&gt;forEach()&lt;/code&gt; it takes a callback as an argument. It will return a new array with the results of executing the function on every item in the Array. Take a look at the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubleArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doubleArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [2, 6, 10, 14]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;includes&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;includes()&lt;/code&gt; method is useful when you would like to find out if a specific value exists among the entries in an Array. It takes the query value as an argument and returns either &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; depending on whether the value is found or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;indexOf&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Great, so we can find out if a value exists within our Array, but what if you need to find out where it is for some reason? That's where &lt;code&gt;indexOf()&lt;/code&gt; comes in. Like &lt;code&gt;includes()&lt;/code&gt;, it takes in a specific value as an argument, but will return the first index where it finds the value. It will return &lt;code&gt;-1&lt;/code&gt; if the value is not present. You can also optionally provide an index from which to start the search. Take a look at the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;find&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;find()&lt;/code&gt; method does exactly what the name implies. It finds the first value in an Array that satisfies the passed in testing function and returns it. Say for example that we would like to find the first element in our array with a value less than 5. The &lt;code&gt;find()&lt;/code&gt; method is the perfect method to do so. Keep in mind that &lt;code&gt;find()&lt;/code&gt; will only return the first value in the array that satisfies the test. We will talk a little later about how to get multiple values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;filter&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;filter()&lt;/code&gt; method is basically the "full" version of find. What I mean is that &lt;code&gt;filter()&lt;/code&gt; works the same &lt;code&gt;find&lt;/code&gt; except that it will return a new Array containing the values of all of the elements that match the passed in testing function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [1, 3]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;sort&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;sort()&lt;/code&gt; method is a bit more complicated than the previous methods we talked about. The default use is simple enough - it sorts the elements of an Array in place and returns the sorted array with the order defaulting to ascending. Optionally you can supply a compare function that defines a specific sort order. This is the complicated bit, and could probably have its own blog post. If you want to learn more about custom compare functions check out the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort"&gt;documentation&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;days&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tues&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Weds&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Thurs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;days&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// ["Mon", "Thurs", "Tues", "Weds"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;reduce&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;reduce()&lt;/code&gt; method is in my opinion the hardest of these built in methods to grasp. It takes in a reducer function and executes it on every element, returning a single value. A good example of when this method would be appropriate is if you wanted to add all of the numbers in an array together.&lt;/p&gt;

&lt;p&gt;The reducer method takes an accumulator and current value as an argument, and optionally takes in a starting index (default is 0) and the array &lt;code&gt;reduce()&lt;/code&gt; was called on. If you would like to get a more in depth understanding, take a look at the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;docs&lt;/a&gt;. Let's look at a simple example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;accum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 24&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;These are the most commonly used methods for iterating over arrays. I hope you found this little cheat sheet/ guide useful. If you would like to learn more about built in Array methods, check out the documentation &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Classes in JavaScript</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 06 Oct 2020 01:54:46 +0000</pubDate>
      <link>https://forem.com/kjdowns/classes-in-javascript-37d6</link>
      <guid>https://forem.com/kjdowns/classes-in-javascript-37d6</guid>
      <description>&lt;p&gt;This week I wanted to go back a bit to some of the concepts that I had learned during my time at bootcamp but never really took the time to look deeper at. One of those concepts in JavaScript is classes. Learning the basics of Object-Oriented programming, classes were a pretty straightforward concept for me. Creating code based representations of what your data is trying to represent seems like a pretty easy way to think of things. The thing about JavaScript though, is that though it supports the paradigm of Object Oriented Programming, it is not itself an Object Oriented language. It does this through classes like many Object Oriented languages, but things work a little bit differently since JavaScript is a Procedural language.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Class in JavaScript?
&lt;/h2&gt;

&lt;p&gt;To understand what classes are we have to talk a little bit about how JavaScript handles data. Almost everything in JavaScript is an &lt;code&gt;Object&lt;/code&gt;, with the exclusion of primitive data types. Classes in JavaScript are just a template for creating objects that encapsulate data. If you're thinking, "Hey that sounds a lot like a function!", you'd be right. Classes are "special functions" and just like functions they can be defined using an expression or a declaration. If that sounds a bit confusing lets take a look at some examples compared to functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Function declaration&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Function expression&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Class declaration&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//Class expression&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;puppy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;Keep in mind that although classes are special functions, they do not follow the same hoisting rules as functions. This means that you will have to declare your class before you call it unless you want a nasty &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Class Body
&lt;/h2&gt;

&lt;p&gt;The body of a class, like the body of a function, is the part between the curly braces. This is where the magic is so to speak. You can define methods, constructors, and other such things that might be useful for your class.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;constructor&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;One of the most important aspects defined in the body of a class is the &lt;code&gt;constructor&lt;/code&gt; method. This method is a special one that allows you to define how your class will be created and initialized. If you look at the constructor for the &lt;code&gt;Dog&lt;/code&gt; class up above, it defines how instances of the &lt;code&gt;Dog&lt;/code&gt; class will be constructed. It takes one argument, a name, and initializes the &lt;code&gt;name&lt;/code&gt; property to the passed in value. Note that you can only have one method with the name "constructor" per class.&lt;/p&gt;

&lt;p&gt;If your class has a "super" or "parent" class, you can also use the &lt;code&gt;super&lt;/code&gt; keyword to call the constructor of the super class.&lt;/p&gt;

&lt;h3&gt;
  
  
  Methods
&lt;/h3&gt;

&lt;p&gt;Methods are a way in which you can define actions for your class. There are four types of methods: standard "instance" methods, static methods, getter methods, and setter methods. Let's look at some examples of each of these and then talk a bit more about them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;side&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// standard method&lt;/span&gt;
  &lt;span class="nx"&gt;areaMessage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`The area of this square is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// static method&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;areaFormula&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A square's area is the length of it's side squared&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// getter method&lt;/span&gt;
  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// setter method&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nx"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;side&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Standard methods work just like regular methods. The can be called on an instance of a class to execute the code inside specific to that instance of the class. In the example above, if we were to call the &lt;code&gt;areaMessage&lt;/code&gt; method on an instance of the &lt;code&gt;Square&lt;/code&gt; class, it would return a string with a message telling you the square's area.&lt;/p&gt;

&lt;p&gt;Static methods are class level methods. The cannot be called by individual instances of the class, only the class itself. They are generally used for utility methods that do not require a specific instance of the class. In the example above, the &lt;code&gt;areaFormula&lt;/code&gt; method returns a message that explains the area formula for squares in general.&lt;/p&gt;

&lt;p&gt;Getter methods allow us to define pseudo properties for our class. For example, in a &lt;code&gt;Square&lt;/code&gt; class you might want an &lt;code&gt;area&lt;/code&gt; property - but the area of a square will change if the length of its sides change. Instead of creating an area property in the constructor that could become inaccurate if other data changes, we can just create a getter method that will return the value of performing area calculations on the square's &lt;code&gt;side&lt;/code&gt; value. &lt;/p&gt;

&lt;p&gt;Setter methods are similar to getter methods, except that they change a value instead of returning one. For our area example, it might be useful to be able to set the area pseudo-property directly. Since our getter method is based on the value of the square's side, we can just reverse the getter process and set the &lt;code&gt;side&lt;/code&gt; value to the square root of the area. Now whenever we change the &lt;code&gt;area&lt;/code&gt; or the &lt;code&gt;side&lt;/code&gt; value, we can be sure that both values will be updated correctly.&lt;/p&gt;

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

&lt;p&gt;If you want to learn more about classes and object oriented programming in JavaScript, definitely take a look at the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"&gt;MDN Docs&lt;/a&gt;. There are some great experimental features that are worth checking out such as private and public field declarations. Happy Coding!&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>React Native: Basic Components</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 29 Sep 2020 01:39:01 +0000</pubDate>
      <link>https://forem.com/kjdowns/react-native-basic-components-5hep</link>
      <guid>https://forem.com/kjdowns/react-native-basic-components-5hep</guid>
      <description>&lt;p&gt;Over the past week I've been digging a little bit into React Native. Since I have been focusing mainly on React for Front-End Development, I thought that the jump to React Native for mobile development would be a natural progression. This week, I'd like to take you through some of the basic components that are provided and how they compare to using React for web development.&lt;/p&gt;

&lt;p&gt;React is a library that makes it simple to create complex (or simple) web applications by breaking down all of the pieces into components. If you'd like to read a bit about React Components to familiarize yourself before reading on, feel free to check out the article I wrote a few weeks ago &lt;a href="https://dev.to/kjdowns/what-is-a-react-component-4ejd"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;React Native provides the same functionality, except instead of using web components to create the building blocks of you application, each React Native component maps to native components. Note that since we are working with Native, your components will not return JSX or HTML, but instead native components. This post will guide you through some of the most used Native components with comparisons to ReactJS if applicable.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;View&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;View&lt;/code&gt; component is the most fundamental component in your React toolbox. View is a container component and maps directly to the equivalent native view for whichever platform you're developing. If would be equivalent to a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; in web development and just like a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; can be nested inside other &lt;code&gt;&amp;lt;View&amp;gt;&lt;/code&gt;s and have children of any type. Take a look at an example &lt;code&gt;&amp;lt;View&amp;gt;&lt;/code&gt; might look like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;WelcomeScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;Text&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;You may have noticed in the code example above that I snuck in an extra component. That's the &lt;code&gt;Text&lt;/code&gt; component and it is used for displaying text on screen. These can also be nested, and are more or less equivalent to a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Image&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Image&lt;/code&gt; component is very similar to the &lt;code&gt;Text&lt;/code&gt; component, except that it is used for displaying images instead of text. It is similar to &lt;code&gt;&amp;lt;img/&amp;gt;&lt;/code&gt;. You can specify the image to be displayed using the &lt;code&gt;source&lt;/code&gt; prop, passing in either a &lt;code&gt;require()&lt;/code&gt; function with the image file or an object with the image uri. Take a look below for an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ImageExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./image.jpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="nx"&gt;used&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Image&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://image.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="nx"&gt;used&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;TextInput&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This component allows the user to input text into our application via the keyboard. It is very similar to &lt;code&gt;&amp;lt;input type="text"&amp;gt;&lt;/code&gt;. There are a number of props that can provide some additional functionality and configuration, but we're going to keep it simple. The most simple use case is presenting the user with an input text box and using an &lt;code&gt;onChangeText&lt;/code&gt; to read the data. Let's take a look at what a simple component using this would look like.&lt;/p&gt;

&lt;p&gt;(If you want to know more about the hook used in this example, check out my post about React Hooks &lt;a href="https://dev.to/kjdowns/react-hooks-1g1d"&gt;here&lt;/a&gt;.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;UselessTextInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onChangeText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Useless Placeholder&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt;
      &lt;span class="nx"&gt;onChangeText&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;onChangeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;ScrollView&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ScrollView&lt;/code&gt; is very similar to the &lt;code&gt;View&lt;/code&gt; component, except as I'm sure you can guess it gives us a view with a scrollbar. Note that it must have a bounded height or you will run into some issues. This is most easily done by making sure that it's parents have a bounded height. You can also set the property directly, but this is discouraged in the documentation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ScrollView&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ScrollExample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ScrollView&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;Lorem&lt;/span&gt; &lt;span class="nx"&gt;ipsum&lt;/span&gt; &lt;span class="nx"&gt;dolor&lt;/span&gt; &lt;span class="nx"&gt;sit&lt;/span&gt; &lt;span class="nx"&gt;amet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;consectetur&lt;/span&gt; &lt;span class="nx"&gt;adipiscing&lt;/span&gt; &lt;span class="nx"&gt;elit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sed&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
          &lt;span class="nx"&gt;eiusmod&lt;/span&gt; &lt;span class="nx"&gt;tempor&lt;/span&gt; &lt;span class="nx"&gt;incididunt&lt;/span&gt; &lt;span class="nx"&gt;ut&lt;/span&gt; &lt;span class="nx"&gt;labore&lt;/span&gt; &lt;span class="nx"&gt;et&lt;/span&gt; &lt;span class="nx"&gt;dolore&lt;/span&gt; &lt;span class="nx"&gt;magna&lt;/span&gt; &lt;span class="nx"&gt;aliqua&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Ut&lt;/span&gt; &lt;span class="nx"&gt;enim&lt;/span&gt; &lt;span class="nx"&gt;ad&lt;/span&gt;
          &lt;span class="nx"&gt;minim&lt;/span&gt; &lt;span class="nx"&gt;veniam&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quis&lt;/span&gt; &lt;span class="nx"&gt;nostrud&lt;/span&gt; &lt;span class="nx"&gt;exercitation&lt;/span&gt; &lt;span class="nx"&gt;ullamco&lt;/span&gt; &lt;span class="nx"&gt;laboris&lt;/span&gt; &lt;span class="nx"&gt;nisi&lt;/span&gt; &lt;span class="nx"&gt;ut&lt;/span&gt;
          &lt;span class="nx"&gt;aliquip&lt;/span&gt; &lt;span class="nx"&gt;ex&lt;/span&gt; &lt;span class="nx"&gt;ea&lt;/span&gt; &lt;span class="nx"&gt;commodo&lt;/span&gt; &lt;span class="nx"&gt;consequat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Duis&lt;/span&gt; &lt;span class="nx"&gt;aute&lt;/span&gt; &lt;span class="nx"&gt;irure&lt;/span&gt; &lt;span class="nx"&gt;dolor&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
          &lt;span class="nx"&gt;reprehenderit&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;voluptate&lt;/span&gt; &lt;span class="nx"&gt;velit&lt;/span&gt; &lt;span class="nx"&gt;esse&lt;/span&gt; &lt;span class="nx"&gt;cillum&lt;/span&gt; &lt;span class="nx"&gt;dolore&lt;/span&gt; &lt;span class="nx"&gt;eu&lt;/span&gt; &lt;span class="nx"&gt;fugiat&lt;/span&gt; &lt;span class="nx"&gt;nulla&lt;/span&gt;
          &lt;span class="nx"&gt;pariatur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Excepteur&lt;/span&gt; &lt;span class="nx"&gt;sint&lt;/span&gt; &lt;span class="nx"&gt;occaecat&lt;/span&gt; &lt;span class="nx"&gt;cupidatat&lt;/span&gt; &lt;span class="nx"&gt;non&lt;/span&gt; &lt;span class="nx"&gt;proident&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sunt&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
          &lt;span class="nx"&gt;culpa&lt;/span&gt; &lt;span class="nx"&gt;qui&lt;/span&gt; &lt;span class="nx"&gt;officia&lt;/span&gt; &lt;span class="nx"&gt;deserunt&lt;/span&gt; &lt;span class="nx"&gt;mollit&lt;/span&gt; &lt;span class="nx"&gt;anim&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="nx"&gt;est&lt;/span&gt; &lt;span class="nx"&gt;laborum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ScrollView&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  StyleSheet
&lt;/h2&gt;

&lt;p&gt;This last component is used a bit differently than the other components we have talked about before. You wont be seeing something like &lt;code&gt;&amp;lt;StyleSheet&amp;gt;&lt;/code&gt;, instead you will be using it to create a &lt;code&gt;StyleSheet&lt;/code&gt; object that can be used as an abstraction similar to &lt;code&gt;CSS&lt;/code&gt;. The object generally contains key names with values that are themselves objects. Those values contain key value pairs that are similar to &lt;code&gt;CSS&lt;/code&gt; properties and values. &lt;/p&gt;

&lt;p&gt;You can pass these objects into components as the style prop using dot notation to provide styling for your components. This method is preferable to providing in line styles since the StyleSheet component automatically validates for property names. It also looks a lot cleaner and helps to separate concerns.&lt;/p&gt;

&lt;p&gt;If that all seems a little confusing, let's take a look at what a simple example would look like in practice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;WelcomeScreen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Text&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/View&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;container&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;textAlign&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;center&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;fontSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There you have it: a basic overview of some of the most commonly used components in React Native. If you'd like to get a little more in depth about what you can do with these different components, check out the &lt;a href="https://reactnative.dev/docs/components-and-apis"&gt;Components documentation&lt;/a&gt; or to learn more about React Native in general take a look at the &lt;a href="https://reactnative.dev/docs/getting-started"&gt;Official documentation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Basic Regex</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 22 Sep 2020 00:56:48 +0000</pubDate>
      <link>https://forem.com/kjdowns/basic-regex-2ge3</link>
      <guid>https://forem.com/kjdowns/basic-regex-2ge3</guid>
      <description>&lt;p&gt;Regex, short for regular expression is a useful tool for searching out patterns in strings. They can be used in string searching methods such as &lt;code&gt;find()&lt;/code&gt; and &lt;code&gt;replace()&lt;/code&gt; as well as in input validation to match a specific pattern. If you are like me, you may have come across regex before while trying to manipulate substrings and got scared off by seemingly confusing syntax. Well good news! Regex isn't nearly as complicated as it looks, and is a great tool for writing clean and concise pattern matches when working with strings. Below I'm going to lay out the basics of regex in a hopefully simple to understand manner. &lt;/p&gt;

&lt;p&gt;Note: I will be using JavaScript for the purposes of this post, though the concepts can be used in almost any language. Also, this guide will only focus on the basics of regex so I will not be talking about more advanced patterns like look aheads and capture groups.&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods - &lt;code&gt;test()&lt;/code&gt; and &lt;code&gt;match()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The first thing I want to talk about are the two methods that I will be using - &lt;code&gt;test()&lt;/code&gt; and &lt;code&gt;match()&lt;/code&gt;. You can use regex in a wide variety of built in string methods, but we're going to keep it simple today. Both of these methods are called on a string and take a regex pattern as an argument. The main difference between the two is the return value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;test()&lt;/code&gt;, as the name implies, tests a regex pattern against a string and returns true if it finds a match and false if it does not.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;match()&lt;/code&gt; is very similar, except it returns an array of the matched substrings if a match is found and null if not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/Hello/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["Hello"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that regex patterns can be either stored in a variable or just input directly as an argument. I think storing them in variables looks cleaner so I will be using them that way in this guide.&lt;/p&gt;

&lt;h2&gt;
  
  
  Literal Patterns
&lt;/h2&gt;

&lt;p&gt;The most simple pattern you can match for is a literal one. You can see an example of this in the code snippet above where we are just matching for the string &lt;code&gt;"Hello"&lt;/code&gt;. To create a literal regex pattern all you have to do is put the word you want to match inside &lt;code&gt;//&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/javascript/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a javascript programmer.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see above, we are checking to see if the substring &lt;code&gt;"javascript"&lt;/code&gt; exists within the string &lt;code&gt;"I am a javascript programmer"&lt;/code&gt;. Pretty simple right? Let's get a little bit more complicated. What if we had multiple different languages we wanted to check for? We could use the "or" symbol &lt;code&gt;|&lt;/code&gt; to test if any of the languages we specify are within the string we want to test. If we use it with &lt;code&gt;match()&lt;/code&gt; instead of test we can also get the specific value that was matched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/javascript|ruby|java/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;js&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a javascript programmer.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ruby&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a ruby programmer.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;java&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a java programmer.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;js&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["javascript"]&lt;/span&gt;
&lt;span class="nx"&gt;ruby&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["ruby"]&lt;/span&gt;
&lt;span class="nx"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["java"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Flags - &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;So far we have some very basic literal patterns that we can match. This is great, but regex is case sensitive and only returns the first match found. Often we will want to match regardless of case and we will want to get all of the instances of our match. This is where regex flags come in. They can be added to the end of a regex pattern to denote rules for the entire pattern.&lt;/p&gt;

&lt;p&gt;Two of the most commonly used flags are &lt;code&gt;i&lt;/code&gt; to denote case insensitivity and &lt;code&gt;g&lt;/code&gt; to denote that you want every match in the string. It is also possible to combine flags together to denote multiple rules on your pattern.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The fox jumps over the dog at the park.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// This pattern will return the first case insensitive match&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;caseRegex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/the/i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;caseRegex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["The"]&lt;/span&gt;

&lt;span class="c1"&gt;// This pattern will return all case sensitive matches&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;multRegex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/the/g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;multRegex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["the", "the"]&lt;/span&gt;

&lt;span class="c1"&gt;// Combined will return all matches regardless of case&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;caseMultRegex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/the/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;caseMultRegex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["The", "the", "the"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Wildcard - &lt;code&gt;.&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we have covered literal patterns and flags, lets start talking about special characters. This is where the power of regex starts to shine. In a pattern we can use the &lt;code&gt;.&lt;/code&gt; in order to represent a wildcard. This &lt;code&gt;.&lt;/code&gt; is a stand in for any character. Say you wanted to match for any three letter word that starts with "b" and ends with "g". Take a look at the snippet below to see how we could use this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/b.g/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bugString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Look at this bug&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bagString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Look at this bag&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;bugString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["bug"]&lt;/span&gt;
&lt;span class="nx"&gt;bagString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["bag"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Multiple Characters - &lt;code&gt;[]&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, and &lt;code&gt;{}&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we've seen the simplest special character - the wildcard, lets talk a bit about some other special characters. The characters that we talk about in this section will allow us to select multiple characters in some for or another.&lt;/p&gt;

&lt;p&gt;Surrounding a set of characters with &lt;code&gt;[]&lt;/code&gt; will match for any of the characters within. This can be useful for example if you want to find all of the vowels in a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;vowels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;aeiou&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;vowels&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["e", "o", "o"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-&lt;/code&gt; character can be used inside of &lt;code&gt;[]&lt;/code&gt; to denote a range of characters that we want to match. Say for example we want to match all of the numbers in a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;0-9&lt;/span&gt;&lt;span class="se"&gt;]&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The value of pi is 3.14&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["3", "1", "4"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; characters are very similar in that they both allow you to specify if a specific character appears in succession. &lt;code&gt;+&lt;/code&gt; will specify that the character appears one or more times in succession while &lt;code&gt;*&lt;/code&gt; specifies zero or more times. Lets look at some examples to clarify.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This pattern specifies one or more&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Where is Mississippi?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["s", "ss", "ss"]&lt;/span&gt;

&lt;span class="c1"&gt;// This pattern specifies zero or more&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/ya*/g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I said yaaas yesterday.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["yaaa", "y"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The final symbol I want to talk about here is &lt;code&gt;{}&lt;/code&gt;. It is similar to &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; except that it allows you to specify a range or exact number of times you want a character to repeat. You can specify a min, min and max, or exact number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timidPirate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aargh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;normalPirate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aaaargh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;excitedPirate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Aaaaaaaaaaaaaargh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Specify exact number - we want a normal pirate&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="se"&gt;{4}&lt;/span&gt;&lt;span class="sr"&gt;/i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;timidPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;normalPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;excitedPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// false&lt;/span&gt;

&lt;span class="c1"&gt;// Specify minimum number - we don't want any timid pirates&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="se"&gt;{4,}&lt;/span&gt;&lt;span class="sr"&gt;/i&lt;/span&gt;

&lt;span class="nx"&gt;timidPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;normalPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;excitedPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="c1"&gt;// Specify min and max number - we only want timid and normal pirates&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/a&lt;/span&gt;&lt;span class="se"&gt;{2,4}&lt;/span&gt;&lt;span class="sr"&gt;/i&lt;/span&gt;

&lt;span class="nx"&gt;timidPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;normalPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;excitedPirate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Shorthand - &lt;code&gt;\w&lt;/code&gt;, &lt;code&gt;\d&lt;/code&gt;, and &lt;code&gt;\s&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Sometimes we want to be able to be able to specify a group of characters, say all digits. Regex provides us with a few shorthand characters that allow us to do so in a single character.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\w&lt;/code&gt; allows us to match any alphanumeric value and includes underscore. Its inverse &lt;code&gt;\W&lt;/code&gt; matches for all values &lt;em&gt;except&lt;/em&gt; alphanumeric and underscore.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\d&lt;/code&gt; matches for all digit values (0-9). Similarly &lt;code&gt;\D&lt;/code&gt; matches for all non digit values.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;\s&lt;/code&gt; matches for all whitespace values (spaces, tabs, line breaks). You can probably guess that &lt;code&gt;\S&lt;/code&gt; matches all non whitespace values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am 31!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Alphanumeric and non alphanumeric&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\w&lt;/span&gt;&lt;span class="sr"&gt;/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;antiRegex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\W&lt;/span&gt;&lt;span class="sr"&gt;/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["I", "a", "m", "3", "1"]&lt;/span&gt;
&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;antiRegex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [" ", " ", "!"]&lt;/span&gt;

&lt;span class="c1"&gt;// Digit and non digit&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\d&lt;/span&gt;&lt;span class="sr"&gt;/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;antiRegex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\D&lt;/span&gt;&lt;span class="sr"&gt;/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["3", "1"]&lt;/span&gt;
&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;antiRegex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["I", " ", "a", "m", " ", "!"]&lt;/span&gt;

&lt;span class="c1"&gt;// Whitespace and non whitespace&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&gt;/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;antiRegex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\S&lt;/span&gt;&lt;span class="sr"&gt;/ig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [" ", " "]&lt;/span&gt;
&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;antiRegex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// ["I", "a", "m", "3", "1", "!"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;p&gt;That's really all there is to basic regex. With the tools I talked about here you can begin to mix and match to start creating your own pattern matches. There are some a few more concepts that are a bit more complicated, and if you'd like to continue exploring the topic of regex I'd encourage you to take a look at them for even more powerful pattern matching. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Resources for more learning:&lt;/em&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=ZfQFUJhPqMM"&gt;Learn Regular Expressions (Regex)&lt;/a&gt;&lt;br&gt;
&lt;a href="https://regexr.com/"&gt;RegExr: Learn, Build, &amp;amp; Test RegEx&lt;/a&gt;&lt;/p&gt;

</description>
      <category>regex</category>
      <category>regularexpression</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React Hooks</title>
      <dc:creator>Kevin Downs</dc:creator>
      <pubDate>Tue, 15 Sep 2020 02:17:22 +0000</pubDate>
      <link>https://forem.com/kjdowns/react-hooks-1g1d</link>
      <guid>https://forem.com/kjdowns/react-hooks-1g1d</guid>
      <description>&lt;p&gt;This week I would like to take a look into a relatively new feature introduced to React: Hooks. If you are anything like myself, you may have been working with React for a bit and heard people talking about Hooks but never really looked into what they actually were. Maybe you filed them under that little folder in your brain labeled "I should probably check that out at some point" and went along working on the projects you were already involved with. Well the day came where I finally decided to double click that little folder and take a look, and I thought why not walk through it for others that are just clicking that little folder too.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's a Hook?
&lt;/h2&gt;

&lt;p&gt;Hooks are an addition made in React 16.8. In the simplest terms, they are functions that allow you to use state and life-cycle features inside functional components. They do not work inside of classes and are intended to allow you to use React without classes. React provides a few built-in hooks like &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;, but also allow you to create custom hooks. This makes it easy to share hooks between components and even to reuse stateful logic without the need to change your component hierarchy. &lt;/p&gt;

&lt;p&gt;There are two simple rules that you need to know about and follow when using hooks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Only call Hooks at the top level.&lt;/li&gt;
&lt;li&gt;Only call Hooks from React functions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The two rules echo the same principle that Hooks are strictly for use within functional React components at the top level. This means that you cannot use hooks inside loops or conditions somewhere in your class and that you cannot use them within class components. (Though this may sound a bit self explanatory as the entire idea behind hooks is to not use class components) They cannot be called within regular JavaScript functions, but can be called from custom hooks and of course inside functional components.&lt;/p&gt;

&lt;p&gt;Now that we have an idea of what Hooks are and what their purpose is, I'd like to show how one of the most used built in hooks can be used: &lt;code&gt;useState&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;useState&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;For this example, I'm going to include some example code from the React docs that explain the usage of &lt;code&gt;useState&lt;/code&gt;. You will see the same class in a traditional class component, and then the same class as a functional component using Hooks to gain access to the state functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example class component&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Example&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;You clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; Click me &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example functional component using Hooks&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Example&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;You clicked &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; times&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Click me
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;You can see that the two look pretty similar. The difference here is that we can avoid having to create a class component and dealing with the &lt;code&gt;constructor&lt;/code&gt; to gain access to state by importing the &lt;code&gt;useState&lt;/code&gt; Hook in a functional component. Let's take a look at how it works.&lt;/p&gt;

&lt;p&gt;First, we import the &lt;code&gt;useState&lt;/code&gt; hook:&lt;/p&gt;

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

&lt;p&gt;Now normally in a class component we would go into the &lt;code&gt;constructor&lt;/code&gt; function and assign &lt;code&gt;this.state&lt;/code&gt; to an object value of the data we want to have in our state. With Hooks though, we are able to call &lt;code&gt;useState&lt;/code&gt; to get the same effect. You may be a bit confused by the odd square bracket syntax so let's break down what exactly is going on here.&lt;/p&gt;

&lt;p&gt;The code: &lt;code&gt;const [count, setCount] = useState(0);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; declares a "state variable" that will be preserved between function calls in the same way state is preserved in a class component. It takes in an initial value as an argument and returns a pair of values: the current state and a function that updates it. We use array destructuring here to be able to use those returned values directly in our component. You can name these variables whatever you would like, but using the pattern "thing", "setThing" helps to keep things clean and organized. &lt;/p&gt;

&lt;p&gt;You are able to repeat this for as many state variables as you might need. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleWithManyStates&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Declare multiple state variables!&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFruit&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;banana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([{&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Learn Hooks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also see in the full component example above we can use these variables directly in our component code. We can just use &lt;code&gt;count&lt;/code&gt; to get its current values and use &lt;code&gt;setCount&lt;/code&gt; by passing in the value that &lt;code&gt;count&lt;/code&gt; should be updated to.&lt;/p&gt;

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

&lt;p&gt;React Hooks are a great way to reduce the use of class components in your code and allow for a lot of flexibility in terms of extracting and reusing bits of code that you otherwise would not be able to. There are a host of other Hooks as well that provide even more functionality such as &lt;code&gt;useEffect&lt;/code&gt; which allows you to avoid using a lot of different life-cycle methods. If you found this write up interesting, go ahead and take a look over at the React &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;docs&lt;/a&gt;. They provide a great wealth of information on different ways you can start using Hooks in your React projects!&lt;/p&gt;

&lt;p&gt;If you liked this post, feel free to follow me elsewhere on &lt;a href="https://twitter.com/AJBanderas"&gt;Twitter&lt;/a&gt;, &lt;a href="https://github.com/kjdowns"&gt;Github&lt;/a&gt;, or &lt;a href="https://www.linkedin.com/in/kevinjdowns/"&gt;LinkedIn&lt;/a&gt;. Happy Coding!&lt;/p&gt;

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