<?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: Avery Berkowitz</title>
    <description>The latest articles on DEV Community by Avery Berkowitz (@aveb).</description>
    <link>https://dev.to/aveb</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%2F239023%2F833e3379-402c-435f-871f-8be1f79aedc3.jpeg</url>
      <title>DEV Community: Avery Berkowitz</title>
      <link>https://dev.to/aveb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aveb"/>
    <language>en</language>
    <item>
      <title>Diving into SWIFT</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Sat, 14 Dec 2019 21:08:57 +0000</pubDate>
      <link>https://dev.to/aveb/diving-into-swift-174j</link>
      <guid>https://dev.to/aveb/diving-into-swift-174j</guid>
      <description>&lt;p&gt;After spending the last month working on a native application in React Native, I figured it would be interesting to dive deeper into mobile development and learn a little bit more about Apple's very own programming language: &lt;strong&gt;Swift&lt;/strong&gt;.  In this post, I will explore why &lt;strong&gt;Swift&lt;/strong&gt; has swiftly (sorry, had to do that once) become the most popular language for iOS development and dive into some of the basics including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;li&gt;Collections&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What is Swift?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Swift&lt;/strong&gt; is a programming language for the suite of apple devices first released in 2014.  It is meant to be a higher-level, more modern take on Objective-C and has certainly lived up to expectations by becoming the most popular language for iOS development by 2018.  It is open source and has a comprehensive, 500+ page manual available for free download &lt;a href="https://books.apple.com/gb/book/the-swift-programming-language-swift-5-1/id881256329"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why learn Swift?
&lt;/h1&gt;

&lt;p&gt;Well, if you like Apple products (I shamelessly do), Swift is the way to go.  As previously mentioned, it is currently the most popular programming language across apple devices and its popularity is continuing to rise.  Check out &lt;strong&gt;Swift's&lt;/strong&gt; position on the 2019 developers survey from stack overflow:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GejUcBqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mfzlxyr2nk8g03cpszdt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GejUcBqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mfzlxyr2nk8g03cpszdt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
If you are hunting for jobs, &lt;strong&gt;Swift&lt;/strong&gt; isn't so bad either.  A quick search on &lt;a href="https://www.ziprecruiter.com/Jobs/Swift-Programming"&gt;ZipRecruiter&lt;/a&gt; currently shows &lt;strong&gt;3336&lt;/strong&gt; open positions posted across the country (as of today, Dec. 14, 2019) posted within the last 30 days.&lt;/p&gt;

&lt;p&gt;Lastly, &lt;strong&gt;Swift&lt;/strong&gt; is a language that isn't too hard to pick up if you are already familiar with another language or two (I am coming from a JavaScript and Python background).  Let's see some &lt;strong&gt;Swift&lt;/strong&gt; basics:&lt;/p&gt;
&lt;h2&gt;
  
  
  Variables in Swift
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Running example code: for simplicity, I recommend trying out &lt;a href="https://repl.it/languages/swift"&gt;repl.it&lt;/a&gt;'s &lt;strong&gt;Swift compiler&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Variables can be declared with either the &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt; keyword.  If the value of the variable is going to be reassigned, &lt;code&gt;var&lt;/code&gt; should be used. &lt;br&gt;
 Constants (variables that will only be assigned once) should use the &lt;code&gt;let&lt;/code&gt; keyword (like &lt;code&gt;const&lt;/code&gt; in Javascript). Variables &lt;em&gt;must&lt;/em&gt; be declared before use!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;changeMe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;dontChangeMe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we want to log values to the console, we can use the built in &lt;code&gt;print&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;changeMe&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// ==&amp;gt; 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;// are used for inline comments and we do not need to use ; to end expressions! Nice.&lt;/p&gt;

&lt;p&gt;Most of the time, we may want to specify type when defining variables.  This can be really helpful to catch bugs as your code base grows.  To specify type, we add a &lt;code&gt;:&lt;/code&gt; followed by the datatype.  An error will be thrown if these variables are not assigned to the specified datatype later during code execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// explicit variable declaration and assignment&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Avery"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Reassigning &lt;code&gt;firstName&lt;/code&gt; to another data type later will throw an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Collections
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Swift&lt;/strong&gt; keeps it simple, with three major types of collections: &lt;code&gt;Arrays&lt;/code&gt;, &lt;code&gt;Sets&lt;/code&gt;, and &lt;code&gt;Dictionaries&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrays&lt;/strong&gt; are indexed collections of data, comma separated within &lt;code&gt;[]&lt;/code&gt;.  They can be made immutable (into a tuple) simply be declaring them with the &lt;code&gt;let&lt;/code&gt; keyword.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;mutableArray&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Can't"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Change"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Me"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice we can explicitly set the type of both the Array and its contents after the colon.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sets&lt;/strong&gt; are unindexed collections.  They look really similar to arrays, except if iterated over, the order will be random.  These are good for storing data where order doesn't matter.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;myCats&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Frank"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hodor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Buddy"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dictionaries&lt;/strong&gt; are collections of key-value pairs.  They also look pretty familiar to sets and arrays.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;me&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Avery"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"32"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"City"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"New Orleans"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Unlike with javascript objects, we can use the data type of our choice for our keys!&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;We define functions in Swift with the &lt;code&gt;func&lt;/code&gt; keyword.  We must also make some decisions about what datatypes our arguments will be, as well as if our function should return a value.&lt;/p&gt;

&lt;h4&gt;
  
  
  Functions with no return value
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello, &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Avery"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// ==&amp;gt; "Hello, Avery"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So a few things here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We name our function &lt;code&gt;sayHello&lt;/code&gt; and specify that it is expecting a variable with a type of &lt;code&gt;String&lt;/code&gt; when it is invoked.  Our print statement shows of &lt;strong&gt;Swift string interpolation&lt;/strong&gt; as &lt;code&gt;\(name)&lt;/code&gt; will be replaced by the function's argument when it is invoked hence the log of &lt;code&gt;Hello, Avery&lt;/code&gt;, if you run this code in your compiler.
####Functions with return value
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"hello, &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Avery"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice we add a &lt;code&gt;-&amp;gt;&lt;/code&gt; (arrow) and specify that our return value will be of data type &lt;code&gt;String&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I hope you have enjoyed this brief intro into &lt;strong&gt;Swift&lt;/strong&gt;.  Clearly, I have just scratched the surface for what &lt;strong&gt;Swift&lt;/strong&gt; is and can do.  For more, I encourage you to check out the official docs &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Intro to web scraping (w/ Node.js example)</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Mon, 09 Dec 2019 06:33:39 +0000</pubDate>
      <link>https://dev.to/aveb/intro-to-web-scraping-w-node-js-example-5a9p</link>
      <guid>https://dev.to/aveb/intro-to-web-scraping-w-node-js-example-5a9p</guid>
      <description>&lt;p&gt;When it comes to scraping the web, &lt;em&gt;Python&lt;/em&gt; definitely is king.  Frameworks like &lt;a href="https://scrapy.org/"&gt;scrapy&lt;/a&gt; and &lt;a href="https://www.crummy.com/software/BeautifulSoup/"&gt;beautiful soup&lt;/a&gt; make parsing through raw HTML (relatively) simple and can be used to build a basic scraping tool in minutes.  Fortunately for javascript developers, there are also some pretty cool tools out there to accomplish much of the same when it comes to scraping the web.  This post will provide a brief intro to scraping using node.js and &lt;a href="https://cheerio.js.org/"&gt;cheerio&lt;/a&gt;.  We will also build our very own &lt;strong&gt;web scraper&lt;/strong&gt; to extract image urls from the website of our choice!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is web scraping?
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="https://www.internetlivestats.com/total-number-of-websites/"&gt;live internet stats&lt;/a&gt;, there are more than &lt;strong&gt;1.7 billion websites&lt;/strong&gt; that can be found on the internet today.  It is estimated that google knows about more than &lt;strong&gt;130 trillion&lt;/strong&gt; pages (2016 estimate, most recent I could find...).  Basically, there is A LOT of data out there.  &lt;strong&gt;Web scrapers&lt;/strong&gt; are tools that help us sift through the madness.  In their simplest form, they request the html from a webpage, and quickly sort through it to find a target as specified by the programmer.  This contact information, phone numbers, embedded links -- really anything you could think of that exists in that raw html request.  So you might be thinking, aren't APIs built for sharing data? Yes, but many websites don't have APIs and even those that do may not want you to have easy access organized information that their pages may contain.  It is up to web scrapers to do the dirty work for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is web scraping legal?
&lt;/h2&gt;

&lt;p&gt;Before we get into actually building a web scraper, it is important to note that some websites &lt;strong&gt;are not ok&lt;/strong&gt; with you scraping them.  Companies like craigslist have even been awarded millions of dollars as the result of legal action taken against other companies that scraped their sites.  So it is always a good idea to check out the &lt;code&gt;robots.txt&lt;/code&gt; file for a website before you try and scrape them.  This can be found by appending &lt;code&gt;robots.txt&lt;/code&gt; to the end most sites' domain name.  Below is what this looks like for craigslist:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Iku-jl8X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7tvva8eokh1q1krpaiql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Iku-jl8X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7tvva8eokh1q1krpaiql.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
What you need to know here is that it is not ok to make a program (bot) that makes requests to these endpoints.  You should also checkout the websites terms of use, usually found in the footer or about page.  So do your homework before you get started.  For the example below, we will be making requests to &lt;a href="http://books.toscrape.com/"&gt;http://books.toscrape.com/&lt;/a&gt; which is a site set up specifically to practice web scraping.&lt;/p&gt;
&lt;h2&gt;
  
  
  Building a simple web scraper
