<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Megan Moulos</title>
    <description>The latest articles on DEV Community by Megan Moulos (@meganmoulos).</description>
    <link>https://dev.to/meganmoulos</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%2F911376%2F7ff40b47-f0ca-4698-a66b-40fc441b8d0e.png</url>
      <title>DEV Community: Megan Moulos</title>
      <link>https://dev.to/meganmoulos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meganmoulos"/>
    <language>en</language>
    <item>
      <title>Ruby on Rails Naming Conventions</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Wed, 12 Oct 2022 17:14:38 +0000</pubDate>
      <link>https://dev.to/meganmoulos/ruby-on-rails-naming-conventions-885</link>
      <guid>https://dev.to/meganmoulos/ruby-on-rails-naming-conventions-885</guid>
      <description>&lt;p&gt;David Heinemeier Hansson, the creator of Ruby on Rails, believed in the importance of &lt;strong&gt;convention over configuration&lt;/strong&gt;. In his own words: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Convention over configuration is the corner stone of Rails and a principle I hold dear. Stop wasting time configuring things where the differences do not matter. - from a &lt;a href="https://7php.com/interview-dhh/" rel="noopener noreferrer"&gt;2013 interview &lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're new to Rails, the specific naming conventions at first can be difficult to memorize and understand. Naming conventions are crucial and an important part of how Rails decides how to link elements of your data.  &lt;/p&gt;




&lt;h3&gt;
  
  
  Active Record Naming Conventions
&lt;/h3&gt;

&lt;p&gt;Let's dive right in. From the &lt;a href="https://guides.rubyonrails.org/active_record_basics.html#naming-conventions" rel="noopener noreferrer"&gt;Rails guides&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The model class name should use a singular word in the CamelCase form, while the table name must be pluralized and use the snake_case form. Active Record also recognizes irregular forms such as Person/people and Mouse/mice. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Model Class&lt;/strong&gt; - Singular with the first letter of each word capitalized (e.g., BookClub).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database Table&lt;/strong&gt; - Plural with underscores separating words (e.g., book_clubs).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that Active Record Associations also follow convention: has_many is followed by a plural while belongs_to is followed by a singular.&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%2Fuploads%2Farticles%2Fpro8rg6j1f8kkrl8r6qt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpro8rg6j1f8kkrl8r6qt.png" alt="active record code"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Ruby Naming Conventions
&lt;/h3&gt;

&lt;p&gt;Ruby naming conventions are as follows: &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%2Fuploads%2Farticles%2Fuo9xni78wbfgcn6yk6zz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuo9xni78wbfgcn6yk6zz.png" alt="Ruby conventions"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Rails Naming Conventions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Routes:&lt;/strong&gt;&lt;br&gt;
Routes should use lowercase, with underscores as needed between words.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;resources :controller_name_plural (example - resources :books)&lt;/li&gt;
&lt;li&gt;resource :controller_name_singluar (example - resource :author)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Controllers&lt;/strong&gt;&lt;br&gt;
Controllers should be CamelCase, plural, and append the word "Controller" at the end. From the &lt;a href="https://guides.rubyonrails.org/action_controller_overview.html#controller-naming-convention" rel="noopener noreferrer"&gt;Rails docs&lt;/a&gt;: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The naming convention of controllers in Rails favors pluralization of the last word in the controller's name, although it is not strictly required (e.g. ApplicationController). For example, ClientsController is preferable to ClientController, SiteAdminsController is preferable to SiteAdminController or SitesAdminsController, and so on.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Here is a quick &lt;strong&gt;cheat sheet&lt;/strong&gt; of naming conventions:&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%2Fuploads%2Farticles%2Ffdy6wwldbgetu8sa7t48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdy6wwldbgetu8sa7t48.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Remember: Naming conventions in Rails are crucial to the language's inner workings. For more information on this topic, see the official guides: &lt;a href="https://guides.rubyonrails.org/index.html" rel="noopener noreferrer"&gt;Rails Guides&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Random Data - Lava Lamps, TRNGs and PRNGs</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Fri, 07 Oct 2022 16:32:35 +0000</pubDate>
      <link>https://dev.to/meganmoulos/random-data-lava-lamps-trngs-and-prngs-47kh</link>
      <guid>https://dev.to/meganmoulos/random-data-lava-lamps-trngs-and-prngs-47kh</guid>
      <description>&lt;p&gt;You may be wondering, what do these retro-looking lava lamps and random data have in common? Randomness in computing is notoriously difficult to achieve. Random numbers are not a new phenomenon, nor are they delegated to the tech domain. Ancient Sumerians and Egyptians used dice as RNGs for games of chance. The random falling patterns of yarrow stalks were used for divination in ancient Chinese religion. As recently as the 1900s scientists used coin tosses, dice rolls, and even picking numbers out of hats to get random numbers for their research. A sequence is considered random if no patterns can be recognized in it. When you flip a coin or roll a pair of dice, you are using randomness to get an outcome - whether it's who will start a football game or who will win the jackpot.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4glMmsag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onbjr2zjfe28glxqeju6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4glMmsag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onbjr2zjfe28glxqeju6.jpg" alt="dice" width="880" height="662"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the 1990s, the need for random numbers for computing exploded. &lt;a href="https://web.archive.org/web/19971210213248/http://lavarand.sgi.com/"&gt;Lavarand&lt;/a&gt; was designed by Silicon Graphics in 1996 to generate pseudorandom numbers. US Patent 5,732,138: "Method for seeding a pseudo-random number generator with a cryptographic hash of a digitization of a chaotic system." The idea was to take photos of patterns made by the floating material of numerous lava lamps, and extract random data from the pictures to seed a number generator. Since 2017, the Cloudflare office in San Francisco has been using a wall of lava lamps, which help to encrypt the requests that go through Cloudflare, which make up a whopping 10% of all internet requests. In Cloudflare's Singapore office, the company uses a pellet of uranium in a glass bell jar (they assure everyone that it is a safe amount), and measures the release of isotopes over time with a geiger counter. This unpredictable data is known as &lt;strong&gt;entropy&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fe6guyIA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0isxwfjg8k6usg8qipt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fe6guyIA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0isxwfjg8k6usg8qipt.jpg" alt="lava lamp wall" width="880" height="1173"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Why are random numbers so important?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;"Everything we do to achieve privacy and security in the computer age depends on random numbers." - Simon Cooper, an encryption expert and author of &lt;em&gt;Building Internet Firewalls&lt;/em&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Random numbers are the crux of cryptography, and crucial to how we send and receive data on the internet. Random Number Generators or RNGs use physics, math, and the haphazard and irregular nature of the outside world to create sequences without recognizable patterns. As of 2003, the improved,  open-source RNG called &lt;a href="//www.lavarnd.org"&gt;LavaRnd&lt;/a&gt; replaces the iconic lava lamps with a webcam with a lens cap on. This seems bizarre, but there is a thermal "noise" emitted by the webcam that "is digitized and put through a hash algorithm that churns the number set, stripping unwanted sections of predictability,"- &lt;a href="https://www.wired.com/2003/08/random/"&gt;Tom McNichol&lt;/a&gt; stated. What that boils down to is a sequence of numbers that can be used in cryptography. &lt;/p&gt;