&lt;/h2&gt;

&lt;p&gt;Prerequisites: must have &lt;a href=""&gt;node&lt;/a&gt; installed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make a new directory with the name of your choice and run: 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm init&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;install dependencies.  We will be using &lt;a href="https://www.axios.com/"&gt;axios&lt;/a&gt; to make http requests and &lt;a href="https://cheerio.js.org/"&gt;cheerio&lt;/a&gt; to help us parse the html that we get back.

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm install --save cheerio axios&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;create a file for our scraper code:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;touch index.js&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Since our scraper is going to be making an http request, we need to be able to wait for our a response.  Axios returns a promise out of the box, thus we can use a &lt;code&gt;.then()&lt;/code&gt; in which we will have access to the html we want to set.  Below is the basic setup for our axios request
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="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;axios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;axios&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;cheerio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cheerio&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://books.toscrape.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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&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="c1"&gt;// our scraping code will go here!&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;something went wrong!&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;ol&gt;
&lt;li&gt;The html string that we want will be stored on the &lt;code&gt;data&lt;/code&gt; property of the response from &lt;em&gt;axios&lt;/em&gt;.  We now want to load this html into the &lt;em&gt;cheerio&lt;/em&gt; package that we downloaded earlier.  Add the following to our &lt;code&gt;.then()&lt;/code&gt; block:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="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;$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cheerio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt; &lt;em&gt;Cheerio&lt;/em&gt; processes the html string and will allow us to select html tags, classes, id's, attributes, and tag contents almost exactly like we would be able to in jquery.  Let's log the uri from the first &lt;code&gt;img&lt;/code&gt; tag's &lt;code&gt;src&lt;/code&gt; in the html for books.toscrape page.  Add the following:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="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;firstUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;body&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;img&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src&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;firstUri&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Notice that we first select the &lt;code&gt;body&lt;/code&gt; tag.  The &lt;code&gt;.find()&lt;/code&gt; method selects the very first &lt;code&gt;img&lt;/code&gt; tag found within the &lt;code&gt;body&lt;/code&gt; tag.  Finally, the &lt;code&gt;.attr()&lt;/code&gt; allows us to select the contents of the &lt;code&gt;src&lt;/code&gt; attribute within that first &lt;code&gt;img&lt;/code&gt; tag.  Even for some as simple as a photo url, it definitely takes a bit of investigation, right ?!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's see are code in action!  In our terminal, run:&lt;/li&gt;
&lt;li&gt;node index.js
Your code may take some time to run.  This is because we have to wait for our axios request to be completed and it takes cheerio a little while to parse all that html.  If you are connected to the internet, you should see a &lt;code&gt;uri&lt;/code&gt; for an image printed out in your console.  Here's what I got:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--COxs18lk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/yugit7ztbtkxn27oqe2p.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While this example is admittedly basic, imagine being able to create a bot that grabs all of the image URIs from a website with dynamic website every day, without you having to lift a finger!  We can even have our web scraper find the &lt;code&gt;next page&lt;/code&gt; button, giving it the ability to &lt;em&gt;crawl&lt;/em&gt; across web pages, even jumping to new ones along the way!&lt;/p&gt;

&lt;p&gt;In a perfect world, every website would create a beautiful, well-documented api with open access granted to anyone who wishes.  In the meantime, web scrapers do the trick.  Have fun trying them out on your own!&lt;/p&gt;

&lt;p&gt;Below is the complete code for the super basic image uri scraper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;axios&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;cheerio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cheerio&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://books.toscrape.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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cheerio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;firstUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;body&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;img&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src&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;firstUrl&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;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;something went wrong!&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;



</description>
      <category>webscaping</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>From Javascript to Python</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Sun, 01 Dec 2019 21:16:18 +0000</pubDate>
      <link>https://dev.to/aveb/from-javascript-to-python-1b1m</link>
      <guid>https://dev.to/aveb/from-javascript-to-python-1b1m</guid>
      <description>&lt;p&gt;So you know javascript but you get this amazing offer to work at this great company in the perfect city and JS is nowhere to be found in their tech stack.  Instead, they use the Python-based Django web development framework.  So where do you start?  In this post, I will (hopefully) lay that foundation.  We will cover:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; What is python?&lt;/li&gt;
&lt;li&gt; Why learn python?&lt;/li&gt;
&lt;li&gt; Basic(simple) datatypes&lt;/li&gt;
&lt;li&gt; Lists&lt;/li&gt;
&lt;li&gt; Variable declaration&lt;/li&gt;
&lt;li&gt; Writing functions&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is expected that the reader is familiar with javascript so if that isn't the case, I recommend checking out this &lt;a href="https://www.udemy.com/course/the-modern-python3-bootcamp"&gt;intro to python&lt;/a&gt; on Udemy where no prior programming knowledge of any kind is needed.&lt;/p&gt;

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

&lt;p&gt;From their website: &lt;em&gt;Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.&lt;/em&gt;  &lt;strong&gt;What??&lt;/strong&gt;  Yeah, that's what I thought the first time I read it, so let's break that sentence down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;interpreted&lt;/em&gt; - just like javascript, &lt;em&gt;Python&lt;/em&gt; is translated directly into code that a computer can read.  In javascript, this is usually handled by an interpreter like Chrome's V8 engine or Node.js on our computer or server.  Python has it's own interpreter that can be downloaded &lt;a href="https://www.python.org/downloads/"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;object-oriented&lt;/em&gt; refers to the way a Python program is organized/structured.  We use classes and objects in python to separate concerns, DRY up our code, and allow our programs to represent things in the &lt;strong&gt;Real World&lt;/strong&gt;.  In contrast, &lt;em&gt;Javascript&lt;/em&gt; is a functional programming language and uses functions to accomplish many of the same organizational strategies as Python.  Conceptually, there is a ton of overlap here that we could discuss for hours.  Coming from javascript, just know that a &lt;strong&gt;Class&lt;/strong&gt; in Python is not a special type of function like it is in Javascript.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;high-level programming language&lt;/em&gt; - this refers to languages that are more comparable to human languages.  Basically, it is easy to read, even for non-programmers.  Javascript also is a high-level language though I would argue that it is not quite as easy to read compared to python.  We will revisit this comparison with code examples in a bit.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;dynamic semantics&lt;/em&gt; - basically, we can assign and reassign variables to point to different values and datatypes.  We can do this in javascript as well.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why Python?
&lt;/h2&gt;

&lt;p&gt;According to Stack Overflow, it is the fastest growing programming language.  Python ranked 4th in most popular technology in the &lt;a href="https://insights.stackoverflow.com/survey/2019"&gt;2019 Developers Survey&lt;/a&gt; on stack overflow.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xrh1zc8C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ertbeezlube09wgfq9dl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xrh1zc8C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ertbeezlube09wgfq9dl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic (Simple) Datatypes
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Time to see what Python is all about.  To try out any of the following code examples, I recommend using &lt;a href="https://repl.it/languages/python3"&gt;repl.it&lt;/a&gt; or you can download the &lt;a href="https://www.python.org/downloads/"&gt;python interpreter&lt;/a&gt;&lt;/em&gt;.  We will be using python's built-in &lt;code&gt;type()&lt;/code&gt; function for type-checking.  This is the equivalent of javascripts &lt;code&gt;typeof&lt;/code&gt;.  To log values upon execution, we use python's console.log equivalent &lt;code&gt;print()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; In javascript, we just have numbers.  Python breaks numbers into three categories: Ints, Floats, Complex Numbers.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# int
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; &amp;lt;class 'int'&amp;gt;
&lt;/span&gt;
&lt;span class="c1"&gt;# float
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10.11&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; &amp;lt;class 'float'&amp;gt;
&lt;/span&gt;
&lt;span class="c1"&gt;# complex
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;5j&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; &amp;lt;class 'complex'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt; Strings and booleans are really similar to javascript.  Notice that &lt;strong&gt;booleans are capitalized in python&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I am a string"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; &amp;lt;class 'str'&amp;gt;
&lt;/span&gt;
&lt;span class="c1"&gt;# float
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; &amp;lt;class 'bool'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;None&lt;/strong&gt; datatype.  There is no null or undefined in python.  Instead, we have &lt;code&gt;None&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; &amp;lt;class 'NoneType'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;em&gt;also notice that comments are written with # in python compared to // in javascript&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Lists and other complex datatypes
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;A list in python is pretty much exactly like an array in javascript.&lt;/em&gt; Values (elements) are contained within &lt;strong&gt;[]&lt;/strong&gt; and comma-separated.  We can also use bracket notation for access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that elements in a list may NOT be separated with spaces and like in javascript, a list is zero-indexed.&lt;br&gt;
Other complex datatypes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tuples: immutable lists &lt;code&gt;my_tuple = (1,2,3,4)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;sets: unordered (not indexed) lists &lt;code&gt;my_set = {1,2,3,4}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;dictionaries: equivalent to objects in javascript (key-value pairs)
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;my_dictionary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Avery"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;"city"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"New Orleans"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_dictionary&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; Avery
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Variable assignment
&lt;/h2&gt;

&lt;p&gt;When writing variables in javascript, we have quite a few choices to make.  Should we use &lt;code&gt;const&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;, or define a global variable without a keyword (never do this...)?  Well in python, we don't use any keyword, ever.  Convention for naming variables is to use snake case.  And just like javascript, variables are case sensitive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;my_dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Jasmine"&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; Jasmine
# &amp;gt;&amp;gt;&amp;gt; 10
# &amp;gt;&amp;gt;&amp;gt; 100
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing functions
&lt;/h2&gt;

&lt;p&gt;Functions in python are written with the &lt;code&gt;def&lt;/code&gt; (short for define) keyword.  Unlike javascript, we use indentation to define the function body (no mustache brackets here!).  The norm is an indentation of &lt;strong&gt;4&lt;/strong&gt; whitespaces, but you may indent however you like as long as you are consistent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply_by_two&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;multiply_by_two&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="c1"&gt;# &amp;gt;&amp;gt;&amp;gt; 60
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  What next?
&lt;/h2&gt;

&lt;p&gt;Clearly, we have just scratched the surface of Python.  But as you can see, it shares many similarities with javascript.  Javascript developers familiar with ES6 Class syntax will be right at home when working with python classes.  Moving forward, I highly recommend Colt Steele's &lt;a href="https://www.udemy.com/course/the-modern-python3-bootcamp"&gt;The Modern Python 3 bootcamp&lt;/a&gt;.  The saying goes that learning your first language is always the hardest -- that certainly was the case from me as picking up python after javascript has been a smooth ride so far.  Best of luck on your python journey!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building a React Native app w/ expo</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Mon, 25 Nov 2019 03:07:36 +0000</pubDate>
      <link>https://dev.to/aveb/building-a-react-native-app-w-expo-77l</link>
      <guid>https://dev.to/aveb/building-a-react-native-app-w-expo-77l</guid>
      <description>&lt;p&gt;While the design principles between building mobile and web applications share many similarities, it was been difficult for many web developers to quickly make the switch. Building mobile (native) apps has long required learning a new language like &lt;em&gt;Swift, Objective-C, or Java&lt;/em&gt;.  The &lt;a href="https://expo.io/" rel="noopener noreferrer"&gt;Expo React Native&lt;/a&gt; platform aims to put an end to that.  Through their cli, you can quickly set up a mobile application for both iOS and android devices using &lt;em&gt;javascript and react&lt;/em&gt;.  This post will walk you through setting up a basic todo-list application.  It is important that you have some experience with &lt;em&gt;React&lt;/em&gt;, &lt;em&gt;React hooks&lt;/em&gt;, and &lt;em&gt;es6&lt;/em&gt; so I suggest checking out the following links if that isn't the case: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;a href="https://reactjs.org/tutorial/tutorial.html" rel="noopener noreferrer"&gt;React&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;React Hooks&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;a href="http://es6-features.org/" rel="noopener noreferrer"&gt;es6 syntax&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  To-do list demo
&lt;/h2&gt;

&lt;p&gt;Here's what we are building:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/9um-CPH4E_Q"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Along the way, we will learn how to use the &lt;em&gt;expo react native&lt;/em&gt; platform to set up our development environment and provide us with some starter code.  We will also cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build-in React Native components&lt;/li&gt;
&lt;li&gt;Building a custom component&lt;/li&gt;
&lt;li&gt;Basic Styling&lt;/li&gt;
&lt;li&gt;Event Handling&lt;/li&gt;
&lt;li&gt;Passing props to components&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;To build our todo-list application, we will be using &lt;em&gt;expo&lt;/em&gt;.  It is a well documented platform that performs much like create-react-app.  In addition, it allows us to test out our application using our own mobile device or emulators through xCode or Android Studio.  For this post, I will run the application on my own mobile device as I don't want to force anyone to waste an hour downloading emulators (though this is recommended if you want to develop a larger application).  Let's first set up &lt;em&gt;expo&lt;/em&gt; and download our starter code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make sure you have &lt;em&gt;node.js&lt;/em&gt; installed on your computer. You can download it &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Download the expo app from the apple or google play store.  We will use this in a moment to see our application in action!&lt;/li&gt;
&lt;li&gt;Download the expo cli using &lt;code&gt;npm install expo-cli --global&lt;/code&gt; in your terminal.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;expo init todo-demo&lt;/code&gt; (&lt;em&gt;todo-demo&lt;/em&gt; will be the name of our project's directory -- feel free to use any name you please).&lt;/li&gt;
&lt;li&gt;running this command will prompt you to make a few choices. 

&lt;ul&gt;
&lt;li&gt;Under &lt;em&gt;Managed Workflows&lt;/em&gt; select &lt;em&gt;blank&lt;/em&gt; for your template.&lt;/li&gt;
&lt;li&gt;Give your app a name (can be whatever you like).  Once again, I use &lt;code&gt;todo-demo&lt;/code&gt; for my app's name.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd todo-demo&lt;/code&gt; and open the contents of the directory in the text editor of your choice!&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;npm start&lt;/code&gt; to run the application.  You will see a qr-code in the terminal and also, a tab should open automatically in your browser with the same qr-code and some more information about the build.  Use your iphone or android camera to scan the code.  You should be prompted to open up the application in expo.  Once open, you may be greeted with a welcome screen if you are first opening expo, but you should see the following once the app is loaded:
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F7jxu3ufjhh15jlzjhn3f.png" alt="iphone"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Components in React Native
&lt;/h2&gt;

&lt;p&gt;Let's open up &lt;code&gt;App.js&lt;/code&gt; in our code editor and check out the contents.  &lt;em&gt;React Native&lt;/em&gt; is built on top of &lt;code&gt;React&lt;/code&gt; thus we must import react into every component that we make.  Unlike &lt;code&gt;React&lt;/code&gt;, &lt;code&gt;React Native&lt;/code&gt; comes with only a few components built in.  If you check out the &lt;a href="https://facebook.github.io/react-native/docs/components-and-apis.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;, you will see only about 20 components that are compatible with both iOS and Android devices.  Fortunately, these components are all we need to build powerful applications!  Back to our &lt;code&gt;App.js&lt;/code&gt; file, we notice that the component is importing two of these components: &lt;code&gt;View&lt;/code&gt; and &lt;code&gt;Text&lt;/code&gt;.  &lt;code&gt;View&lt;/code&gt; is essentially our &lt;code&gt;div&lt;/code&gt; tag in React Native.  We can give it properties like &lt;code&gt;style&lt;/code&gt; and also events to make them interactive.  Let's modify our &lt;code&gt;App.js&lt;/code&gt; to include an input and button component so our users can type in a todo and post it to the screen.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import &lt;code&gt;Button&lt;/code&gt; and &lt;code&gt;TextInput&lt;/code&gt; from &lt;code&gt;react-native&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;&amp;lt;TextInput /&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Button title="Add Todo" /&amp;gt;&lt;/code&gt; below the &lt;code&gt;Text&lt;/code&gt; component that is already in &lt;code&gt;App.js&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Upon saving, you should see the new button render on your phone!  The TextInput will not be visible.  We can give it styling by adding an inline style prop.  Add &lt;code&gt;style={{borderWidth: 1, width: 300}}&lt;/code&gt; to the &lt;code&gt;TextInput&lt;/code&gt; component.  Now, you should see the input field when you save!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is what my &lt;code&gt;App.js&lt;/code&gt; component looks like at this point:&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;export&lt;/span&gt; &lt;span class="k"&gt;default&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="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;View&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="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;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Awesome Todo Demo&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&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;TextInput&lt;/span&gt; &lt;span class="na"&gt;style&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="na"&gt;borderWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&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="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Add Todo"&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;View&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;h2&gt;
  
  
  Adding Events
&lt;/h2&gt;

&lt;p&gt;Clicking on our button will trigger a nice animation, but clearly, nothing happens.  Just like in react, we need to tell the Button component what to do when it is pressed.  This is done with an &lt;code&gt;onPress&lt;/code&gt; prop.  We could use an inline function to handle the button press, but it is best practice to create a separate function within our component to do this.  We also need to add a prop to our TextInput component in order to &lt;em&gt;save&lt;/em&gt; the input that is typed in.  We will store the current input text and submitted todo's using the &lt;code&gt;useState&lt;/code&gt; hook built into &lt;em&gt;React&lt;/em&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add state to our App component to store user text input and submitted todo's.

&lt;ul&gt;
&lt;li&gt;import &lt;code&gt;useState&lt;/code&gt; from react at top of our file.&lt;/li&gt;
&lt;li&gt;create a state variable and setter for user input and submitted todos's.  Place these before the return statement inside of your &lt;code&gt;App&lt;/code&gt; component:
&lt;/li&gt;
&lt;/ul&gt;
&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;textInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTextInput&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Notice we are initializing our &lt;code&gt;textInput&lt;/code&gt; state as an empty string and &lt;code&gt;todos&lt;/code&gt; as an array literal&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;pressHandler&lt;/code&gt; function above the &lt;code&gt;return&lt;/code&gt; inside of our &lt;code&gt;App&lt;/code&gt; component.
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pressHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;textInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;We use the spread operator to extract all of the previously saved todos and add the new todo stored in &lt;code&gt;textInput&lt;/code&gt; to the end of the todos array.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;typingHandler&lt;/code&gt; function to update the &lt;code&gt;textInput&lt;/code&gt; state when the user types into the text input component:
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;typingHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTextInput&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add props to our &lt;code&gt;TextInput&lt;/code&gt; and &lt;code&gt;Button&lt;/code&gt; components to fire these functions whenever text is inputed or the button is pressed.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;onChangeText={typingHandler} value={textInput}&lt;/code&gt; props to the &lt;code&gt;TextInput&lt;/code&gt; component.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;onPress={pressHandler}&lt;/code&gt; to the &lt;code&gt;Button&lt;/code&gt; component.
&lt;em&gt;We add the &lt;code&gt;value&lt;/code&gt; prop to our &lt;code&gt;TextInput&lt;/code&gt; in order to store the current value that has been typed into the input area.  It is automatically sent to our &lt;code&gt;typingHandler&lt;/code&gt; function whenever text is added.&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is what our &lt;code&gt;App.js&lt;/code&gt; looks like so far:&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;TextInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;Button&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&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;textInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTextInput&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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;pressHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;textInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;typingHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTextInput&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="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;View&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="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;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Awesome Todo Demo&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&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;TextInput&lt;/span&gt; 
        &lt;span class="na"&gt;onChangeText&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;typingHandler&lt;/span&gt;&lt;span class="si"&gt;}&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;textInput&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;style&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="na"&gt;borderWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&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="nc"&gt;Button&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Add Todo"&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;View&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;container&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#fff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;alignItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;center&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;justifyContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;center&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;h2&gt;
  
  
  Creating our own &lt;code&gt;Todo&lt;/code&gt; component