&lt;p&gt;Random number generation is an enormous industry and touches most parts of our internet-connected lives. Banking apps, online shopping, credit card encryption - all of these rely on random numbers. Even gambling has had a major upgrade by using better and longer sequences of numbers to improve win to loss randomness. Because of their nature, computers are terrible at coming up with random numbers by themselves. “Computers from the beginning have been designed to very reliable, very predictable,” says Cloudflare’s CEO Matthew Prince. “When you turn them on, they always do exactly the same thing and what they’re told to do."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Bfm-8lms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npfxbi407fcgsgu0bbnd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Bfm-8lms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npfxbi407fcgsgu0bbnd.jpg" alt="gambling machine" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  TRNGs and PRNGs
&lt;/h3&gt;

&lt;p&gt;There are True Random Number Generators, or &lt;strong&gt;TRNGs&lt;/strong&gt;, and Pseudo-Random Number Generators, most commonly known as &lt;strong&gt;PRNGs&lt;/strong&gt;. TRNGs rely on outside, physical, real-world phenomena to determine a number (like the radioactive isotopes or lava lamps above). Switzerland’s FourmiLab invented a &lt;a href="https://www.fourmilab.ch/hotbits/"&gt;HotBits&lt;/a&gt; generator which uses radioactive decay and the "quantum mechanical laws of nature" - from their webpage: "HotBits is an Internet resource that brings genuine random numbers, generated by a process fundamentally governed by the inherent uncertainty in the quantum mechanical laws of nature, directly to your computer in a variety of forms. HotBits are generated by timing successive pairs of radioactive decays detected by a Geiger-Müller tube interfaced to a computer." Other companies use things like atmospheric noise levels, weather and wind data, and other "truly" random natural phenomena. &lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://www.synopsys.com/designware-ip/technical-bulletin/true-random-number-generator-security-2019q3.html"&gt;Synopsys&lt;/a&gt;: "True random numbers are at the heart of any secure system and their quality contributes to the security strength of designs. Weak or predictable random numbers open the door for attacks that can compromise keys, intercept data, and ultimately hack devices and their communication." TRNGs are a necessary part of our growing reliance on big data and cryptography in general. Synopsys goes on to say:  &lt;/p&gt;

&lt;p&gt;True random numbers are required in a variety of security scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Key generation for various algorithms (symmetric, asymmetric, MACs) and protocols (SSL/TLH, SSH, WiFi, LTE, IPsec, etc.)&lt;/li&gt;
&lt;li&gt;Chip manufacturing (seeding device unique and platform keys)&lt;/li&gt;
&lt;li&gt;Initial values (for encryption and MAC algorithms, TCP packet values, etc.)&lt;/li&gt;
&lt;li&gt;Nonce generation and initial counter values for various cryptographic functions&lt;/li&gt;
&lt;li&gt;Challenges used for protocol authentication exchanges&lt;/li&gt;
&lt;li&gt;Randomization input for side channel countermeasure solutions for protecting against physical attacks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are standards that TRNGs must meet before being considered random enough for cryptographic applications. The US National Institute of Standards and Technology (NIST) agency has developed a set of NIST SP 800-90A/B/c standards and the German standards body, Bundesamt für Sicherheit in der Informationstechnik (BSI), has long had a separate set of RNG standards (AIS 20/31). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PRNGs&lt;/strong&gt; do &lt;em&gt;not&lt;/em&gt; offer absolute randomness. They are computer programs with algorithms which create seemingly random sequences, which appear random to humans. PRNGs can be faster, and are still widely used in many industries for things like simulation and modeling applications. TRNGs are usually less efficient and not always necessary for simple applications, although they are crucial for data encryption (and, arguably, gambling). &lt;/p&gt;

&lt;p&gt;Read more about RNGs and randomness in general:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Random_number_generation"&gt;Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/random-number-generator/"&gt;FreeCodeCamp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.redhat.com/en/blog/understanding-random-number-generators-and-their-limitations-linux"&gt;RedHat&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>datascience</category>
      <category>programming</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Ruby &amp; Active Record Associations</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Mon, 03 Oct 2022 16:52:08 +0000</pubDate>
      <link>https://dev.to/meganmoulos/ruby-active-record-associations-1ij7</link>
      <guid>https://dev.to/meganmoulos/ruby-active-record-associations-1ij7</guid>
      <description>&lt;p&gt;In this guide you will learn: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to declare associations between models using Active Record&lt;/li&gt;
&lt;li&gt;How to understand the different types of associations available with Active Record&lt;/li&gt;
&lt;li&gt;How to use the methods automatically added to your models after creating these associations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using associations with Active Record is very powerful and an important part of using Ruby with databases. An &lt;strong&gt;association&lt;/strong&gt; is a connection between two Active Record &lt;strong&gt;models&lt;/strong&gt;. These associations provide built-in methods to make your databases easier to work with. This walkthrough assumes that you already understand how to create migrations and models. &lt;/p&gt;

&lt;p&gt;There are 6 types of associations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;belongs_to&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;has_one&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;has_many&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;has_many :through&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;has_one :through&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;has_and_belongs_to_many&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To figure out which type of association that fits your needs, it is helpful to create an ** Entity Relationship Diagram (ERD)**. There are helpful tools online to create your own ERD quickly and easily, but you can also use pen and paper! &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.lucidchart.com/pages/examples/er-diagram-tool" rel="noopener noreferrer"&gt;Lucidchart&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.smartdraw.com/entity-relationship-diagram/er-diagram-tool.htm" rel="noopener noreferrer"&gt;Smartdraw&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dbdiagram.io/home" rel="noopener noreferrer"&gt;dbdiagram.io&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this walkthrough, we will use dbdiagram.io. &lt;/p&gt;




&lt;h2&gt;
  
  
  belongs_to, has_many
&lt;/h2&gt;

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

&lt;p&gt;The &lt;code&gt;belongs_to&lt;/code&gt; association sets up a connection between two models, where the instance of one model "belongs to" the second model. In this example we have one author who has written many books. In this case, each book &lt;code&gt;belongs_to&lt;/code&gt; an author. This association is made through the &lt;strong&gt;foreign key&lt;/strong&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: From &lt;a href="https://learn.co/lessons/activerecord-associations-review" rel="noopener noreferrer"&gt;Flatiron School docs&lt;/a&gt; - "Foreign keys are columns that refer to the primary key of another table. Conventionally, foreign keys in Active Record are comprised of the name of the model you're referencing, and _id. So for example if the foreign key was for a posts table it would be post_id." Read more about foreign keys at &lt;a href="https://www.theodinproject.com/lessons/ruby-on-rails-active-record-associations" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is the corresponding code:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
  &lt;span class="nx"&gt;belongs_to&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the &lt;code&gt;belongs_to&lt;/code&gt; association must use the singular term ("author"). From the &lt;a href="https://guides.rubyonrails.org/association_basics.html" rel="noopener noreferrer"&gt;official docs&lt;/a&gt;: "This is because Rails automatically infers the class name from the association name. If the association name is wrongly pluralized, then the inferred class will be wrongly pluralized too."&lt;/p&gt;