&lt;/h2&gt;

&lt;p&gt;In order to display our submitted todo's, we need to create a new component!  Normally, we would create a new file to do this but for the sake of this tutorial, we can do so under our App component.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;Todo&lt;/code&gt; component at the bottom of &lt;em&gt;App.js&lt;/em&gt;:
&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="nc"&gt;View&lt;/span&gt; 
    &lt;span class="na"&gt;style&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="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#eaeaea&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="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;Text&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&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;View&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;ol&gt;
&lt;li&gt;Import &lt;code&gt;FlatList&lt;/code&gt; component from react.  This will be used to display our list.  This component will allow our saved todos to be rendered to the screen.  It will also allow us to scroll if there are more todos than space allows.  Scrolling would otherwise not be enabled.&lt;/li&gt;
&lt;li&gt; Add &lt;code&gt;FlatList&lt;/code&gt; component below our submit button
&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;FlatList&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;renderItem&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Todo&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;}&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;&lt;em&gt;Notice how we pass our &lt;code&gt;todos&lt;/code&gt; prop to the &lt;code&gt;data&lt;/code&gt; prop within the &lt;code&gt;FlatList&lt;/code&gt; component.  The &lt;code&gt;renderItem&lt;/code&gt; prop acts like &lt;code&gt;map&lt;/code&gt; in javascript and accepts a function that is called for each todo in the &lt;code&gt;todos&lt;/code&gt; array.  Notice that all of the text for each &lt;code&gt;todo&lt;/code&gt; is located on the &lt;code&gt;item&lt;/code&gt; property.  Confusing, yes, but it is where we have to point to to access our todo text.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Add &lt;code&gt;marginTop: 50&lt;/code&gt; to the &lt;code&gt;container&lt;/code&gt; object inside of &lt;code&gt;styles&lt;/code&gt;.  This is necessary because adding the &lt;code&gt;FlatList&lt;/code&gt; pushes all of our components to the top of the phone screen.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At this point, we should have a working App!  Go ahead and add some todo's and see it in action!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fog1pebsfon897m9d0mrr.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fog1pebsfon897m9d0mrr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may notice some yellow warning messages at the bottom of your phone.  These appear because we are not giving each &lt;code&gt;todo&lt;/code&gt; component a unique key.  For now, just dismiss the messages but know that you should be passing a unique key to each component when you do this in the future.  Since todo's would probably be stored in some sort of database, this key would usually be available.&lt;/p&gt;

&lt;p&gt;Here is the final code for &lt;code&gt;App.js&lt;/code&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="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;TextInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;FlatList&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&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;textInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTextInput&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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;pressHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTodos&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;textInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;typingHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTextInput&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="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;View&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="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;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Awesome Todo Demo&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&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;TextInput&lt;/span&gt;
        &lt;span class="na"&gt;onChangeText&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;typingHandler&lt;/span&gt;&lt;span class="si"&gt;}&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;textInput&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;style&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="na"&gt;borderWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&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="nc"&gt;Button&lt;/span&gt;
        &lt;span class="na"&gt;onPress&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pressHandler&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Add Todo"&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;FlatList&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;renderItem&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Todo&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;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="nc"&gt;View&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;StyleSheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;container&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;marginTop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#fff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;alignItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;center&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;justifyContent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;center&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="nc"&gt;View&lt;/span&gt; 
    &lt;span class="na"&gt;style&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="na"&gt;backgroundColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#eaeaea&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="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;Text&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&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;View&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;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>reactnative</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building a React app with Next.js</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Mon, 18 Nov 2019 06:14:20 +0000</pubDate>
      <link>https://dev.to/aveb/building-a-react-app-with-next-js-303f</link>
      <guid>https://dev.to/aveb/building-a-react-app-with-next-js-303f</guid>
      <description>&lt;p&gt;Getting started with React is pretty easy.  Install it, run &lt;code&gt;create-react-app&lt;/code&gt; and poof!  You have an app -- well, the client, at least.  Afterwards, it's up to you, the developer, to choose a file structure, add routing, build out a server, configure the webpack, choose a styling library -- the list goes on.  This makes sense - React is a Javascript library.  For those looking for a bit more direction, &lt;strong&gt;Next.js&lt;/strong&gt; to the rescue.  &lt;em&gt;This post will explain why Next.js has become a leading React framework and provide a step-by-step guide to set up a basic app.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Next.js?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Next.js&lt;/strong&gt; is a server-side rendering React framework.  This means that the app code is compiled on the server-side and sent to the client only once it has fully rendered.  The difference isn't very noticeable in a web browser unless we look under the hood.  Let's use the google chrome tools to inspect a normal React app and contrast it with a Next app.&lt;/p&gt;

&lt;h3&gt;
  
  
  React
&lt;/h3&gt;

&lt;p&gt;&lt;a href="//web.whatsapp.com"&gt;whatsapp web&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OWDHJspn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cioasflcpnq4af482sfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OWDHJspn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/cioasflcpnq4af482sfm.png" alt="whatsapp"&gt;&lt;/a&gt;&lt;br&gt;
This is all you get for the most popular message app in the world!  The rest of the application will be rendered by the browser depending on user input and interaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next.js
&lt;/h3&gt;

&lt;p&gt;&lt;a href="//www.nike.com"&gt;Nike&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--80ZcVYyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bbbrsljnuf0pid9mp8w6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--80ZcVYyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bbbrsljnuf0pid9mp8w6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n2YSRTnD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8hy9o7ku802qd6fs8jza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n2YSRTnD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8hy9o7ku802qd6fs8jza.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
...and there is much more that I didn't screenshot as I think I have made my point -- there is way more when we inspect a Next.js app.  So, you are probably wondering &lt;em&gt;why&lt;/em&gt; on earth we would ever want that?  Let's talk about that now:&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Next.js?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Search Engine Optimization (SEO)&lt;/strong&gt;. In order for our websites/apps to be found on the internet, it is important to make them easy to find by common search engines like google.  Without getting into too much boring detail about &lt;strong&gt;SEO&lt;/strong&gt;, just know that one of the main factors is including keywords in your app's HTML so it can be easily found by bots and web-scrapers.  In the &lt;a href="//web.whatsapp.com"&gt;whatsapp web&lt;/a&gt; HTML above, you can see that there isn't much to go off of.  Clearly, we see the opposite with the &lt;a href="//www.nike.com"&gt;Nike&lt;/a&gt; code.  Try and find the &lt;em&gt;INDEX_PAGE_META_KEYWORDS&lt;/em&gt; near the bottom of the picture above.  These are used to help identify this page as relevant in internet searches.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Next.js&lt;/em&gt; also makes some of the harder architecture decisions for you, particularly when setting up your file structure and routing.  Routing, in particular, requires quite a bit of set-up including the installation of additional &lt;code&gt;npm packages&lt;/code&gt; like &lt;code&gt;react-router&lt;/code&gt;.  Fortunately, &lt;em&gt;Next.js&lt;/em&gt; comes with this capability out of the box, with far less code necessary for implementation.  We will see this in action in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started with Next.js
&lt;/h2&gt;

&lt;p&gt;In this next section, we will be building a basic &lt;em&gt;Hello World&lt;/em&gt; application with a few different routes to demonstrate some of the decisions that Next.js makes for you, the developer.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make a project directory&lt;/li&gt;
&lt;li&gt;Create a package.json
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir next-demo
$ cd next-demo
$ npm init -y
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;code&gt;-y&lt;/code&gt; flag automatically runs &lt;code&gt;npm init&lt;/code&gt;, skipping all the questions you would normally be asked during setup.  It is totally optional.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;code&gt;react&lt;/code&gt;, &lt;code&gt;react-dom&lt;/code&gt;, and &lt;code&gt;next&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install --save react react-dom next
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In contrast to &lt;code&gt;create-react-app&lt;/code&gt;, you still have a little work to do if you want to actually see anything in the browser. &lt;em&gt;If you try to start up your app now, you will get an error message.&lt;/em&gt; . &lt;/p&gt;

&lt;p&gt;We need to add a few more folders and files for us to be able to see our app in action.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create a pages directory and index.js file
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir pages
$ touch pages/index.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Next.js&lt;/em&gt; automatically looks inside the &lt;code&gt;pages&lt;/code&gt; directory for the &lt;code&gt;index.js&lt;/code&gt; file when rendering.  It is a requirement for you to set up your app.  Let's add a simple React component to our &lt;code&gt;index.js&lt;/code&gt; file in order for us to be able to actually see something when we run our app in our local environment.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Add a React component to &lt;code&gt;index.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="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;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="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;h1&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;&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="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;ol&gt;
&lt;li&gt; Add a start script to &lt;code&gt;package.json&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;add &lt;code&gt;"start": "next"&lt;/code&gt; inside of scripts in &lt;code&gt;package.json&lt;/code&gt;.  your scripts should look something like this:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u4Mv6gL1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/7ii2md67i5m0qoquke3u.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, start up your app with &lt;code&gt;npm start&lt;/code&gt;!  It will take a moment for your app to compile, but you should see a link display in your terminal for &lt;a href="http://localhost:3000/"&gt;localhost:3000&lt;/a&gt;.  Here's what you should see:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qxmt3ns4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ivkr7z5bv05o6dq82lgc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qxmt3ns4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ivkr7z5bv05o6dq82lgc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cool!  We have a working app!  Without stopping your server, try changing the text for your &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;.  Notice how your app is automatically re-rendered upon saving.  Pretty easy to set up, right?!&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic routing
&lt;/h2&gt;

&lt;p&gt;One of my big gripes with react is that setting up routing can be a bit of a pain.  In order to do any routing at all in &lt;code&gt;create-react-app&lt;/code&gt;, for example, we have to install a package like &lt;code&gt;react-router&lt;/code&gt;.  &lt;em&gt;Next.js&lt;/em&gt; handles this for us as "routes" are created by simply naming our files within the &lt;code&gt;pages&lt;/code&gt; directory.  Our &lt;code&gt;index.js&lt;/code&gt; file defaults as our &lt;code&gt;/&lt;/code&gt; route.  All others will be the exact same as the file name.  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create a new route by creating &lt;code&gt;home.js&lt;/code&gt; within our pages directory:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$touch pages/home.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt; Add a basic functional component to &lt;code&gt;home.js&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="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;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="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;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&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;&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if we visit &lt;a href="http://localhost:3000/home"&gt;localhost:3000/home&lt;/a&gt;, we should see:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f7XMCXuv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ucm8wlzxe7hm0vrnklf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f7XMCXuv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ucm8wlzxe7hm0vrnklf4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order for us to link these two routes that we have created, we need to add a &lt;code&gt;Link&lt;/code&gt; component to each of our pages.  This will allow us to jump back and forth via client side routing, which is optimal for speed and creating single page applications (what React was made for)!  This can be done by adding &lt;code&gt;import Link from "next/link"&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Modify your &lt;code&gt;index.js&lt;/code&gt; by adding a link to your &lt;code&gt;home.js&lt;/code&gt; page:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="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;Link&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;next/link&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;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="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;h1&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/home"&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;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home Page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&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;Link&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="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;Notice that we add an &lt;code&gt;href&lt;/code&gt; property to our &lt;code&gt;Link&lt;/code&gt; component specifying the route we want.  We also need to pass in a child component to &lt;code&gt;Link&lt;/code&gt;.  I used an &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag here but a &lt;code&gt;button&lt;/code&gt;, &lt;code&gt;div&lt;/code&gt;, or most other text-friendly tags would also work just fine.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Update &lt;code&gt;home.js&lt;/code&gt; by adding a route back to the &lt;code&gt;index.js&lt;/code&gt; page:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="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;Link&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;next/link&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;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="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;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Home&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&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;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Go back to index&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;Link&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Refresh your browser and you should now be able to jump back and forth from your &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;home&lt;/code&gt; pages with ease!&lt;/p&gt;

&lt;h2&gt;
  
  
  Summing up
&lt;/h2&gt;

&lt;p&gt;Getting started with &lt;em&gt;Next.js&lt;/em&gt; is quite painless and fast.  Routing can be quickly set up from the starter code but we have to be careful with our initial setup as folder structure is opinionated.  To see more of what &lt;em&gt;Next.js&lt;/em&gt; can do, I suggest checking out the &lt;a href="https://nextjs.org/learn/basics/getting-started"&gt;docs&lt;/a&gt;.  I don't say this often, but they really are well written!  &lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>ES6 Map -- more than just another Object</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Sun, 10 Nov 2019 02:45:06 +0000</pubDate>
      <link>https://dev.to/aveb/es6-map-more-than-just-another-object-4kco</link>
      <guid>https://dev.to/aveb/es6-map-more-than-just-another-object-4kco</guid>
      <description>&lt;p&gt;ECMAScript 2015, better known as &lt;strong&gt;ES6&lt;/strong&gt;, was many widely-adopted features including &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; variable declaration, array and object &lt;em&gt;de-structuring&lt;/em&gt;, &lt;code&gt;class&lt;/code&gt; keyword, and &lt;em&gt;arrow functions&lt;/em&gt; (just to name a few).  &lt;strong&gt;ES6&lt;/strong&gt; also includes a new datatype: the &lt;code&gt;Map&lt;/code&gt;.  While not yet as widely used as some of the other syntaxes previously mentioned, &lt;strong&gt;Map&lt;/strong&gt; is basically  a souped up javascript &lt;code&gt;object&lt;/code&gt; with some pretty cool features.  This post covers the basics of creating and using &lt;strong&gt;ES6&lt;/strong&gt; &lt;code&gt;map&lt;/code&gt; and also compares it to the more familiar javascript &lt;code&gt;object&lt;/code&gt; along the way.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a map?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Map&lt;/code&gt; is a datatype similar to an &lt;code&gt;object&lt;/code&gt; (ok, technically it is an instance of an object) where data is stored in key-value pairs.  Unlike traditional objects, keys can be &lt;em&gt;any&lt;/em&gt; datatype (keys must be strings in objects).  This is pretty nifty as we can now use numbers, booleans, and even objects as keys!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating our first Map
&lt;/h2&gt;