&lt;p&gt;The other side of the coin for this particular example, the author's &lt;code&gt;has_many&lt;/code&gt; relationship, 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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Author&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  belongs_to, has_one
&lt;/h2&gt;

&lt;p&gt;If we changed our example above so that each author &lt;em&gt;only&lt;/em&gt; wrote a single book, we could use the &lt;code&gt;has_one&lt;/code&gt; association:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Author&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;has_one&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;book&lt;/code&gt; is singular. This may seem intuitive, but it is very important to note when to use singular or plural cases when writing your associations. The Book model would remain the same in this case, reading &lt;code&gt;belongs_to: author&lt;/code&gt; in the singular.&lt;/p&gt;




&lt;h2&gt;
  
  
  has_many, through:
&lt;/h2&gt;

&lt;p&gt;This association is often used to set up a many-to-many connection with another model. The declaring model can be matched with instances of another model &lt;em&gt;through&lt;/em&gt; a third, connecting model. For example, imagine a hospital with doctors that see many patients &lt;em&gt;through&lt;/em&gt; the patients' appointments. The diagram would 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%2Fuploads%2Farticles%2Fsp5p3rdhp16dvndiltr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp5p3rdhp16dvndiltr0.png" alt="has many through"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each doctor has many patients &lt;em&gt;through&lt;/em&gt; the appointments table. The patient also has many doctors &lt;em&gt;through&lt;/em&gt; the appointments table. Here is the corresponding association code:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Doctor&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;patients&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;through&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Appointment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;belongs_to&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;doctor&lt;/span&gt;
    &lt;span class="nx"&gt;belongs_to&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointment&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Patient&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;doctors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;through&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then new join models are automatically created for the newly associated objects. &lt;/p&gt;




&lt;h2&gt;
  
  
  has_one, through:
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;has_one, through:&lt;/code&gt; association is similar to the &lt;code&gt;has_many, through:&lt;/code&gt; association because they both create join models automatically. The difference is in the syntax, and that there is a one to many relationship. In our example above, imagine a patient only had one doctor, through the patient's appointments:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Doctor&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;patients&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;through&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Appointment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;belongs_to&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;doctor&lt;/span&gt;
    &lt;span class="nx"&gt;belongs_to&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointment&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;

  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Patient&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;ActiveRecord&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;
    &lt;span class="nx"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
    &lt;span class="nx"&gt;has_one&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;doctor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;through&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;appointments&lt;/span&gt;
  &lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  has_and_belongs_to_many
&lt;/h2&gt;

&lt;p&gt;This association is rarely used, and there is a blog post titled "&lt;a href="https://flatironschool.com/blog/why-you-dont-need-has-and-belongs-to-many/" rel="noopener noreferrer"&gt;Why You Don’t Need Has_and_belongs_to_many Relationships&lt;/a&gt; explaining why. From the Rails docs: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you'll need to remember to create the joining table in the database).&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Bonus: Polymorphic Association
&lt;/h2&gt;

&lt;p&gt;Polymorphic association allows us to connect a model to multiple other models on a single association. Look at this table provided by the &lt;a href="https://guides.rubyonrails.org/association_basics.html" rel="noopener noreferrer"&gt;Rails Active Record documentation&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%2Fuploads%2Farticles%2Flgkvm35odj0d9ll4hif4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgkvm35odj0d9ll4hif4.png" alt="polymorphic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A polymorphic &lt;code&gt;belongs_to&lt;/code&gt; declaration sets up an interface that any other model can use. For example, from an instance of an Employee model, a collection of pictures can be retrieved using @employee.pictures. You could also retrieve @product.pictures by the same logic.&lt;/p&gt;

&lt;p&gt;Find more information, as well as helpful tips and tricks, in the &lt;a href="https://guides.rubyonrails.org/association_basics.html" rel="noopener noreferrer"&gt;official documentation here&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>React Hooks</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Wed, 14 Sep 2022 16:47:58 +0000</pubDate>
      <link>https://dev.to/meganmoulos/react-hooks-486a</link>
      <guid>https://dev.to/meganmoulos/react-hooks-486a</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hooks&lt;/strong&gt; were introduced to React 16.8. Hooks allow you to use things like state without writing a class-based component. In essence, Hooks are JavaScript functions that allow you to "hook into" React state and lifecycle features from function components. There are a number of built-in Hooks as well as the ability to write your own custom Hooks. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: It's important to remember that Hooks are backwards compatible, so you can use them with class components without a problem. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All React Hook names are prefixed with the word "use," like &lt;code&gt;useState&lt;/code&gt; or &lt;code&gt;useEffect&lt;/code&gt;. To use Hooks, you must import them first. You can enter any number of Hooks at the top of the file like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="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="nx"&gt;useEffect&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&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;h3&gt;
  
  
  Why use hooks?
&lt;/h3&gt;

&lt;p&gt;When compared to class components, hooks reduces the amount of code we need to write (and read). Take a look at this example: &lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1056594421079261185-1" src="https://platform.twitter.com/embed/Tweet.html?id=1056594421079261185"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1056594421079261185-1');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1056594421079261185&amp;amp;theme=dark"
  }



 &lt;/p&gt;

&lt;p&gt;With Hooks we can organize logic inside of a component. These units can be reusable, helping us avoid duplicated logic. &lt;a href="https://overreacted.io/" rel="noopener noreferrer"&gt;Dan Abramov&lt;/a&gt; writes that "Hooks apply the React philosophy (explicit data flow and composition) &lt;em&gt;inside&lt;/em&gt; a component, rather than just &lt;em&gt;between&lt;/em&gt; the components." Hooks are fully &lt;strong&gt;encapsulated&lt;/strong&gt;, and are a way to share stateful logic. &lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1057030555202535424-258" src="https://platform.twitter.com/embed/Tweet.html?id=1057030555202535424"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1057030555202535424-258');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1057030555202535424&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Here are some reasons to use Hooks from &lt;a href="https://www.smashingmagazine.com/author/shedrack-akintayo/" rel="noopener noreferrer"&gt;Shedrack Akintayo&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improved code reuse&lt;/li&gt;
&lt;li&gt;Better code composition&lt;/li&gt;
&lt;li&gt;Better defaults&lt;/li&gt;
&lt;li&gt;Sharing non-visual logic with the use of custom Hooks&lt;/li&gt;
&lt;li&gt;Flexibility in moving up and down the components tree&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Rules
&lt;/h3&gt;