&lt;p&gt;We make a &lt;code&gt;map&lt;/code&gt; using the &lt;code&gt;new&lt;/code&gt; keyword.  Let's check out the difference between declaring a &lt;code&gt;map&lt;/code&gt; vs. a plain old &lt;code&gt;object&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;normalObject&lt;/span&gt; &lt;span class="o"&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 order to add key-value pairs, &lt;code&gt;map&lt;/code&gt; comes with a &lt;code&gt;set&lt;/code&gt; method that takes a key and value as its arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="nx"&gt;firstMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&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;my key is a number!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;firstMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;using this as a key! WHATT??&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;firstMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;boat&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;I can use strings too!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//object&lt;/span&gt;
&lt;span class="nx"&gt;normalObject&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my key must be a string...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;normalObject&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my key is still a string (:&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;&lt;em&gt;This is particularly useful in front-end development because we can assign properties to object keys like DOM elements!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Working With Maps
&lt;/h2&gt;

&lt;p&gt;Let's look at some of the most common &lt;code&gt;map&lt;/code&gt; methods and their &lt;code&gt;object&lt;/code&gt; equivalent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;property look-up
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="nx"&gt;firstMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 'using this as a key! WHATT??'&lt;/span&gt;
&lt;span class="c1"&gt;// object&lt;/span&gt;
&lt;span class="nx"&gt;normalObject&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 'my key must be a string...'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;delete key-value pairs
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="nx"&gt;firstMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; true&lt;/span&gt;
&lt;span class="c1"&gt;// object&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;normalObject&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// doesn't return anything&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Notice that &lt;code&gt;Map&lt;/code&gt; returns a boolean confirmation upon delete --pretty useful when you aren't sure the key exists in the first place!&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;iterating through all values (let's log all values)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;firstMap&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;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// object&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalObject&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;key&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;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;normalObject&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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;ul&gt;
&lt;li&gt;counting key-value pairs:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="nx"&gt;firstMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 2&lt;/span&gt;
&lt;span class="c1"&gt;// object&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;normalObject&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;checking if key exists
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// map&lt;/span&gt;
&lt;span class="nx"&gt;firstMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; false&lt;/span&gt;
&lt;span class="c1"&gt;// object&lt;/span&gt;
&lt;span class="nx"&gt;normalObject&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;normalObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;With objects, &lt;code&gt;'key' in normalObject&lt;/code&gt; will return true if 'key' exists on the prototype.  &lt;code&gt;maps&lt;/code&gt; do not allow prototypal inheritance, this ridding us from the 'where is the property actually from' puzzle.&lt;/em&gt; &lt;/p&gt;

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

&lt;p&gt;By implementing some subtle yet powerful changes, &lt;strong&gt;ES6&lt;/strong&gt; &lt;code&gt;Map&lt;/code&gt; allows us to save key-value pairs in memory in a more logical, legible way.  Operations like lookup and iteration are done with clear and concise code - a definite step up from the verbose and somewhat confusing object syntax.  In web development, &lt;code&gt;Map&lt;/code&gt; is particularly useful when we want to associate properties with complex datatypes such as objects.  Some drawbacks include loss of performance when adding tons of properties (&lt;a href="https://twitter.com/goodmodule/status/820582596648325120"&gt;see this graph&lt;/a&gt;) and lack of compatibility with JSON (at least, for now).  What is clear is that the brains behind &lt;em&gt;javascript&lt;/em&gt; are actively trying to improve upon one of language's most fundamental components (objects, of course).  &lt;code&gt;Map&lt;/code&gt; is a step in the right direction and it will be interesting to see what ECMAScript 2020 has in store.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Introduction to Linked Lists w/ ES6</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Mon, 21 Oct 2019 03:27:41 +0000</pubDate>
      <link>https://dev.to/aveb/introduction-to-linked-lists-w-es6-26c8</link>
      <guid>https://dev.to/aveb/introduction-to-linked-lists-w-es6-26c8</guid>
      <description>&lt;p&gt;I've spent the past month or so working through coding challenges on sites like &lt;a href="https://leetcode.com"&gt;leetcode&lt;/a&gt; and &lt;a href="//codewars.com"&gt;codewars&lt;/a&gt; and have noticed that problems involving &lt;strong&gt;Linked Lists&lt;/strong&gt; are quite popular.  In fact, 6 of the top 25 most popular interview questions from &lt;a href="https://leetcode.com"&gt;leetcode&lt;/a&gt; involve link lists in some form.  In this post, we will cover the basics of &lt;em&gt;singly&lt;/em&gt; and &lt;em&gt;doubly&lt;/em&gt; linked lists, and look at a simple implementation utilizing the popular &lt;code&gt;Class&lt;/code&gt; keyword in &lt;em&gt;Javascript ES6&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Linked List?
&lt;/h2&gt;

&lt;p&gt;A linked list is a &lt;em&gt;linear&lt;/em&gt; data structure where each piece of data, referred to as a &lt;strong&gt;node&lt;/strong&gt;, also contains a reference to the location of another node in memory.  In plain english, each element of the list knows the location of the next.  Linked lists are more common than you might think: music apps incorporate linked lists in song playlists, sites like &lt;a href="//ticketmaster.com"&gt;ticketmaster&lt;/a&gt; use them to keep track of a virtual line of customers (ever seen the "you have 5 minutes remaining to complete your purchase"?).&lt;/p&gt;

&lt;p&gt;Link lists come in two forms -- &lt;em&gt;Singly and Doubly-linked lists&lt;/em&gt;.  Let's look at a visual and code example of each:&lt;/p&gt;

&lt;h4&gt;
  
  
  Singly-linked-list
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4S5rlmNE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1y5ocmdxfmusiyzcl7s3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4S5rlmNE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/1y5ocmdxfmusiyzcl7s3.png" alt="SLL"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The beginning of the list is referred to as the &lt;strong&gt;head&lt;/strong&gt;.  Each subsequent node keeps a reference only to the location of the next.  The node at the end of the list is called the &lt;strong&gt;tail&lt;/strong&gt;.  It does not contain any references to any other node.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;ES6 allows us to use the &lt;code&gt;class&lt;/code&gt; keyword to construct a basic singly-linked-list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;SinglyLinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="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;We create our list by executing &lt;code&gt;const SLL = new SinglyLinkedList()&lt;/code&gt; which creates a javascript object that has a head, tail and optional length property.  In order to create the nodes, it is common to write another constructor function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;In order to add values to our linked list, we can add an instance method to our SinglyLinkedList class.  Let's create an unshift method, which will add a node to the beginning of the list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;SinglyLinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="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="nx"&gt;val&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;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;//if no head&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="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;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="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;Notice that in order to add a node to the beginning of our list, we first must create the node by invoking &lt;code&gt;new Node()&lt;/code&gt;, passing in a value.  This creates an object which inherits from our &lt;em&gt;Node&lt;/em&gt; class.  We must then make sure that we set the head and the tail to our new node if the node being created is the first in our list.  If it is not the first, we only need to update the head, pointing the new node's next property to that of the old head.  Last, we increment our count property to keep track of the total number of nodes in our list and optionally return are update list (represented by &lt;code&gt;this&lt;/code&gt;).  Unlike javascript arrays, &lt;em&gt;adding to the front of our linked list takes constant (&lt;strong&gt;O(1)&lt;/strong&gt;) time thus demonstrating one of the main benefits of using a linked list when adding to the back of line is desirable&lt;/em&gt;.  Arrays, in contrast, unshift in linear (O(n)) time.  &lt;/p&gt;

&lt;h4&gt;
  
  
  Doubly-Linked-Lists
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QxAmieKe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9d89atwye9qgslh9z52i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QxAmieKe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9d89atwye9qgslh9z52i.png" alt="DLL"&gt;&lt;/a&gt;&lt;br&gt;
Notice how each node in between the head and tail keep track of the nodes in the position immediately before and after.  We only need to make a few modifications from our singly-linked-list code from before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;DoublyLinkedList&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="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="nx"&gt;val&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;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;//if no head&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previous&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//Node class constructor&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;previous&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;Notice that when unshifting in our doubly-linked-list, we have to set the old head's &lt;code&gt;previous&lt;/code&gt; property to point to the new node before updating the head property of the linked list to the new node.  While this may seem like a bit more code, a doubly linked list can be &lt;em&gt;traversed both forwards and backwards&lt;/em&gt; where as a singly linked list can only be traversed from the head.  We use a doubly-linked-list type structure daily as we navigate back and forth in our browser with the forwards and backwards arrows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summing up
&lt;/h2&gt;

&lt;p&gt;This post really just scratches the surface of what these data structures look like and how they function.  I highly recommend checking out &lt;a href="https://visualgo.net/en/list"&gt;visualgo's&lt;/a&gt; animation on this any many other methods if you want to learn more.  When you are ready to take on some interview questions, try out the following from &lt;a href="https://leetcode.com"&gt;leetcode's&lt;/a&gt; top 100 interview questions:&lt;br&gt;
 1.&lt;a href="https://leetcode.com/problems/add-two-numbers/"&gt;Add two numbers&lt;/a&gt;&lt;br&gt;
 2.&lt;a href="https://leetcode.com/problems/remove-nth-node-from-end-of-list/"&gt;Remove Nth Node From End of List&lt;/a&gt;&lt;br&gt;
 3.&lt;a href="https://leetcode.com/problems/merge-two-sorted-lists/"&gt;Merge two sorted lists&lt;/a&gt;&lt;br&gt;
 4.&lt;a href="https://leetcode.com/problems/merge-k-sorted-lists/"&gt;Merge k sorted lists&lt;/a&gt;&lt;br&gt;
 5.&lt;a href="https://leetcode.com/problems/swap-nodes-in-pairs/"&gt;Swap nodes in pairs&lt;/a&gt;&lt;br&gt;
 6.&lt;a href="https://leetcode.com/problems/reverse-nodes-in-k-group/"&gt;Reverse nodes in k group&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
    </item>
    <item>
      <title>Intro to React Hooks</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Mon, 14 Oct 2019 03:21:43 +0000</pubDate>
      <link>https://dev.to/aveb/intro-to-react-hooks-4h56</link>
      <guid>https://dev.to/aveb/intro-to-react-hooks-4h56</guid>
      <description>&lt;p&gt;Most web developers would probably agree that the the &lt;strong&gt;React Javascript Library&lt;/strong&gt; is pretty simple to implement.  Just imagine the component you want, whip up some JSX, render and BOOM!  You've got an app!  Prior to this year, however, it was necessary for you to decide &lt;em&gt;what type&lt;/em&gt; of component you wanted to build before you started coding.  If you wanted to make a component to simply display data, like a card or list item, you would make a &lt;strong&gt;functional component&lt;/strong&gt;.  If you wanted to make a smart, reactive component that listens to client input and keeps track of certain properties (&lt;strong&gt;State&lt;/strong&gt;), you make a stateful &lt;strong&gt;class component&lt;/strong&gt;.  With the release of React 16.8 (Feb 2019), you can now stick with just functional components with React &lt;strong&gt;hooks&lt;/strong&gt;.  Hooks allow us to give functional components &lt;strong&gt;state&lt;/strong&gt; without having to create class components.  &lt;em&gt;This blog will compare a class component and hooked functional component that both create the same component&lt;/em&gt;. Before we see hooks in action, let's take a look at an example of each:&lt;/p&gt;

&lt;h4&gt;
  
  
  functional component
&lt;/h4&gt;



&lt;div class="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;listItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Print my &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;li&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;this component takes &lt;code&gt;props&lt;/code&gt; as an argument and returns an &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; tag that displays the name property on props.  Nice and simple.&lt;/p&gt;

&lt;h4&gt;
  
  
  class component with state
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;BankAccount&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Your Balance is: $&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&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;h3&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;Right a way, we see that class components require quite a bit more than their functional counterparts.  We must use the ES6 &lt;code&gt;class&lt;/code&gt; keyword and extend from &lt;code&gt;React.Component&lt;/code&gt; and call &lt;code&gt;super(props)&lt;/code&gt; in our constructor in order to access some special methods we will need later on in order to make our component keep track of state.  We must also explicitly define a render method, which will actually create our component when a new instance is instantiated.  Notice from our functional component example, &lt;strong&gt;no render is required&lt;/strong&gt;.  React takes care of the rendering behind the scene, saving us code.&lt;/p&gt;

&lt;p&gt;This component represents a bank account with a balance that is stored with a &lt;code&gt;state&lt;/code&gt; object.  If we wanted to add money to our balance, we could create an instance method to update the &lt;code&gt;state&lt;/code&gt; using the built in method &lt;code&gt;setState&lt;/code&gt; method that comes out of the box with class components in react.  Below is the updated bank account with a &lt;code&gt;deposit&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;BankAccount&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deposit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;amount&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;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Your Balance is: $&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&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;h3&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;Calling &lt;code&gt;deposit(100)&lt;/code&gt; updates the &lt;code&gt;balance&lt;/code&gt; property and the render method is automatically called thus an updated balance of $110 would be added to the DOM.  Cool stuff right?  Well we can accomplish the same thing in less code by using a &lt;strong&gt;functional component with a hook&lt;/strong&gt;.  Check out our updated &lt;code&gt;BankAccount&lt;/code&gt; below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BankAccount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setBalance&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deposit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&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;setBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;amount&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Your Balance is: $&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;balance&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;h3&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;While we do need to import a &lt;code&gt;useState&lt;/code&gt; method from react, our code is significantly reduced.  &lt;code&gt;useState&lt;/code&gt; creates an array with two elements: the first we assign to &lt;code&gt;balance&lt;/code&gt;.  This is the exact same as &lt;code&gt;this.state.balance&lt;/code&gt; from the class example.  The second element we assign to &lt;code&gt;setBalance&lt;/code&gt;.  This acts in the same way as &lt;code&gt;setState&lt;/code&gt; from our class example.  Nice that we can name our setter something that actually fits!  We are also saved from writing a &lt;code&gt;render&lt;/code&gt; method as react takes care of it for us.  Nice!  We have successfully &lt;strong&gt;hooked&lt;/strong&gt; our functional component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Gz285nA3Kq5YQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Gz285nA3Kq5YQ/giphy.gif" alt="fishing gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With our refactoring of the &lt;code&gt;BankAccount&lt;/code&gt; component, we have just begun to scratch the surface of what hooks can do for us.  We can also:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write lifecycle methods in functional components using &lt;code&gt;useEffect&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create multiple states within a single function&lt;/li&gt;
&lt;li&gt;Avoid the confusing &lt;code&gt;this&lt;/code&gt; keyword&lt;/li&gt;
&lt;li&gt;Share non-visual logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When learning react, a developer can focus on just one component syntax to create dynamic and complex components with less code.  Impressive for a library that is already one of the most popular around.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Making Your First Get Request to Youtube Search API</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Mon, 07 Oct 2019 02:47:43 +0000</pubDate>
      <link>https://dev.to/aveb/making-your-first-get-request-to-youtube-search-api-4c2f</link>
      <guid>https://dev.to/aveb/making-your-first-get-request-to-youtube-search-api-4c2f</guid>
      <description>&lt;p&gt;When I was first introduced to &lt;strong&gt;API&lt;/strong&gt;s (Application programming interfaces), I was thrilled.  How amazing is it that we can harness the power of one or many other applications within our own!?  Then I tried to make my first request...  Let's just say I spent the next few hours learning about every &lt;strong&gt;400&lt;/strong&gt;something error there is.  So how do we actually get an &lt;strong&gt;API&lt;/strong&gt; to talk back nicely?  This article will walk you through making a simple &lt;strong&gt;GET&lt;/strong&gt; request to the &lt;a href="https://developers.google.com/youtube/v3/docs/search/list" rel="noopener noreferrer"&gt;Youtube Search API&lt;/a&gt; using &lt;em&gt;javascript&lt;/em&gt; and a bit of &lt;em&gt;JQuery&lt;/em&gt;.  Let's get to it! &lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up your environment:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open up a new &lt;em&gt;pen&lt;/em&gt; in &lt;a href="https://codepen.io" rel="noopener noreferrer"&gt;CodePen&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add &lt;em&gt;Jquery&lt;/em&gt; to your &lt;code&gt;.html&lt;/code&gt; file.  We will need &lt;em&gt;Jquery&lt;/em&gt; to make our request and to update a few HTML elements that we will add shorty.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://code.jquery.com/jquery-3.4.1.min.js"&lt;/span&gt;
&lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="&lt;/span&gt;
&lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;If you have something against CodePen, feel free to create your files on your local machine and edit them in your favorite text editor.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;We need a few more HTML elements in order to display the data we will get back from the youtube API.  Add the following to your &lt;code&gt;.html&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="nt"&gt;&amp;lt;iframe&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/iframe&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;Video Title&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"description"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Video Description&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At this point, your page should look like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwkpnvoo3alm3ruh0s1rv.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwkpnvoo3alm3ruh0s1rv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get your API key.  This is necessary in order to make requests to the Youtube Data API.  Requests without this key will respond with one of those nasty 400 codes. Navigate to the &lt;a href="https://console.developers.google.com" rel="noopener noreferrer"&gt;google developers console&lt;/a&gt;.  If you don't have an account, take a moment to set one up &lt;strong&gt;(It's Free!)&lt;/strong&gt; . Once you are logged in, you will need to:

&lt;ul&gt;
&lt;li&gt;Create a new project&lt;/li&gt;
&lt;li&gt;Enable Youtube Data API&lt;/li&gt;
&lt;li&gt;Create Credentials&lt;/li&gt;
&lt;li&gt;Copy Your API Key&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Here's a quick video if you want to see how I got my key for this tutorial:&lt;/em&gt;&lt;br&gt;
&lt;a href="http://youtu.be/iItL3MmqJQo?hd=1" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F3ivmxeeglrrxlbshoe2a.jpg" alt="get your api key"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Making a GET request
&lt;/h2&gt;

&lt;p&gt;We've got our HTML template, we have our API key, now let's write some javascript.  We will be using Ajax -- a feature built into JQuery in order to make our request.  Let's write a function that will make a &lt;code&gt;GET&lt;/code&gt; request to the youtube API.  In this request we will be passing along some information to YouTube telling them that we want them to send us back some information about a video.  With that information, we will be able to successfully embed the video in our page!&lt;/p&gt;
&lt;h4&gt;
  
  
  Our request function:
&lt;/h4&gt;

&lt;p&gt;Place the following into your &lt;code&gt;.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getVideo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ajax&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;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://www.googleapis.com/youtube/v3/search&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PASTE YOUR API KEY HERE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;part&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;snippet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;maxResults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;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;video&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;videoEmbeddable&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="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nf"&gt;embedVideo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Request Failed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;getVideo()&lt;/code&gt; function calls a jquery method &lt;code&gt;ajax&lt;/code&gt;.  This message takes an object with some special properties to help us request and process our data. Let's break down the properties inside this &lt;code&gt;ajax&lt;/code&gt; call.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Our first property specifies that we are making a &lt;code&gt;GET&lt;/code&gt; request.&lt;/li&gt;
&lt;li&gt;We then set our &lt;code&gt;path&lt;/code&gt; to the youtube data API as a string.&lt;/li&gt;
&lt;li&gt;Next, we define a &lt;code&gt;data&lt;/code&gt; property and create an object to contain all the necessary parameters that youtube needs. A &lt;code&gt;key&lt;/code&gt; and &lt;code&gt;part&lt;/code&gt; property are required by the API.  Paste in your newly created API key (in quotes) and make sure you specify &lt;code&gt;"snippet"&lt;/code&gt; as your &lt;code&gt;part&lt;/code&gt;.  This will ensure that we receive an object with all the info necessary to embed our video.  I have added a few additional properties to make sure we get back viable data.  In order to get back just one video, I have set &lt;code&gt;maxResults&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;.  &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;videoEmbeddable&lt;/code&gt; ensure that I get back the data for a video that is actually able to be embedded into a site.  Feel free to checkout the &lt;a href="https://developers.google.com/youtube/v3/docs/search/list" rel="noopener noreferrer"&gt;youtube search documentation&lt;/a&gt; to see all the options you can put in the data object. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;success&lt;/code&gt; will call a function with the data we receive back from our &lt;code&gt;GET&lt;/code&gt; request.  the &lt;code&gt;ajax&lt;/code&gt; method will magically wait for us to receive the data before calling the function.  In just a moment, we will write out &lt;code&gt;embedVideo&lt;/code&gt; function inside, so just hang tight!&lt;/li&gt;
&lt;li&gt;Lastly, we assign &lt;code&gt;error&lt;/code&gt; to a function that will log the server response if it is unsuccessful. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Using the Data Received from our Request
&lt;/h2&gt;