&lt;p&gt;There are &lt;strong&gt;two important rules&lt;/strong&gt; for using Hooks: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Only call Hooks at the top level&lt;/strong&gt;. Don't call Hooks inside of loops, conditions, or nested functions. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Only call Hooks from React functions&lt;/strong&gt;. Do not call Hooks from regular JavaScript functions. You can and should call Hooks from React function components. You may also call Hooks from other custom Hooks. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;There is a plug-in called esLint which will enforce the rules of Hooks. Find and install it &lt;a href="https://www.npmjs.com/package/eslint-plugin-react-hooks" rel="noopener noreferrer"&gt;here&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You &lt;strong&gt;can&lt;/strong&gt; use multiple Hooks in a single component, and React will rely on the order in which Hooks are called. Dan Abramov also states: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The ability to pass data between Hooks make them a great fit for expressing animations, data subscriptions, form management, and other stateful abstractions. &lt;strong&gt;Unlike render props or higher-order components, Hooks don’t create a “false hierarchy” in your render tree.&lt;/strong&gt; They’re more like a flat list of “memory cells” attached to a component. No extra layers.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Built-in Hooks
&lt;/h3&gt;

&lt;p&gt;React has 10 built-in Hooks, here are some of the more commonly used ones: &lt;/p&gt;




&lt;h4&gt;
  
  
  useState
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;useState&lt;/code&gt; Hook is called inside of a function component to add local state to it. It allows you to create, update, and manipulate state inside the functional component it is used in. &lt;code&gt;useState&lt;/code&gt; returns a destructured array containing two values: the first is the current state; the second is a setter function used to update the state. &lt;code&gt;useState&lt;/code&gt; takes a single argument, which is it's initial state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is an example of a simple counter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="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="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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// The new state variable is called "count" here, and the initial value will be 0 since the counter will begin at 0.&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="nf"&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;The button was 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="nf"&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 to Count
          &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;The new state variable is called &lt;code&gt;count&lt;/code&gt;, and the setter function is called &lt;code&gt;setCount&lt;/code&gt;. You can call these two variables anything you'd like, but this is the typical naming convention: use "set" concatenated with the variable name. The initial state is set to 0 since we want the counter to begin at 0. The &lt;code&gt;setCount&lt;/code&gt; function will be called each time the button is clicked, and update the count variable by 1. React will remember the current value of &lt;code&gt;count&lt;/code&gt; between re-renders, and provide the most recent one to the function component. If we want to update it, we just call &lt;code&gt;setCount&lt;/code&gt; again. &lt;/p&gt;

&lt;p&gt;As you can see, &lt;code&gt;setCount&lt;/code&gt; was called inside of the function's return, namely in the JSX code of the button's &lt;code&gt;onClick&lt;/code&gt;. This is perfectly fine!  &lt;/p&gt;

&lt;p&gt;You can use the state &lt;br&gt;
Hook multiple times in a single function component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ManyStatesExample&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jimmy&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;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="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;35&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;favoriteColors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFavoriteColors&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: Updating a state variable replaces it's contents. &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h4&gt;
  
  
  useEffect
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; Hook replicates React's lifecycle methods, but inside of a functional component. The official React documentation states: "If you’re familiar with React class lifecycle methods, you can think of &lt;code&gt;useEffect&lt;/code&gt; Hook as &lt;code&gt;componentDidMount&lt;/code&gt;, &lt;code&gt;componentDidUpdate&lt;/code&gt;, and &lt;code&gt;componentWillUnmount&lt;/code&gt; combined." The &lt;code&gt;useEffect&lt;/code&gt; Hook lets you perform &lt;strong&gt;side effects&lt;/strong&gt; (such as external API interactions and data fetching). &lt;code&gt;useEffect&lt;/code&gt; accepts two arguments: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A function with the code to run&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;dependency array&lt;/strong&gt;, which tells the Hook how many times to run. If this is left out, the Hook will run after every render. Be careful, leaving out the dependency array can cause infinite loops! &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you only want to run the &lt;code&gt;useEffect&lt;/code&gt; Hook when certain values in your function have changed, you must pass the variables as dependencies into the array. If you supply an empty array, the Hook will only run once. This can be very useful for things such as fetch requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&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="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://somelink.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="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dependency array is empty because we only need to GET our data once. &lt;/p&gt;

&lt;p&gt;Sometimes the &lt;code&gt;useEffect&lt;/code&gt; Hook can require &lt;strong&gt;cleanup&lt;/strong&gt;, which is written about in some detail in the &lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;React docs&lt;/a&gt;. &lt;/p&gt;




&lt;h4&gt;
  
  
  useContext
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;useContext&lt;/code&gt; Hook allows you to make particular data accessible to all components throughout the whole of the application (consider it "global" in scope for a tree of React components). It works with the &lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;React Context API&lt;/a&gt;. This Hook is powerful because data can be passed across all components in an app, rather than having to manually pass a prop down through various levels to get data to a child. &lt;/p&gt;

&lt;p&gt;For example, let's say you have an array of objects, and would like to pass that information down to a child function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;menuItems&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;dish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;spaghetti and meatballs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$12.99&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="na"&gt;dish&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;steak and potatoes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$29.99&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MenuContext&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="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;menuItems&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We are creating a context object using &lt;code&gt;React.createContext&lt;/code&gt; and now can pass &lt;code&gt;menuItems&lt;/code&gt; to any components in the app. Each context needs to be enclosed in a Provider wrapper, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Menu&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="nc"&gt;MenuContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;menuItems&lt;/span&gt;&lt;span class="si"&gt;}&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="nc"&gt;DinnerMenu&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="nc"&gt;MenuContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&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;Now &lt;code&gt;DinnerMenu&lt;/code&gt; becomes the consuming component of the context, and can use the objects stored in the MenuItems array. When the Provider updates, the Hook will trigger a rerender with the latest context value. See React's &lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;advanced Context guide here&lt;/a&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  useReducer
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;useReducer&lt;/code&gt; can be thought of as an alternative to the &lt;code&gt;useState&lt;/code&gt; Hook. The &lt;code&gt;useReducer&lt;/code&gt; Hook can work with more complex logic, and lets you rerender the UI whenever values change within the Hook. It accepts two arguments: a reducer function and an initial state. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;useReducer(reducerFunction, initialState);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Much like &lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useReducer&lt;/code&gt; returns an array of two values, which can be destructured to: the current value of the state; and a dispatch function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatchFunction&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reducerFunction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;state&lt;/code&gt; is the current value of the initialState passed to the Hook. The &lt;code&gt;reducerFunction&lt;/code&gt; accepts the state and an action. These two arguments will determine how the value of the state will change. The &lt;code&gt;dispatchFunction&lt;/code&gt; is how an action is passed to the &lt;code&gt;reducerFunction&lt;/code&gt;. There is usually a switch statement with a number of cases to determine how the value of state will change. Here is an example of a simple counter from the &lt;a href="https://reactjs.org/docs/hooks-reference.html#usecontext" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;reducer&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;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;increment&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="na"&gt;count&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="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;decrement&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="na"&gt;count&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="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&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;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useReducer&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="nx"&gt;initialState&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;&amp;gt;&lt;/span&gt;
      Count: &lt;span class="si"&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;
      &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="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;decrement&lt;/span&gt;&lt;span class="dl"&gt;'&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;-&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;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="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;increment&lt;/span&gt;&lt;span class="dl"&gt;'&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;+&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;/&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;blockquote&gt;