&lt;p&gt;Time to see if we are getting anything back!  Add the following code to process a successful request and render some data to the &lt;strong&gt;DOM&lt;/strong&gt;.&lt;br&gt;
Add the following to your &lt;code&gt;.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;embedVideo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&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;iframe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src&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;https://www.youtube.com/embed/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&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="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;videoId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&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;h3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&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="nx"&gt;snippet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&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;.description&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&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="nx"&gt;snippet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&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;&lt;code&gt;data&lt;/code&gt; is returned to us by our &lt;code&gt;ajax&lt;/code&gt; message as an array. In the first line inside of our function, we use &lt;code&gt;jquery&lt;/code&gt; to select our &lt;code&gt;iframe&lt;/code&gt; element and update the &lt;code&gt;src&lt;/code&gt; attribute.  In order to embed a video, you must provide &lt;code&gt;'https://www.youtube.com/embed/'&lt;/code&gt; + the unique &lt;code&gt;videoId&lt;/code&gt; that we can access within the data array from our request.  Since we only are receiving one video, we access the 0 index with &lt;code&gt;data.items[0]&lt;/code&gt; we then access the &lt;code&gt;id&lt;/code&gt; property and finally, the &lt;code&gt;videoId&lt;/code&gt; property.  Quite the drilling, but expect this when working with most API's.  The next two lines employ similar drilling down into the data array to retrieve the video's title and description, updating the &lt;code&gt;h3&lt;/code&gt; and &lt;code&gt;p&lt;/code&gt; text on the &lt;strong&gt;DOM&lt;/strong&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing up!
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Double check to make sure you have pasted in your API key to the &lt;code&gt;getVideo&lt;/code&gt; function!!&lt;/strong&gt;&lt;br&gt;
Let's make our request!  We must call our &lt;code&gt;getVideo&lt;/code&gt; function.  Add the following function invocation at the bottom of your &lt;code&gt;.js&lt;/code&gt; file:&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="nf"&gt;getVideo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, here's what your app should look like:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fidmen983xx4xb476avn3.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fidmen983xx4xb476avn3.png" alt="preview"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try changing the &lt;code&gt;q&lt;/code&gt; string inside your &lt;code&gt;ajax&lt;/code&gt; request and watch the app update to a new video!  Pretty cool, right?  Check out the app on CodePen if you want to see all of the code together.  I have &lt;strong&gt;taken out&lt;/strong&gt; the &lt;strong&gt;youtube data API key&lt;/strong&gt; so make sure you paste yours in so you can actually see some data displayed!&lt;/p&gt;

&lt;p&gt;If you made it this far, hopefully you feel a little less lost making requests to APIs.  Until next time!&lt;/p&gt;

&lt;p&gt;Avery&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/aveb87/pen/qBBBVqO" rel="noopener noreferrer"&gt;Link to my CodePen for this post if you want to fork it&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Javascript Class Syntax</title>
      <dc:creator>Avery Berkowitz</dc:creator>
      <pubDate>Mon, 30 Sep 2019 05:23:34 +0000</pubDate>
      <link>https://dev.to/aveb/uses-of-static-methods-javascript-19ao</link>
      <guid>https://dev.to/aveb/uses-of-static-methods-javascript-19ao</guid>
      <description>&lt;p&gt;Of all the changes adopted in ECMAScript 2015 &lt;strong&gt;(ES6)&lt;/strong&gt;, none may be quite as controversial as the introduction of the &lt;code&gt;class&lt;/code&gt; keyword.  A quick google search yields mixed messages -- many are avidly for or against this it.  Some feel that javascript is giving in to the pressure from other popular &lt;em&gt;classical&lt;/em&gt; programming languages like &lt;strong&gt;Python&lt;/strong&gt; and &lt;strong&gt;Java&lt;/strong&gt;.  Controversy aside, the &lt;code&gt;class&lt;/code&gt; keyword hides complexity and allows even the inexperienced developer to quickly leverage the power of javascript's &lt;strong&gt;prototype inheritance&lt;/strong&gt; style of object oriented programming with clear, concise code.  This article will introduce javascript &lt;code&gt;class&lt;/code&gt; syntax and creation of &lt;code&gt;static&lt;/code&gt; class methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating "Classes"
&lt;/h2&gt;

&lt;p&gt;The javascript &lt;code&gt;class&lt;/code&gt; is really just a handy type of function.  When invoked with the &lt;code&gt;new&lt;/code&gt; keyword, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates an object&lt;/li&gt;
&lt;li&gt;Points the newly recreated object to the class constructor's prototype object&lt;/li&gt;
&lt;li&gt;Adds properties and methods, if specified, to object&lt;/li&gt;
&lt;li&gt;Returns newly created object&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see &lt;code&gt;class&lt;/code&gt; in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Nfl {
    constructor(){
        this.sport = "American Football";
        this.level = "Professional";
        this.salaryCap = 188200000;
    }
    raiseSalaryCap(newCap){
        this.salaryCap = newCap;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;new Nfl()&lt;/code&gt; will create and return a new object with access to the &lt;code&gt;Nfl&lt;/code&gt; class properties, as well as the &lt;code&gt;raiseSalaryCap&lt;/code&gt; method.  &lt;code&gt;this&lt;/code&gt; will conveniently refer to the specific instance of &lt;code&gt;Nfl&lt;/code&gt;.  Prior to &lt;strong&gt;ES6&lt;/strong&gt;, it was necessary to attach methods directly to the constructors prototype in order for them to be inherited by instances.  &lt;code&gt;class&lt;/code&gt; takes care of it for us!&lt;/p&gt;

&lt;h2&gt;
  
  
  Subclassing
&lt;/h2&gt;

&lt;p&gt;When creating subclasses, we can &lt;code&gt;extend&lt;/code&gt; from any other &lt;code&gt;class&lt;/code&gt;, instead of explicitly pointing our subclass to the constructor's prototype.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Team extends Nfl {
    constructor(name){
        super();
        this.name = name;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Any &lt;code&gt;new Team()&lt;/code&gt; will still have access to &lt;code&gt;Nfl&lt;/code&gt;'s properties and methods via its prototype.  This connection is created through the use of the &lt;code&gt;extend&lt;/code&gt; keyword and &lt;code&gt;super&lt;/code&gt;.  &lt;strong&gt;We don't have to explicitly set it&lt;/strong&gt;.   Nice right?  &lt;/p&gt;

&lt;h2&gt;
  
  
  Static Methods
&lt;/h2&gt;

&lt;p&gt;Ok, so how do we create a method on a class that we might &lt;em&gt;not&lt;/em&gt; want a subclass to know about?  Say we want our &lt;code&gt;Nfl&lt;/code&gt; class to have a helper function that prints out the names of all teams given as arguments-- something that would NOT be relevant to any team instance.  To do that, we can use the &lt;code&gt;static&lt;/code&gt; keyword.  Let's modify our &lt;code&gt;Nfl&lt;/code&gt; constructor function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Nfl {
    constructor(){
        this.sport = "American Football";
        this.level = "Professional";
        this.salaryCap = 188200000;
    }
    raiseSalaryCap(newCap){
        this.salaryCap = newCap;
    }
    static printTeams(...teams){
        teams.forEach( team =&amp;gt; {console.log(team.name)});
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can invoke this static method by referencing the &lt;code&gt;Nfl&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//create some teams
const saints = new Team("Saints");
const cowboys = new Team("Cowboys");

//print team names to console
Nfl.printTeams(saints, cowboys)
//Logs:
  Saints
  Cowboys
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Attempting to call the &lt;code&gt;printTeams&lt;/code&gt; method on a &lt;code&gt;Team&lt;/code&gt; instance will throw an error as the static method is not able to be referenced via prototypal inheritance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;saints.printTeams(saints, cowboys)
//throws error, saints.printTeams is not a function 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In short, the &lt;code&gt;class&lt;/code&gt; keyword and features allows anyone utilize prototypal inheritance even with little understanding of what is going on under the hood.  While this may drive the more traditional javascript developer nuts, it is a powerful tool that mimics classical &lt;strong&gt;OOP&lt;/strong&gt; in simple and concise syntax.  With the &lt;code&gt;static&lt;/code&gt; keyword, you can share only the methods you want with instances and subclass instances.  I guess you can say they are finally putting the &lt;code&gt;Java&lt;/code&gt; in &lt;code&gt;Javascript&lt;/code&gt;.&lt;/p&gt;

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