&lt;p&gt;Note: React guarantees that &lt;strong&gt;dispatch&lt;/strong&gt; function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the &lt;code&gt;useEffect&lt;/code&gt; or &lt;code&gt;useCallback&lt;/code&gt; dependency list.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  useRef
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;useRef&lt;/code&gt; Hook allows you to persist values between renders. You can store a mutable value that does &lt;strong&gt;not&lt;/strong&gt; cause a re-render when updated. &lt;code&gt;useRef&lt;/code&gt; returns an Object called &lt;code&gt;current&lt;/code&gt;. Consider &lt;code&gt;useRef&lt;/code&gt; like a "box" that can hole a mutable value in this &lt;code&gt;current&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;You can use this Hook to keep track of previous state values, because &lt;code&gt;useRef&lt;/code&gt; persists it's values between renders. Consider the following example from &lt;a href="https://www.w3schools.com/react/react_useref.asp" rel="noopener noreferrer"&gt;W3Schools&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&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="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useRef&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&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="nx"&gt;ReactDOM&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-dom/client&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="nf"&gt;App&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;inputValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInputValue&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&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;previousInputValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="nx"&gt;previousInputValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;inputValue&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="nx"&gt;inputValue&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;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputValue&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange&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="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setInputValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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="si"&gt;}&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;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Current Value: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputValue&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&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;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Previous Value: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;previousInputValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&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;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See &lt;a href="https://dmitripavlutin.com/react-useref-guide/" rel="noopener noreferrer"&gt;this blog post&lt;/a&gt; by Dmitri Pavlutin for more information and use cases of &lt;code&gt;useRef&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Other Built-in Hooks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;useCallback&lt;/li&gt;
&lt;li&gt;useMemo&lt;/li&gt;
&lt;li&gt;useImperativeHandle&lt;/li&gt;
&lt;li&gt;useLayoutEffect&lt;/li&gt;
&lt;li&gt;useDebugValue&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Build Your Own Hooks
&lt;/h3&gt;

&lt;p&gt;Along with these Hooks you can write your own custom Hooks that let you extract component logic into reusable, sharable functions. Because both components and Hooks are functions, you can extract the logic to a third, shared function (a new Hook!). A &lt;strong&gt;custom Hook&lt;/strong&gt; is a JavaScript function whose name starts with "use" and that may call other Hooks. It still follows the rules of built-in Hooks, but you get to decide which arguments it will take and what it should return. &lt;/p&gt;

&lt;p&gt;For more information on building custom Hooks, see: &lt;a href="https://reactjs.org/docs/hooks-custom.html" rel="noopener noreferrer"&gt;Building Your Own Hooks&lt;/a&gt;. &lt;/p&gt;




&lt;h4&gt;
  
  
  Further Reading:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/hooks-overview.html" rel="noopener noreferrer"&gt;Hooks at a Glance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.telerik.com/kendo-react-ui/react-hooks-guide/" rel="noopener noreferrer"&gt;The Guide to Learning React Hooks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.smashingmagazine.com/2020/04/react-hooks-api-guide/" rel="noopener noreferrer"&gt;Getting Started With the React Hooks API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://blog.bitsrc.io/writing-your-own-custom-hooks-4fbcf77e112e" rel="noopener noreferrer"&gt;Writing Your Own Custom React Hooks&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>programming</category>
    </item>
    <item>
      <title>React - Introducing JSX</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Wed, 07 Sep 2022 02:26:52 +0000</pubDate>
      <link>https://dev.to/meganmoulos/react-introducing-jsx-1nal</link>
      <guid>https://dev.to/meganmoulos/react-introducing-jsx-1nal</guid>
      <description>&lt;h2&gt;
  
  
  What is JSX?
&lt;/h2&gt;

&lt;p&gt;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 jsx"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&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="nc"&gt;Nav&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;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is a header&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&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;This is some text&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;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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;JSX&lt;/strong&gt; stands for &lt;a href="https://facebook.github.io/jsx/"&gt;'JavaScript XML'&lt;/a&gt; and is a syntax extension for JavaScript. It is used to create DOM elements that are then rendered in the React DOM. Although it &lt;em&gt;looks&lt;/em&gt; like HTML, it is actually an XML-like syntax specifically written for use in React. Interestingly, JSX is not valid JavaScript either. JSX needs to be compiled by a tool like &lt;a href="https://babeljs.io/"&gt;Babel&lt;/a&gt; to be translated into regular JavaScript that a browser can understand. Put simply, JSX describes what the UI should look like, and React takes care of properly rendering it. &lt;/p&gt;

&lt;h3&gt;
  
  
  But this looks like HTML!
&lt;/h3&gt;

&lt;p&gt;You can use all of the elements you would find in an HTML document in JSX, including &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; - &lt;code&gt;&amp;lt;h6&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; and many more. You can also add JSX attributes such as &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;className&lt;/code&gt; (watch out, it's &lt;code&gt;className&lt;/code&gt; rather than just &lt;code&gt;class&lt;/code&gt;!). It's important to remember that this is &lt;strong&gt;not&lt;/strong&gt; HTML and cannot be read as HTML by your browser.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Top-level Elements and React Fragments
&lt;/h3&gt;

&lt;p&gt;In the code example above, you may notice that a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; encloses a number of child elements. JSX can only return &lt;strong&gt;one&lt;/strong&gt; top-level element. The top-level element can be anything from a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, a &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;, an &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag, or even what is known as a react &lt;strong&gt;fragment&lt;/strong&gt;. With a fragment, you simply enclose what you'd like to return in empty opening &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; and closing tags &lt;code&gt;&amp;lt;/&amp;gt;&lt;/code&gt;. Fragments allow you to group a list of children without adding unnecessary nodes to the DOM. &lt;/p&gt;

&lt;p&gt;We could refactor our example above to use a fragment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&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;&amp;gt;&lt;/span&gt; 
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nav&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;h3&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"example"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is a header&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"shortP"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is some text&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;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&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 render the same elements to the DOM as the example above, but without an overarching &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; node.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript and JSX
&lt;/h3&gt;

&lt;p&gt;JavaScript expressions can be embedded within JSX expressions, and must be places between { curly braces }. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&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;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="si"&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;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All JavaScript code needs to be inside of curly braces. This is known as &lt;strong&gt;JSX Expression Syntax&lt;/strong&gt;. Here are some valid things you &lt;em&gt;can&lt;/em&gt; have inside of a JSX Expression:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A string&lt;/li&gt;
&lt;li&gt;A number&lt;/li&gt;
&lt;li&gt;An array&lt;/li&gt;
&lt;li&gt;An object property that will evaluate to some value&lt;/li&gt;
&lt;li&gt;A map method (which will return a new array)&lt;/li&gt;
&lt;li&gt;More JSX &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And there are things you &lt;em&gt;cannot&lt;/em&gt; have in a JSX Expression: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any loop&lt;/li&gt;
&lt;li&gt;A variable declaration&lt;/li&gt;
&lt;li&gt;A function declaration&lt;/li&gt;
&lt;li&gt;If/else conditionals&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  JSX Conditionals
&lt;/h3&gt;

&lt;p&gt;You cannot use if/else syntax in embedded JavaScript. You can express conditionals in other ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A ternary within curly braces&lt;/li&gt;
&lt;li&gt;An if statement outside of a JSX element&lt;/li&gt;
&lt;li&gt;Logical short circuit &amp;amp;&amp;amp; operator
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using a ternary operator&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggleMode&lt;/span&gt; &lt;span class="o"&gt;=&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;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
        &lt;span class="si"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;isDarkMode&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Set Light Mode&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Set Dark Mode&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="si"&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="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Using an if statement outside of the JSX element&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;option&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;isDarkMode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;option&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Set Light Mode&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;option&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Set Dark Mode&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;toggleMode&lt;/span&gt; &lt;span class="o"&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="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;option&lt;/span&gt; &lt;span class="si"&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="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Using the &amp;amp;&amp;amp; operator&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;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isTrue&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is also true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&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;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Components &amp;amp; JSX
&lt;/h3&gt;

&lt;p&gt;A function component &lt;strong&gt;must&lt;/strong&gt; return JSX. Every function component in your React app must return one JSX element. This is where the single top-level element (or fragment) come into play. &lt;/p&gt;




&lt;h2&gt;
  
  
  Why use JSX?
&lt;/h2&gt;

&lt;p&gt;JSX allows developers to write cleaner, easier to read code. You don't strictly &lt;em&gt;need&lt;/em&gt; to use JSX, but it makes creating React applications easier by allowing programmers to use HTML-like syntax to describe what the UI should look like. &lt;/p&gt;

&lt;p&gt;JSX represents objects. The &lt;a href="https://reactjs.org/docs/introducing-jsx.html"&gt;React docs&lt;/a&gt; provide two examples, both of which will render identically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&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;h1&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"greeting"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    Hello, world!
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is going to have exactly the same output as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h1&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;className&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;greeting&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&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;In my opinion, JSX is easier to write and easier to read. JSX uses a declarative style of programming, which abstracts what Vanilla JavaScript takes a number of steps to do into a simple JSX expression of &lt;em&gt;what&lt;/em&gt; we want to render. React then figures things out behind the scenes (with a little help from Babel, as I mentioned above).  &lt;/p&gt;

&lt;p&gt;For more information, try these links:&lt;br&gt;
&lt;a href="https://reactjs.org/docs/introducing-jsx.html"&gt;React Docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/facebook/react/issues/13525#issuecomment-417818906"&gt;Why use className instead of class?&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Beginner's Guide to String Methods in JavaScript</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Thu, 25 Aug 2022 14:21:31 +0000</pubDate>
      <link>https://dev.to/meganmoulos/beginners-guide-to-string-methods-in-javascript-4am4</link>
      <guid>https://dev.to/meganmoulos/beginners-guide-to-string-methods-in-javascript-4am4</guid>
      <description>&lt;p&gt;First, a quick overview: In JavaScript, a &lt;code&gt;string&lt;/code&gt; object is used to represent a sequence of one or more characters. A string can contain any number of letters, numbers, or symbols. &lt;/p&gt;

&lt;p&gt;Strings can be created by using single quotes, double quotes, or template literals (backticks). &lt;/p&gt;

&lt;p&gt;&lt;code&gt;'This is a string.'&lt;br&gt;
"This is also a string."&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Strings are an immutable (unchanging), primitive data type. It is important to remember that string indexes are zero-based: this means that the first character is in position 0, the second in 1, and so on. Because JavaScript treats strings as objects, they also have a number of built-in methods, some of which we will be covering in this blog post. You can try any of these examples in your browser's console. &lt;/p&gt;


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

&lt;p&gt;Returns the character at a specific index. As mentioned above, all strings have zero-based indices. In the string &lt;code&gt;hello&lt;/code&gt; for example, h is at index 0, e is at index 1, l at index 2, and so on. We can access each character in two ways. One is using the charAt() method:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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;charAt&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="c1"&gt;// gives value "h"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because strings can be treated like arrays, you can access the same value by using the index number in square brackets:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// also gives value "h"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;Combines the text of multiple strings and returns a new string. This method does not change the original strings.&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;str1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, &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;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;World.&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns 'Hello, World."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a second way to easily concatenate strings in JavaScript: use the &lt;code&gt;+&lt;/code&gt; concatenation operator.&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;race&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;car&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// returns 'racecar'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Interestingly, if you concatenate non-string variables, JavaScript will type-convert them into strings. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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="s1"&gt;huge&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 'huge100false'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;This method will return &lt;code&gt;true&lt;/code&gt; if the string it's called on contains a specified string. It is case sensitive.&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;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am the best at coding.&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;doesItInclude&lt;/span&gt; &lt;span class="o"&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;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;best&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns 'true'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;This method will return the position of the first occurrence of a specified value in a string. It will return -1 if the value isn't found at all. Like includes(), it is case sensitive.&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;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I love to code!&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;result&lt;/span&gt; &lt;span class="o"&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;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;love&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns '2'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add an optional second argument: the position to start from (the default is 0). &lt;/p&gt;




&lt;h4&gt;
  
  
  length
&lt;/h4&gt;

&lt;p&gt;Quite simply, this will return the length of the string it's called on.&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Amazing!&lt;/span&gt;&lt;span class="dl"&gt;'&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="c1"&gt;// returns 8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;This method returns a new string with copies of a string, the number of which is specified in the parenthesis. The original string is not modified.&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I love JavaScript!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;repeat&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="c1"&gt;// returns 'I love JavaScript!I love JavaScript!I love JavaScript!I love JavaScript!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;Slice extracts a part of the string, specified by one or two parameters: The first is the start position, the second is where to end (up to, but not including) the slice. If no second argument is provided, the default is the entire string length. This method returns a new string. A negative number will select from the end of the string.&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;sentence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, World!&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&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;12&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns 'World'&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&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="c1"&gt;// returns 'World!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;This method takes a string and splits it into an array containing substrings. It will return the new array without changing the original string. This is particularly useful if you'd like to split a sentence into a number of separate words.&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;sentence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;An array would be nice!&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;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// returns ['An', 'array', 'would', 'be', 'nice!']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  toLowerCase() and toUpperCase()
&lt;/h4&gt;

&lt;p&gt;This simple method returns a new string with the letters of the original string converted to lowercase letters. &lt;code&gt;toUpperCase()&lt;/code&gt; takes a string and converts all the letters to uppercase.&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;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am the greatest!&lt;/span&gt;&lt;span class="dl"&gt;'&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;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// returns 'I AM THE GREATEST!'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;This method is a bit different from the others because it turns other data types in JavaScript &lt;em&gt;into&lt;/em&gt; a string. You can convert numbers, booleans, or even objects to strings.&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="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// returns '15'&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// returns 'false'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;This has been an overview of common string methods used in JavaScript. A more comprehensive list can be found on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String"&gt;MDN Web Docs.&lt;/a&gt;&lt;/p&gt;

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

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Markdown for Beginners - Create a Better Readme</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Wed, 24 Aug 2022 20:37:53 +0000</pubDate>
      <link>https://dev.to/meganmoulos/markdown-for-beginners-create-a-better-readme-1g8c</link>
      <guid>https://dev.to/meganmoulos/markdown-for-beginners-create-a-better-readme-1g8c</guid>
      <description>&lt;h2&gt;
  
  
  What is Markdown?
&lt;/h2&gt;

&lt;p&gt;Markdown is one of the most popular markup languages. Developed by &lt;a href="https://daringfireball.net/projects/markdown/"&gt;John Gruber&lt;/a&gt; in 2004, it's a simple and lightweight way to add formatting (and pizazz) to plaintext documents. In Gruber's words: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While many of us are used to Rich Text Editors like Microsoft Word, where changes are visible immediately, Markdown uses unique plaintext syntax to change how text appears. This guide will go over commonly used Markdown syntax, so you can create a better GitHub README!&lt;/p&gt;

&lt;p&gt;You can add Markdown using any text editor application. There are also Markdown applications, some of which allow you to preview your document in real time. For our purposes, you can follow along in &lt;a href="https://dillinger.io/"&gt;Dillinger&lt;/a&gt;, so that you can see the changes immediately. &lt;/p&gt;




&lt;h3&gt;
  
  
  Headings
&lt;/h3&gt;

&lt;p&gt;There are 6 headers available, H1 through H6. &lt;/p&gt;

&lt;p&gt;Here is the Markdown syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Heading 1
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Heading 2
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Heading 3
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Heading 4
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Heading 5
&lt;/h5&gt;

&lt;h6&gt;
  
  
  Heading 6
&lt;/h6&gt;




&lt;h3&gt;
  
  
  Adding &lt;em&gt;emphasis&lt;/em&gt; to your text
&lt;/h3&gt;

&lt;p&gt;Here is the Markdown syntax for making text &lt;strong&gt;bold&lt;/strong&gt;, &lt;em&gt;italic&lt;/em&gt;, and &lt;u&gt;underlined&lt;/u&gt;. You can also &lt;del&gt;strike through&lt;/del&gt;, or 'cross out,' text using the strikethrough method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**bold**
_italic_
&amp;lt;u&amp;gt;underlined&amp;lt;/u&amp;gt;
~~strike through~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  BONUS: Subscripts and Superscripts
&lt;/h4&gt;

&lt;p&gt;Sometimes we may want to use superscripts for things like chemical notation H20 or exponents x&lt;sup&gt;2&lt;/sup&gt;. With Markdown, it's simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;H&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;0
x&amp;lt;sup&amp;gt;2&amp;lt;/sup&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For superscripts, you surround the element you want to make a subscript with the 'sub' tag. For subscripts, you surround the element with the 'sup' tag.&lt;/p&gt;




&lt;h3&gt;
  
  
  Highlighting Syntax &amp;amp; Code Blocks
&lt;/h3&gt;

&lt;p&gt;Using a single backtick will &lt;code&gt;highlight&lt;/code&gt; a word or piece of code. You can see it in the following example below: &lt;/p&gt;

&lt;p&gt;In this sentence, the word &lt;code&gt;hello&lt;/code&gt; will be highlighted.&lt;/p&gt;

&lt;p&gt;Using three backticks will create a code block. If you add a language identifier directly after the backticks you will have more colorful, readable code. Here is an example using JavaScript:&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;makeAVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// This will show different colors &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;makeAVariable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Quotes
&lt;/h3&gt;

&lt;p&gt;To show a quote, use a greater than symbol. Quotes are extremely useful for when you are referencing someone else's work. It is important to credit where credit is due. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Make sure to save your code!" - Megan&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Lists
&lt;/h3&gt;

&lt;p&gt;To create an ordered list, begin from 1 and count upwards. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Item one&lt;/li&gt;
&lt;li&gt;Item two&lt;/li&gt;
&lt;li&gt;Item three&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can also create sub-items on your list and even include bullet points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Item one

&lt;ol&gt;
&lt;li&gt;Sub-item one &lt;/li&gt;
&lt;li&gt;Sub-item two&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;Item two

&lt;ul&gt;
&lt;li&gt;Sub-item three&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Item three&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Unordered lists are created using a minus sign before each item. See below: &lt;/p&gt;

&lt;p&gt;Shopping List:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cereal&lt;/li&gt;
&lt;li&gt;eggs&lt;/li&gt;
&lt;li&gt;cheese&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Images &amp;amp; Emojis
&lt;/h3&gt;

&lt;p&gt;To add an image, use an exclamation point followed by square brackets containing your alt text, followed by your html link in parentheses like so: &lt;code&gt;![alt text](url)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2VkQBNem--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.pexels.com/photos/326012/pexels-photo-326012.jpeg%3Fcs%3Dsrgb%26dl%3Dpexels-pixabay-326012.jpg%26fm%3Djpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2VkQBNem--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.pexels.com/photos/326012/pexels-photo-326012.jpeg%3Fcs%3Dsrgb%26dl%3Dpexels-pixabay-326012.jpg%26fm%3Djpg" alt="Bunny" width="880" height="1320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also use emojis in Markdown. There is a complete list of emojis recognized &lt;a href="https://gist.github.com/rxaviers/7360908"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;😍👍🐰&lt;/p&gt;




&lt;h3&gt;
  
  
  Links
&lt;/h3&gt;

&lt;p&gt;To create a link, simply put the link text in square brackets and then parentheses enclosing the URL you'd like to point to. The syntax looks like this: &lt;code&gt;[](url)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Check out some great links below!&lt;br&gt;
&lt;a href="https://github.com/meganmoulos"&gt;My GitHub&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/megan-moulos-69706334/"&gt;My LinkedIn&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;This has been a quick guide to making your README files more beautiful using Markdown. Show me your new README in the comments below! &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>productivity</category>
      <category>markdown</category>
    </item>
    <item>
      <title>Beginner's Guide to Array Methods in JavaScript</title>
      <dc:creator>Megan Moulos</dc:creator>
      <pubDate>Mon, 22 Aug 2022 17:11:03 +0000</pubDate>
      <link>https://dev.to/meganmoulos/beginners-guide-to-array-methods-in-javascript-25ph</link>
      <guid>https://dev.to/meganmoulos/beginners-guide-to-array-methods-in-javascript-25ph</guid>
      <description>&lt;p&gt;JavaScript has a number of helpful array methods integrated into the language. This guide will cover a few of the methods I use the most. It's helpful to note that arrays are JavaScript objects, and can contain a mix of different data types. You can recognize an array in JavaScript by the use of &lt;code&gt;square brackets [ ]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is an example of an array with multiple elements: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const myNewArray = [30, 40, true, 'Megan'];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Each element in an array has an &lt;code&gt;index&lt;/code&gt; beginning at [0]. In our example above, the element 30 is at &lt;code&gt;myNewArray[0]&lt;/code&gt;, 40 is at &lt;code&gt;myNewArray[1]&lt;/code&gt;, and so on. This is very important information when you are working with JavaScript methods. This will become more apparent as we work through the examples. Now, on to the methods! &lt;/p&gt;

&lt;h2&gt;
  
  
  Array.length()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;length&lt;/code&gt; property returns the number of elements in an array. Let's call it on our array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myNewArray&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Megan&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;arrayLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myNewArray&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;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;arrayLength&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the syntax to access the &lt;code&gt;length&lt;/code&gt; property is yourArray.length. This property becomes very useful in things like for loops. &lt;/p&gt;

&lt;h2&gt;
  
  
  Array.push()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;push&lt;/code&gt; method adds any number of items to the end of an array. It will return the new length of the array. This is considered a &lt;strong&gt;destructive&lt;/strong&gt; method because it changes the original array. Let's see how it works with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;horse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// add 'pig' to the array&lt;/span&gt;
&lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pig&lt;/span&gt;&lt;span class="dl"&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;animals&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['cat', 'dog', 'horse', 'pig']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have our 'pig' added to the end of the original animals array! &lt;/p&gt;

&lt;h2&gt;
  
  
  Array.pop()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;pop&lt;/code&gt; method removes a single item from the end of an array. Let's say we want to remove the pig from our animals array above. We simply use the following syntax:&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;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&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;animals&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['cat', 'dog', 'horse']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our pig is gone. This method, like push, is &lt;strong&gt;destructive&lt;/strong&gt; because it removes the element from our original array. It also returns the removed element. &lt;/p&gt;

&lt;h2&gt;
  
  
  Array.shift()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;shift&lt;/code&gt; method removes the first element of an array. Let's see how it works on our original animals array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;animals&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;horse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Remove the first element from the array&lt;/span&gt;
&lt;span class="nx"&gt;animals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&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;animals&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['dog', 'horse']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The shift method is also &lt;strong&gt;destructive&lt;/strong&gt; because it removes the element from the original array. &lt;/p&gt;

&lt;h2&gt;
  
  
  Array.unshift()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;unshift&lt;/code&gt; method will add any number of items to the beginning of an array. Much like push, this method is &lt;strong&gt;destructive&lt;/strong&gt; and makes changes to the original array. Let's try a new example using the unshift syntax:&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;colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Add two new colors to the beginning of the array&lt;/span&gt;
&lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;purple&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;yellow&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['purple', 'yellow', 'blue', 'red', 'green']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've added our two new colors to the original array! As you can see they've been added in the original order that we passed them in. &lt;/p&gt;

&lt;h2&gt;
  
  
  Array.slice()
&lt;/h2&gt;

&lt;p&gt;You may be thinking to yourself that all of these destructive methods seem a little, well, destructive! There are array methods that are &lt;strong&gt;nondestructive&lt;/strong&gt; and return a new array that is a copy or a section of the original. &lt;code&gt;Slice&lt;/code&gt; is one such method. When passed no arguments, &lt;code&gt;slice&lt;/code&gt; will return a new shallow copy of the original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Make a copy &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colorsCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&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;colorsCopy&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['blue', 'red', 'green']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be a very useful tool for when we don't want to change our original array. &lt;code&gt;Slice&lt;/code&gt; can be used to copy a portion of an array when passed additional arguments. &lt;code&gt;Slice&lt;/code&gt; takes in a start and an end so that JavaScript knows what portion of the array you'd like to copy. The syntax looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;arr.slice(start, end);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;'Start' being the index of the element you want to start the 'copy' from, and 'end' being the index of the last element you want to include. If you do not provide an end, the selection will end at the index of the last array element. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iceCream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chocolate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vanilla&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rocky road&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Slice only the elements we want to copy to our new array&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;iceCream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&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="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;newArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['vanilla', 'rocky road']&lt;/span&gt;

&lt;span class="c1"&gt;// Remember, our original array will be untouched&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;iceCream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['chocolate', 'vanilla', 'rocky road', 'mint']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Array.splice()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;splice&lt;/code&gt; method is perhaps the most confusing on this list, but is super useful for adding, updating, or removing elements in an array. It is &lt;strong&gt;destructive&lt;/strong&gt; and does modify the original array. It also returns a new array of the deleted elements (if you did so). Let's take a look at the syntax first:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;splice(start, deleteCount, item1, item2...)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt; is the index at which to start changing the array. If your start number is greater than the length of your array, no elements will be deleted. Instead it will add the elements you provide as arguments. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;deleteCount&lt;/code&gt; is an optional parameter, and is an integer indicating the number of elements that should be removed. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;item1&lt;/code&gt;, &lt;code&gt;item2&lt;/code&gt;, etc. are the elements you want added to the array, beginning from &lt;code&gt;start&lt;/code&gt;. If you don't add these elements, &lt;code&gt;splice&lt;/code&gt; will only remove things from the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although it seems like a lot of information, let's see it in action with our iceCream array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;iceCream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chocolate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vanilla&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rocky road&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Starting at array index 1 we are deleting one element and then adding a new element&lt;/span&gt;
&lt;span class="nx"&gt;iceCream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;coffee&lt;/span&gt;&lt;span class="dl"&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;iceCream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['chocolate', 'coffee', 'rocky road', 'mint']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What would happen if we don't delete any elements? We would have the following output!&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;iceCream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;splice&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;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;coffee&lt;/span&gt;&lt;span class="dl"&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;iceCream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output: ['chocolate', 'coffee', 'vanilla', 'rocky road', 'mint']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see nothing has been deleted, but 'coffee' has been inserted where our &lt;code&gt;start&lt;/code&gt; indicated. &lt;/p&gt;

&lt;p&gt;As you can see, array methods are crucial to any programmer's skillset. JavaScript has many more built-in methods, a list of which can be found on MDN's Web Docs: &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"&gt;MDN's Array Information&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sMbs-ts4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1os3j74i5jugqnpexzn9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sMbs-ts4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1os3j74i5jugqnpexzn9.png" alt="Image description" width="880" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let me know in the comments below if there are more array methods you'd like explained! &lt;/p&gt;

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