<?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: Lawrence Eagles</title>
    <description>The latest articles on DEV Community by Lawrence Eagles (@lawrence_eagles).</description>
    <link>https://dev.to/lawrence_eagles</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%2F35543%2Fc9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg</url>
      <title>DEV Community: Lawrence Eagles</title>
      <link>https://dev.to/lawrence_eagles</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lawrence_eagles"/>
    <language>en</language>
    <item>
      <title>The 3 Main Reasons, You Should Learn JavaScript </title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Sat, 27 Jun 2020 22:28:31 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/why-you-should-learn-javascript-i3d</link>
      <guid>https://dev.to/lawrence_eagles/why-you-should-learn-javascript-i3d</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The JavaScript Law&lt;/li&gt;
&lt;li&gt;The JavaScript Scope&lt;/li&gt;
&lt;li&gt;The JavaScript Evolution&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. The JavaScript Law &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Any application that can be written in JavaScript, will eventually be written in JavaScript.&lt;/em&gt;&lt;br&gt;
-- &lt;strong&gt;&lt;em&gt;Jeff Atwood,&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;Author, Entrepreneur, Cofounder of StackOverflow&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;13 years ago, Jeff Atwood gave us what we now referred to as the JavaScript law.&lt;br&gt;
It is interesting how JavaScript has evolved as a language. Although, it made a humble entrance into the programming world, in so much that it was named &lt;strong&gt;&lt;em&gt;Java&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;script&lt;/em&gt; so as to coax Java developers. The language has since grown stronger and has turned its fortunes around.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 &lt;em&gt;The naming of JavaScript was pure marketing. JavaScript is to Java what Seahorse is to Horse&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Time has past and the language has evolved tremendously beyond the imagination of many. &lt;/p&gt;

&lt;p&gt;Today, JavaScript is everywhere.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 &lt;em&gt;Like insects, JavaScript is ubiquitous.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While it began (primarily) as a desktop-constrained browser-only language launched by Netscape in 1995, it has conquered virtually every internet-connected platform on Earth: phones, tablets, desktop, servers, and thousands of devices in the Internet of Things.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The language has conquered the web and has even made its way into space.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m_g3llqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/drquzbncy/image/upload/v1594470089/DEV.TO/javascript_in_space_sozpgb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m_g3llqA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/drquzbncy/image/upload/v1594470089/DEV.TO/javascript_in_space_sozpgb.jpg" alt="alt JavaScript in space"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  2. The JavaScript Scope &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;JavaScript is an extremely versatile language. Once you’ve mastered it, the possibilities are endless, hence there are numerous job opportunities for JavaScript developers.&lt;/p&gt;

&lt;p&gt;JavaScript developers often have a wide area of specialization. They can build front-end, back-end, mobile developers, and even desktop applications. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 &lt;em&gt;The language has even applications in machine learning, blockchain, and IoT its scope is tremendous.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below, are some areas of application of JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Frontend web development: &lt;br&gt;
JavaScript is the king of Frontend web development. The undisputed champion. Although, Dart and Web Assembly are hovering around it still rule supreme. &lt;br&gt;
In the frontend, JavaScript via its libraries and framework viz: &lt;strong&gt;Angular, React.js, Vue.js&lt;/strong&gt;, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Backend web development: &lt;br&gt;
JavaScript somehow found its way out of the browser via Node.js. This is an interesting breakthrough considering JavaScript lived a big part of its life enclosed in the browser.&lt;br&gt;
With Node.js, developers are able to bring the awesomeness of the JavaScript language to the backend and this marked a new era in web development. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using JavaScript for both frontend and backend allows developers to create &lt;a href="https://en.wikipedia.org/wiki/Isomorphic_JavaScript"&gt;isomorphic applications&lt;/a&gt; and gives a better developer experience among other advantages.&lt;br&gt;
little wonder several big tech companies such as &lt;strong&gt;Walmart, Paypal, Netflix, Linkedin, Groupon&lt;/strong&gt; (to name a few), were quick to adopt Node.js&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mobile Development: This is an area you would expect to see the likes of Java, Kotlin, and Swift shine, not JavaScript. However, JavaScript again finds its way here and shines; I dare say brighter.
With the likes of &lt;a href="https://reactnative.dev/"&gt;React Native&lt;/a&gt;, we are able to build cross-platform &lt;a href="https://ionicframework.com/resources/articles/what-is-hybrid-app-development"&gt;hybrid mobile applications&lt;/a&gt; using just HTML, CSS, and JavaScript.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many company prefer this because it eliminates the need for building two separate codes for iOS and Android, it is cost-effective and easier to build and maintain. All this without actually trading off quality.&lt;br&gt;
Some &lt;a href="https://reactnative.dev/showcase"&gt;companies that power their mobile application with React Native&lt;/a&gt; are:&lt;br&gt;
&lt;strong&gt;Slack, Bloomberg, Airbnb, Instagram, Skype.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Desktop Application: This is the most unlikely place you would expect to see a language that was meant to live in the browser yet it is a shining star in this world.
With &lt;a href="https://www.electronjs.org/"&gt;Electron.js&lt;/a&gt; desktop applications can now be built using JavaScript. 
As in other areas, JavaScript simplifies building a desktop application to a matter of writing just HTML, CSS, and &lt;em&gt;JavaScript&lt;/em&gt;. 
Again this is very cost-effective and easier to build and maintain. Hence, it is preferred by many companies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notable &lt;a href="https://www.electronjs.org/apps"&gt;desktop applications built with Electron.js&lt;/a&gt; are:&lt;br&gt;
&lt;strong&gt;Slack, Whatsapp, Microsoft Teams, Invision, Figma, Visual Studio Code, Twitch, Atom Text Editor&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 &lt;em&gt;Many languages have advertised themselves with quotes such as: "code once, run anywhere" or "learn once write anywhere", but JavaScript is the language that these quotes best describe. It is aptly called the language of the web&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  3. The JavaScript Evolution &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;With every new version of JavaScript, we see the language evolving for the better. New amazing features are being added and we get syntactic sugars where necessary (e.g &lt;a href="https://dev.to/lawrence_eagles/an-easy-guide-to-understanding-classes-in-javascript-3bcm"&gt;JavaScript Classes&lt;/a&gt;). We have gone from writing &lt;strong&gt;callbacks&lt;/strong&gt; to &lt;strong&gt;promises&lt;/strong&gt; and now &lt;strong&gt;async-await&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These improvements redound into all areas where JavaScript is applied. &lt;/p&gt;

&lt;p&gt;As a result of these, the language has solidified its position as the language of the web. Today we use popular software architecture designs such as &lt;a href="https://jamstack.org/"&gt;&lt;strong&gt;JAM Stack&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Single-page_application"&gt;&lt;strong&gt;Single Page Application (SPA)&lt;/strong&gt;&lt;/a&gt;, but in the heart of both, we see JavaScript sitting as King. &lt;/p&gt;

&lt;p&gt;In addition, as the language gets better it keeps expanding its scope of application. This means more job opportunities for JavaScript developers.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Closing Thoughts &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;There several other good reasons to learn JavaScript, the 3 above are for me the main ones. It is also worth mentioning that the language is very simple and beginner-friendly. And being very popular, there are tons of free learning material online. &lt;/p&gt;

&lt;p&gt;I do hope I got you interested in the language or at least; I dare say made you appreciate your skill as a JavaScript developer more.  &lt;/p&gt;

&lt;p&gt;As always, if there are other notable mentions that I missed, or you have an opinion or correction to make, you are more than welcome to share it on the comment below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>showdev</category>
    </item>
    <item>
      <title>An Easy Guide To Understanding Classes In JavaScript</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Wed, 17 Jun 2020 21:10:50 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/an-easy-guide-to-understanding-classes-in-javascript-3bcm</link>
      <guid>https://dev.to/lawrence_eagles/an-easy-guide-to-understanding-classes-in-javascript-3bcm</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;An Introduction To Classes&lt;/li&gt;
&lt;li&gt;Classes In Detail&lt;/li&gt;
&lt;li&gt;Subclasses In Detail&lt;/li&gt;
&lt;li&gt;Classes In Action&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. An Introduction To Classes In JavaScript &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;In the previous article in this series we looked at function constructors as one of the recommended ways to set an object's prototype in JavaScript, and we noted that although, there are other ways of doing this, concocting them into one discursive article would be a disincentive to readers. &lt;br&gt;
You can get a refresher from this article here:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/lawrence_eagles" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ffxfonux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--yw4zAhLy--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/35543/c9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg" alt="lawrence_eagles"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/lawrence_eagles/an-easy-guide-to-understanding-constructors-in-javascript-2mf6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;An Easy Guide To Understanding Constructors In JavaScript&lt;/h2&gt;
      &lt;h3&gt;Lawrence Eagles ・ May 9 '20 ・ 10 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In this article we will pick up where we left off by looking at Classes in JavaScript. &lt;br&gt;
This is another recommended method of setting an object's prototype in JavaScript and it makes for a very interesting discourse in this OOP (Object Oriented Programming) in JavaScript series.&lt;/p&gt;

&lt;p&gt;Below are some gleanings from a previous post in this series:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;The classical inheritance or class bases inheritance involves writing classes; which are like blueprints of objects to be created. Classes can inherit from classes and even create subclasses.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;✨ &lt;em&gt;In the prototypal inheritance an object inherits its properties and methods from its prototype object. And sometimes, a search down the prototype chain is necessary for an object to access some properties and methods.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can get a refresher from this article here:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/lawrence_eagles" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ffxfonux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--yw4zAhLy--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/35543/c9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg" alt="lawrence_eagles"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/lawrence_eagles/understanding-prototypal-inheritance-in-javascript-4f31" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Understanding Prototypal Inheritance In JavaScript&lt;/h2&gt;
      &lt;h3&gt;Lawrence Eagles ・ Apr 23 '20 ・ 13 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#showdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;🎉 So what is interesting is that JavaScript introduced classes in ECMAScript 2015 but is primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Although, one can say in a nutshell; classes in JavaScript are just new syntax for the same old prototypal inhertance. They however, have significant improvements over function constructors and are a very powerful addition to the JavaScript programming language.&lt;/p&gt;

&lt;p&gt;Lets take a deeper look at classes in JavaScript in the next section.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Classes In Detail &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;In JavaScript classes are functions. And since all functions are objects in JavaScript, classes are also objects.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Classes are Functions
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;Kindly run and consider the code below. Be sure to expand the console.log output for a more detailed result&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
class Person {
  constructor(name, gender) {
    this.name = name;
    this.logo = gender;
  }
}

console.log("Person class", Person) 
console.log("Person prototype", Person.__proto__) 

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


&lt;p&gt;From the results of the code above we can see the follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Person class is a function with a &lt;code&gt;name&lt;/code&gt; property in this case Person.&lt;/li&gt;
&lt;li&gt;It has a &lt;code&gt;prototype&lt;/code&gt; property which points to the &lt;code&gt;Person {}&lt;/code&gt; object. Take note this is not the prototype of the Person class. But this is the prototype of all the instances of the Person class.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The prototype property of a class is not the prototype of the class. It is the prototype of every object created from that class&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The Person class has a &lt;code&gt;__proto__&lt;/code&gt; property which points to the &lt;em&gt;function prototype&lt;/em&gt;. This is the prototype of the Person class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets elaborate on the last two points above with some code samples.&lt;br&gt;
Kindly run and consider the codes below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
class Person {
  constructor(name, gender) {
    this.name = name;
    this.logo = gender;
  }
}

console.log("Person class", Person.prototype) 
console.log("Person prototype", Person.__proto__) 

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


&lt;p&gt;From the results of the code above it should be clear that &lt;code&gt;Person.prototype&lt;/code&gt; is different from Person &lt;code&gt;__proto__&lt;/code&gt;. The former is the prototype of all instance of the Person class and the latter is the prototype of the Person class itself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 &lt;em&gt;If this feels a little fast-paced for you, please know that I am building upon what I have already covered in the previous two articles. &lt;br&gt;
However, if you have not been following or for some reason you are finding this section a bit puzzling, kindly refer to my previeous articles in this series. I have already shared their links in section 1.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Like regular functions in JavaScript you can have a class declaration and a class expression.&lt;br&gt;
&lt;em&gt;Kindly examine the code below&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// A class declaration&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Person_Dec&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gender&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;// An Unnamed class expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Person_Exp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gender&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;// Named class expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Person_Exp2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Person_Exp&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;gender&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;All three methods above are valid ways to implement a class in JavaScript.&lt;/p&gt;
&lt;h4&gt;
  
  
  Classes are Objects
&lt;/h4&gt;

&lt;p&gt;In other programming languages like C#, C++ and Java, that uses classical (class-based) inheritance the class is a template or a blueprint that details the structure of an object. Objects are built from them but they are not objects.&lt;/p&gt;

&lt;p&gt;In JavaScript however, the Class is a special function and all functions in JavaScript are objects hence the JavaScript class is an object. JavaScript still uses prototypal inhertance. Classes however, provide a new and improved way to set the objects prototype. We will look at this in a moment. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Kindly consider the results of the code below&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
class Person {
  constructor(name, job) {
    this.name = name;
    this.job = job;
  }

// Methods
  getName() {
    return "Hello My Name is " + this.name ;
  }

//static
  static getPersonGender () {
    return "male"
  }
}

const developer = new Person("Lawrence Eagles", "developer"); 

console.log("Developer's Name", developer.getName());  

console.log("Developer's Prototype", developer.proto); 

console.log("Person's gender", Person.getPersonGender()); 

// console.log("Developer's gender", developer.getPersonGender()); 

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



&lt;p&gt;Above is the declaration of a Person Class with a &lt;code&gt;normal method&lt;/code&gt; and a &lt;br&gt;
&lt;code&gt;static method&lt;/code&gt;.&lt;br&gt;
From the results of running the code, we can see the following.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The instance of the Person class (the developer object) inherites the properties and methods of the Person class. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus we were able to call the &lt;code&gt;getName&lt;/code&gt; method on the developer object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Developer's Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;developer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;  &lt;span class="c1"&gt;// calls the getName function inherited from the Person Class&lt;/span&gt;
&lt;span class="c1"&gt;// returns "Lawrence Eagles"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Using the &lt;code&gt;static keyword&lt;/code&gt; with a method, creates a static method for the class.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//static&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;getPersonGender&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;male&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;These methods are not inherited by the instances of a class hence they cannot be called on the instances of a class itself.  &lt;/p&gt;

&lt;p&gt;You can prove this by uncommenting this line from our code sample:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//console.log("Developer's gender", developer.getPersonGender()); &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Notice however, that we where able to successfully call the &lt;code&gt;getPersonGender&lt;/code&gt; method on the Person class itself.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Person's gender&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getPersonGender&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// calls the getPersonGender static method of the Person class &lt;/span&gt;
&lt;span class="c1"&gt;// This call is successful because the static method is available on the Person class&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;Static methods aren't called on instances of the class. Instead, they're called on the class itself. &lt;br&gt;
They are often utility functions, such as functions to create or clone objects.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The prototype of the instance of the Person class (the developer object) is the &lt;code&gt;Person {}&lt;/code&gt; object. We have already noted that the Person object have a &lt;code&gt;prototype&lt;/code&gt; property which points to the &lt;code&gt;Person {}&lt;/code&gt; object and serves as the prototype of all instances of the Person class. Here we get an elboration on this with codes samples.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Developer's Prototype&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;developer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// gets the prototype of the developer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  3. Subclasses In Detail &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Besides creating instance of a class we can also &lt;code&gt;extend&lt;/code&gt; a class (create subclasses from an existing class). This is kind of advance and it is a very powerful feature of the JavaScript class.&lt;/p&gt;

&lt;p&gt;JavaScript introduced a new keyword for this purpose called &lt;code&gt;extends&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The extends keyword is used in class declarations or class expressions to create a class that is a child of another class.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Kindly run and consider the code below:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
class Person {
  constructor(name) {
    this.name = name;
  }

sayName() {
    console.log("My name is " + this.name);
  }
}

class Developer extends Person {
  constructor(name) {
    super(name);
    this.name = name;
  }

getBio() {
    super.sayName();
    console.log("I am a developer");
  }
}

let ReactGuy = new Developer("Lawrence Eagles");
ReactGuy.getBio(); 

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



&lt;p&gt;The code above shows a Person class and a Developer subclass created from the person class. Below are some comments to elaborate on the code above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In other to create a subclass we extend the parent class using the keyword &lt;code&gt;extends&lt;/code&gt; Hence: &lt;code&gt;class Developer extends Person&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;super keyword&lt;/code&gt; is used to access and call functions on an object's parent. Notice we used &lt;code&gt;super.sayName()&lt;/code&gt; in the &lt;code&gt;getBio&lt;/code&gt; method of the Developer subclass to call the sayName method of the Person Parent class. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calling &lt;code&gt;super()&lt;/code&gt; method inside a contractor would call the constructor of the parent class.&lt;br&gt;
In our example calling the &lt;code&gt;super method&lt;/code&gt; inside the Developer's subclass constructor would call the Person class constructor. Now any arguments passed to the &lt;code&gt;super()&lt;/code&gt; would also be passed to the parent class constructor. I will elaborate with a small contrived perspicuous example &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Kindly run and consider the code below&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
class ObjectFactory {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}

class Square extends ObjectFactory {
    constructor(length) {
        super(length, length);
    }
}

const Square1 = new Square() 
const Square2 = new Square(4) 

console.log("Square1", Square1)
console.log("Square2", Square2)

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


&lt;p&gt;Above we created a &lt;code&gt;Square&lt;/code&gt; subclass from the &lt;code&gt;ObjectFactory&lt;/code&gt; class. Notice that the &lt;code&gt;ObjectFactory&lt;/code&gt; constructor expects two parameters viz &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;width&lt;/code&gt;. Values for these are provided when we call &lt;code&gt;super()&lt;/code&gt; in the &lt;code&gt;Square&lt;/code&gt; subclass.&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;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;when the &lt;code&gt;Square1&lt;/code&gt; and &lt;code&gt;Square2&lt;/code&gt; instances where created the argument provided to the length parameter was passed down to &lt;code&gt;super()&lt;/code&gt; which calls the parent (ObjectFactory) constructor with these arguments.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Square1&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;Square&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;Square2&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;Square&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;width&lt;/code&gt; property of the &lt;code&gt;Square1&lt;/code&gt; instance is &lt;code&gt;undefined&lt;/code&gt; however, because no argument was provided when it was created by invoking the &lt;code&gt;Square&lt;/code&gt; subclass.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Square1&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;Square&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// no argument provided.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;super()&lt;/code&gt; method must be called in the constructor in a class before the &lt;code&gt;this keyword&lt;/code&gt; is made available else we get a reference error. Again I would elaborate with an example.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note running the code below would throw an error this is delebrate:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
class ObjectFactory {
    constructor(height, width) {
        this.name = "Object Factory"
        this.height = height;
        this.width = width;
    }
}

class Square extends ObjectFactory {
    constructor(length) {
        this.name = "Square Object"
        super(length, length);
    }
}

const Square1 = new Square() 

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



&lt;p&gt;To get the code to work properly kindly move the &lt;code&gt;super(length, length)&lt;/code&gt; above &lt;code&gt;this.name = "Square Object"&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unlike instances of a class, static methods can be called on a subclass.
Let's elaborate with some code examples.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Kindly run and consider the code below:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
class Person {
  constructor(name) {
    this.name = name;
  }

static logGreeting() {
    console.log("Good day " + this.name);
  }

sayName() {
    console.log("My name is " + this.name);
  }
}

class Developer extends Person {
  constructor(name) {
    super(name);
    this.name = name;
  }

getBio() {
    super.sayName();
    console.log("I am a developer");
  }
}

const ReactGuy = new Person("Lawrence Eagles")

console.log("Developer Prototype", Object.getPrototypeOf(Developer))

console.log("greeting from developer", Developer.logGreeting())

//console.log("greeting from developer", ReactGuy.logGreeting())

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


&lt;p&gt;In our code example above we noticed that the &lt;br&gt;
Person Class has a &lt;code&gt;static method&lt;/code&gt; viz:&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;static&lt;/span&gt; &lt;span class="nx"&gt;logGreeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Good day &lt;/span&gt;&lt;span class="dl"&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;name&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;This method was successfully called by the developer subclass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// calls the logGreeting static method of the Person Class in the developer subclass&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;greeting from developer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Developer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logGreeting&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;Static methods&lt;/code&gt; however cannot be called on instances of a class. You can test this by on commenting this line and rerun the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//console.log("greeting from developer", ReactGuy.logGreeting())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a subclass is created from a class, the class becomes its prototype.&lt;/p&gt;

&lt;p&gt;We prove this from our code sample above with this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Developer Prototype&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&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;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Developer&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The &lt;code&gt;Object.getPrototypeOf()&lt;/code&gt; method returns the prototype of any object passed to it as an argument.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From the result of our code above we can see that the prototype of the developer subclass is the Person parent class.&lt;/p&gt;

&lt;h4&gt;
  
  
  Classes In Action &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;I belive by now we should be pretty comfortable with the JavaScript class. &lt;/p&gt;

&lt;p&gt;Classes in JavaScript are more than just a new way to create objects and set their prototype. As we have seen above they come with a number of interesting features.  &lt;/p&gt;

&lt;p&gt;In addition, they would throw an error when they are called without the &lt;br&gt;
&lt;code&gt;new operator&lt;/code&gt; this helps developer to avoid nasty bugs common when using &lt;code&gt;function constructors&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Classes power several popular JavaScript frameworks and libraries like React.js and Angular. Their rich feature set paves the way for several advanced design patterns in different JavaScript libraries and frameworks.&lt;/p&gt;

&lt;p&gt;Let's see some real life code examples.&lt;br&gt;
&lt;em&gt;Kindly examine the code below&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ErrorBoundary&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;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;getDerivedStateFromError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Update state so the next render will show the fallback UI.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&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="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;if&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;hasError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// You can render any custom fallback UI&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt; &lt;span class="nx"&gt;went&lt;/span&gt; &lt;span class="nx"&gt;wrong&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&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;Above is the boilerplate code for an &lt;code&gt;ErrorBoundary component&lt;/code&gt; in React.&lt;br&gt;
Although, this is not an article on React; I only want us to see classes in action in some of the most advanced JavaScript library.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;React 16 introduces a new concept of an “error boundary”, in other to catch error anywhere in your application and render a fallback UI instead of allowing the whole applicaiton to crash because of a JavaScript error. &lt;br&gt;
It is an advance design pattern in React.js&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Notice the &lt;code&gt;ErrorBoundary component&lt;/code&gt; is implemented with a JavaScript class by extending the React Component class.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ErrorBoundary&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Notice how &lt;code&gt;super(props)&lt;/code&gt; was called before &lt;code&gt;this keyword&lt;/code&gt; was used in the constructor.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;hasError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Notice the &lt;code&gt;static method getDerivedStateFromError&lt;/code&gt; of the ErrorBoundary subclass. It is used to render a fallback UI after an error has been thrown
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;getDerivedStateFromError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Update state so the next render will show the fallback UI.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hasError&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are not a React.js Developer you may not fully comprehend what is going on here, but that is understandable. My aim here is to show you some of the real world implementation of classes in regards to our discourse. &lt;/p&gt;

&lt;h4&gt;
  
  
  5. Closing Toughts: &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;It has really been a long article and if you got here you are really appreciated and I am more than elated.&lt;/p&gt;

&lt;p&gt;I really hope that at this point, you can see the benefits of our long discussion and at least got a thing or two from this article. If so, I would be honoured if you would share it with you friends and colleagues,  while also looking forward to hearing your opinions, comments, questions, or requests (in case anything is not clear) at the comments section below. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>showdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Hello everyone, I apologize for being totally off for a while now.</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Wed, 03 Jun 2020 11:26:45 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/hello-everyone-i-apologize-for-being-totally-off-for-a-while-now-34an</link>
      <guid>https://dev.to/lawrence_eagles/hello-everyone-i-apologize-for-being-totally-off-for-a-while-now-34an</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;However, I am back and I would like to announce my upcoming series viz: Mastering React.js Inside out.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In my new series, I would be explaining React.js to the last detail, in hope of helping you to master the awesome library.&lt;/em&gt; 💖&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stay connected&lt;/em&gt; ✨&lt;/p&gt;

</description>
      <category>tutorials</category>
      <category>showdev</category>
      <category>webdev</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>An Easy Guide To Understanding Constructors In JavaScript</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Sat, 09 May 2020 22:14:10 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/an-easy-guide-to-understanding-constructors-in-javascript-2mf6</link>
      <guid>https://dev.to/lawrence_eagles/an-easy-guide-to-understanding-constructors-in-javascript-2mf6</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;An Introduction To Constructors&lt;/li&gt;
&lt;li&gt;Functions, Constructors And The &lt;em&gt;new&lt;/em&gt; Operator&lt;/li&gt;
&lt;li&gt;Constructors And Prototypal Inheritance&lt;/li&gt;
&lt;li&gt;JavaScript's Built-In Constructors&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. An Introduction To Constructors &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;In the previous article in this series, we looked at prototypal inheritance in JavaScript and talked about important object-oriented (OOP) JavaScript concepts like the prototype, the prototype chain, inheritance, and more. We also looked at how to set an object's prototype using its &lt;em&gt;__proto__&lt;/em&gt; property (&lt;em&gt;we noted that this is not the recommended way.&lt;/em&gt;) and dealt with the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable in detail. &lt;br&gt;
You can read through this article below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/lawrence_eagles" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35543%2Fc9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg" alt="lawrence_eagles"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/lawrence_eagles/understanding-prototypal-inheritance-in-javascript-4f31" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Understanding Prototypal Inheritance In JavaScript&lt;/h2&gt;
      &lt;h3&gt;Lawrence Eagles ・ Apr 23 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#showdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;In this article, we would pick up where we left off by looking at the recommended ways to set an object's prototype in JavaScript. &lt;br&gt;
While there are several ways to do this, our focus here is on function constructors. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;We will deal with the other recommended methods in the upcoming articles in this series. Talking in detail about all of them; in a single article would make it very long and grueling to read!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h5&gt;
  
  
  Constructors
&lt;/h5&gt;

&lt;p&gt;Constructors in JavaScript are special functions that are used to construct objects. This topic may appear difficult and intimidating but it is actually very simple. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The key to understanding constructors is to know that they are actually normal JavaScript functions. What makes them special is that they are always invoked along with a very powerful operator in JavaScript called the new operator.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Kindly run the code below and consider its result.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function Person () {
     this.firstname = "Lawrence"
     this.lastname = "Eagles"
     this.occupation = "Software Developer"
     this.gender = "male"
}

const Lawrence = new Person(); 
console.log(Lawrence);

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


&lt;p&gt;Our small contrieved example above creates a new object and stores a reference to it in the &lt;em&gt;Lawrence&lt;/em&gt; variable. This object has all the properties specified in the Person constructor. &lt;br&gt;
The &lt;em&gt;Person&lt;/em&gt; function itself is a normal JavaScript function; what gives it the power of a constructor (&lt;em&gt;the power to construct objects&lt;/em&gt;) is this line of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Lawrence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;✨ The &lt;em&gt;new&lt;/em&gt; operator modifies the behaviour of the function it operates on. Using this along with some JavaScript design patterns, we are able to create powerful constructors. We will elaborate on this in the next section.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  2. Functions, Constructors And The &lt;em&gt;new&lt;/em&gt; Operator &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;In section 1 we learned that when the Person constructor, (or any other constructor) is invoked without the &lt;em&gt;new&lt;/em&gt; operator it is invoked as a regular JavaScript function. In this section, we will elaborate on this with code examples.&lt;br&gt;
&lt;em&gt;Kindly consider the code below.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&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;firstname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence&lt;/span&gt;&lt;span class="dl"&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;lastname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eagles&lt;/span&gt;&lt;span class="dl"&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;occupation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Software Developer&lt;/span&gt;&lt;span class="dl"&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;gender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;male&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;Above is the declaration of the &lt;em&gt;Person&lt;/em&gt; function. We can notice two things from it viz:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It sets some properties e.g firstname, lastname, occupation, and gender to the object the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable binds (or is pointing) to. In this case the global object.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;As mentioned in the previous article in this series, one rule of the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;variable is that when it is used inside a function it points to the global object but when it is used inside a method (a function in an object) it would point to that object&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If this is not very clear to you, feel free to visit my previous article on OOP JavaScript. I have already provided a link to it in section 1. &lt;br&gt;
However, here is a quick recap.&lt;br&gt;
&lt;em&gt;Kindly run the code below and consider its result&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const devName = "Lawrence Eagles"
function tellDevName () {
    console.log("result", this.devName)
}
tellDevName();
// returns "Lawrence Eagles"

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



&lt;p&gt;The above example shows that the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable inside a function is pointing to the global object. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Another thing that should be pretty obvious about the &lt;em&gt;Person&lt;/em&gt; function is that it does not have a return statement hence when invoked it would return undefined. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The JavaScript engine would return undefined from any function that does not return a value. This behaviour is leveraged in creating constructors as we are able to modify what that function returns using the new operator. Hence it is a common pattern in JavaScript, that constructors do not have a return statement&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  The New Operator
&lt;/h5&gt;

&lt;p&gt;This is a very powerful JavaScript operator that has the ability to modify certain behaviours of a function. &lt;br&gt;
The &lt;em&gt;new&lt;/em&gt; operator can be very confusing and somewhat intimidating at first. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;The key to understanding it is to always see it as another regular JavaScript operator. Hence, a good understanding of operators in JavaScript is necessary to grasp the power of this operator&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h5&gt;
  
  
  Operators
&lt;/h5&gt;

&lt;p&gt;Operators are special JavaScript functions that are syntactically different from regular functions. They are not like a regular JavaScript functions objects hence passing them to &lt;em&gt;console.dir()&lt;/em&gt; would &lt;em&gt;throw&lt;/em&gt; an error. You can see some code examples below.&lt;br&gt;
&lt;em&gt;Kindly run the codes below and consider the results:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function tellDevName () {
    console.log("result", this.devName)
}
console.dir("function properties", tellDevName)
console.dir("function properties", Date)
// if you uncomment the lines below and run the codes in you get an error.
// console.dir("function properties", +)
// console.dir("function properties", new)

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


&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The console.dir() displays an interactive list of the properties of the specified JavaScript object&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1589055569%2FDEV.TO%2FScreenshot_2020-05-09_at_21.19.15_h8pirh.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1589055569%2FDEV.TO%2FScreenshot_2020-05-09_at_21.19.15_h8pirh.png" alt="codes result from runkit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see all the properties of the &lt;em&gt;tellDevName&lt;/em&gt; function and the &lt;em&gt;Date&lt;/em&gt; constructor when you run the code but if you uncomment the lines where I passed an operator as a parameter, and try to run the code, runkit would &lt;em&gt;throw&lt;/em&gt; an error, this tells us that they are not regular function objects.&lt;/p&gt;

&lt;p&gt;Operators much like regular functions take parameters (which are called operands) but unlike regular functions, they give us a convenient syntax which can be in the form of any of the three notations below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Infix Notation: In this notation, operators are placed between their operands. 
&lt;em&gt;Kindly consider the code below:&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;// returns 4&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;// returns 9&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;// returns 0&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;// returns 1&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;// returns 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In our examples above each operator sits between two parameters (operands) and returns a value. &lt;a href="https://en.wikipedia.org/wiki/Infix_notation" rel="noopener noreferrer"&gt;Learn more about the infix notation here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Postfix Notation: In this Notation, the operators follow their operands.
&lt;em&gt;Kindly consider the codes below:&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mixNumbers&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&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="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;mixNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mixNumbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
       &lt;span class="nx"&gt;evenNumbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mixNumbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;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;even numbers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;evenNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Above is a small example that finds the even number from a list of numbers. But what concerns us from this example is the increment operator. &lt;br&gt;
There is also the decrement operator. &lt;a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation" rel="noopener noreferrer"&gt;Learn more about the postfix notation&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Kindly consider the code below:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="nx"&gt;operator&lt;/span&gt;
&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="nx"&gt;decrement&lt;/span&gt; &lt;span class="nx"&gt;operator&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Prefix Notation: In this notation, the operator precedes its operands. &lt;a href="https://en.wikipedia.org/wiki/Polish_notation" rel="noopener noreferrer"&gt;Learn more about the prefix notation&lt;/a&gt;
&lt;em&gt;Kindly consider the codes below:&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;               &lt;span class="c1"&gt;// logical NOT (!) returns false&lt;/span&gt;
&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;              &lt;span class="c1"&gt;// logical NOT (!) returns true&lt;/span&gt;
&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;                 &lt;span class="c1"&gt;// prefix increment             &lt;/span&gt;
&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;                 &lt;span class="c1"&gt;// prefix decrement&lt;/span&gt;
&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;// returns the newly constructed object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From our examples above we can see that the new operator uses the prefix notation it takes a function (constructor) invocation and returns a newly constructed object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎉 &lt;em&gt;I do hope that our succinct discourse on operators, would make you understand the new operator better and hence further your understanding of function constructors&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With our understanding of operators, we can now clearly see that the &lt;em&gt;new&lt;/em&gt; operator actually takes a function (constructor) invocation as its parameter(operand) it then performs some operations on it and returns a value. &lt;br&gt;
Below are the operations of the &lt;em&gt;new&lt;/em&gt; operator on a function constructor.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates an empty object and binds (points) the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable to the newly created object.&lt;/li&gt;
&lt;li&gt;Returns the object the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable binds to (the newly created object) if the function doesn't return its own object &lt;em&gt;(this is why constructors should not have a return statement)&lt;/em&gt;.
&lt;em&gt;Kindly run the codes below and consider the results:&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
// regular function
function person () {}
const regularDeveloper = person()
console.log("regular function result", regularDeveloper)

// constructor 
function Person () {
    console.log("this binds to", this)
}
const constructorDeveloper = new Person()
console.log("Person constructor result", constructorDeveloper)

function BadPerson () {
    console.log("this binds to", this)
    return {name: "Jack", age: "70"}
}
const BadJack = new BadPerson()
console.log("BadPerson constructor result", BadJack)

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



&lt;p&gt;From the code example above I have deliberately given 2 of the 3 functions the same name but since JavaScript is case sensitive they are two different functions. Notice that the first letter of the constructor's name is capitalized while the regular function name is all lowercase. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;We use this pattern in JavaScript to differentiate constructors from regular functions. The first letter of a constructor's name is always capitalized. This would also serve as a reminder for you to use the new operator with every constructor. In our code example above, Person is the constructor and person is the regular function.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can see from the result of the code above that the regular function returns &lt;em&gt;undefined&lt;/em&gt; as expected but the constructor returns a new object created by the &lt;em&gt;new&lt;/em&gt; operator which also binds the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable in that constructor to this object. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💥 &lt;em&gt;Also notice the BadPerson constructor that returns its own object fails to return the object created from the new operator. This is a pitiful we must avoid. Again, as a rule, a constructor should not have a return statement❗&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1589045590%2FDEV.TO%2FScreenshot_2020-05-09_at_18.32.34_oqkosj.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1589045590%2FDEV.TO%2FScreenshot_2020-05-09_at_18.32.34_oqkosj.png" alt="Alt JavaScript constructor's result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  JavaScript Design Patterns For Creating Constructors
&lt;/h5&gt;

&lt;p&gt;With our knowledge of constructors and the &lt;em&gt;new&lt;/em&gt; operator, we can easily add properties to the newly constructed object . Here is a common JavaScript pattern for this.&lt;br&gt;
&lt;em&gt;Kindly consider the code below&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&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;firstname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence&lt;/span&gt;&lt;span class="dl"&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;lastname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eagles&lt;/span&gt;&lt;span class="dl"&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;occupation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Software Developer&lt;/span&gt;&lt;span class="dl"&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;gender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;male&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The only limitation here is that any object created by this constructor is always going to have these properties. In other to make the object properties dynamic, we can pass them as parameters to the constructor (since constructors are regular functions in the first place).&lt;br&gt;
&lt;em&gt;Kindly run the codes below and consider its result:&lt;/em&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function Person (firstname, lastname, occupation, gender) {
     this.firstname = firstname
     this.lastname = lastname
     this.occupation = occupation
     this.gender = gender
}
const Developer = new Person("Lawrence", "Eagles", "Software Developer", "Male")
const Doctor = new Person("Ben", "Carson", "Neurosurgeon", "Male")
const Scientist = new Person("Albert", "Einstein", "Scientist", "Male")

console.log("developer", Developer)
console.log("doctor", Doctor)
console.log("scientist", Scientist)

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



&lt;p&gt;From the results of running the code above, we can see that the arguments passed to each constructor, when invoked with the &lt;em&gt;new&lt;/em&gt; operator are used to set up the properties of the newly constructed objects.&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new" rel="noopener noreferrer"&gt;You can read more about the &lt;em&gt;new&lt;/em&gt; operator at MDN&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly the &lt;em&gt;new&lt;/em&gt; operator links (sets) the prototype of the newly created object to another object. 
In our introduction, we said we were going to talk about the recommended ways to set an object's prototype and our focus was on function constructors. This point brings our long discourse back to the subject matter. Let's talk more about it in the next section.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  3. Constructors And Prototypal Inheritance &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;In JavaScript, every function has a property called the &lt;strong&gt;&lt;em&gt;prototype&lt;/em&gt;&lt;/strong&gt;. This sits as an empty object in the function and remains dormant throughout the life of that function. It would only become active and quite useful if that function is used as a constructor.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The prototype property of a function (which is an object in JavaScript) is not the prototype of that function object but it acts as the prototype of any object constructed with that function when it is used as a constructor. The naming is a little confusing but we love our JavaScript 😉&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Kindly run the code below and consider its result:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function Person (firstname, lastname, occupation, gender) {
     this.firstname = firstname
     this.lastname = lastname
     this.occupation = occupation
     this.gender = gender
}

// lets add some properties to the prototype property of the Person constructor.
Person.prototype.getPersonBio = function () {
          console.log("Hello my name is " + this.lastname + " " + this.firstname + " I am a " + this.occupation )
    }
const Developer = new Person("Lawrence", "Eagles", "Software Developer", "Male")
const Doctor = new Person("Ben", "Carson", "Neurosurgeon", "Male")
const Scientist = new Person("Albert", "Einstein", "Scientist", "Male")

console.log("Developer's bio:", Developer.getPersonBio())
console.log("Doctor's bio:", Doctor.getPersonBio())
console.log("Scientist's bio", Scientist.getPersonBio())

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

&lt;p&gt;From the results of the code above we can see that all the objects constructed with the Person constructor have access to the &lt;em&gt;getPersonbio&lt;/em&gt; method which sits in the prototype property of the Person constructor. As we have noted above this property becomes the prototype of each object. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;The details of how the search is made down the prototype chain in other for all the constructed object to access the getPersonBio method has already been discussed in the previous article. I would kindly suggest you take a look at it if this section is not very clear to you.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  4. JavaScript's Built-In Constructors &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;JavaScript comes with some built-in constructors. If you are a JavaScript developer, there is a high probability that you have used some of them.&lt;br&gt;
&lt;em&gt;Kindly run the code below and consider its result:&lt;/em&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const NumObject = new Number("20")
const StringObject = new String("Hello World")
const Today = new Date()

console.log(NumObject)
console.log(StringObject)
console.log(Today)

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


&lt;p&gt;From running the codes above we can see that each returns an object because every constructor in JavaScript returns an object. &lt;br&gt;
You can learn more about each of these built-in constructors from the links below:&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number" rel="noopener noreferrer"&gt;Number Constructor&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String" rel="noopener noreferrer"&gt;String Constructor&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date" rel="noopener noreferrer"&gt;Date Constructor&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;These are just a selected few you can get a more robots list &lt;a href="http://es5.github.io/#x15.1.4" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  5. Closing Thoughts &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;I do hope you followed through to this point. If you did you are really appreciated. It has been a long discussion, and I hope you got a thing or two. If so, I would now be looking forward to hearing your opinions, comments, questions, or requests (in case anything is not clear) in the comments section below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>AN EASY GUIDE TO UNDERSTANDING CLOSURES IN JAVASCRIPT</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Fri, 01 May 2020 14:56:43 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/an-easy-guide-to-understanding-closures-in-javascript-4n5m</link>
      <guid>https://dev.to/lawrence_eagles/an-easy-guide-to-understanding-closures-in-javascript-4n5m</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;An Introduction To Closure&lt;/li&gt;
&lt;li&gt;The Fundamental Tenets&lt;/li&gt;
&lt;li&gt;Scope And Scope Chain&lt;/li&gt;
&lt;li&gt;A Second Look At Closure&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. An Introduction To Closure &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Closures are an extremely powerful feature of the JavaScript programming language.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;A closure is a combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.&lt;/em&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The superb definition above utterly explains a closure. It is actually a feature of the JavaScript language, it is not something we code; it just happens due to the way the JavaScript language works. As a result of this, a function is able to access the variables of its parent (outer) function even after that function has returned.&lt;/p&gt;

&lt;p&gt;Let's throw more light on the above definitions with an example below:&lt;br&gt;
&lt;em&gt;Kindly run the code below and consider its result.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function getBio(shortDevBio) {
    return function(devName) {
        console.log("Hi, my name is " + devName + " " + shortDevBio);
   }
}

const talkAboutDev = getBio("I am a developer, writer and instructor")
talkAboutDev("Lawrence Eagles") 

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


&lt;p&gt;Our small contrived example above features a function &lt;em&gt;getBio&lt;/em&gt; that takes a succinct developer bio and returns another function (an anonymous function) that takes a developer name. This inner function then tells us about the developer by logging his name and his bio to the console. &lt;br&gt;
One thing to note is that the &lt;em&gt;getBio&lt;/em&gt; function does not return a function call but rather it returns an anonymous function. This means that when the &lt;em&gt;getBio&lt;/em&gt; function is called it returns the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="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;Hi, my name is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shortDevBio&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;And because this is an anonymous function we assigned it to the variable &lt;em&gt;talkAboutDev&lt;/em&gt;. Then we called this anonymous function through the &lt;em&gt;talkAboutDev&lt;/em&gt; variable which now holds a reference to it in memory. &lt;br&gt;
I have already explained why this is possible in my previous article in the &lt;strong&gt;&lt;em&gt;functional programming in JavaScript series&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
If this is not very clear for you, I would kindly suggest that you refer to my article on anonymous and first-class functions in JavaScript for a quick brush-up. You can access it below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/lawrence_eagles" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35543%2Fc9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg" alt="lawrence_eagles"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/lawrence_eagles/understanding-first-class-functions-and-anonymous-functions-in-javascript-b2d" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;UNDERSTANDING FIRST-CLASS FUNCTIONS AND ANONYMOUS FUNCTIONS IN JAVASCRIPT&lt;/h2&gt;
      &lt;h3&gt;Lawrence Eagles ・ Apr 13 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;So when we call the &lt;em&gt;talKAboutDev&lt;/em&gt; function, it tells us about the developer whose bio was passed to the &lt;em&gt;getBio&lt;/em&gt; function.&lt;br&gt;
&lt;em&gt;This is puzzling❗&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How did the &lt;em&gt;talkAboutDev&lt;/em&gt; function get the bio of the developer since the getBio function has already returned before it was called ❓&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can have a second look at the codes as you digest this question:&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;getBio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shortDevBio&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;devName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="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;Hi, my name is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;devName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shortDevBio&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;talkAboutDev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getBio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a developer, writer and instructor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;talkAboutDev&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns "Hi, my name is Lawrence Eagles I am a developer, writer, and instructor"&lt;/span&gt;

&lt;span class="c1"&gt;// But how did it get the developer bio?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;This is possible because of closure. The inner function talkAboutDev still has access to the (free) variables of the outer (getBio) function even after it has returned.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above answer may not really be satisfying especially if you do not have good knowledge of closure in JavaScript before now. We will be taking a deep look at this notorious, often difficult to understand, and extremely powerful feature in the JavaScript programming language in the remaining sections below.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;It is very important to have a good understanding of closure in JavaScript especially if you want to level-up your skill of the language.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other to fully understand closure in JavaScript, we need a solid understanding of some key concepts, which are its fundamental tenets. &lt;br&gt;
We will be looking at these in the next section.&lt;/p&gt;
&lt;h4&gt;
  
  
  2. The Fundamental Tenets &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Section 1 gives us an overview of closure. Although we saw it in action there are still some unanswered questions. In other, for us to get a thorough understanding of it, we need to learn about the key concepts that come into play when closures are created in JavaScript. &lt;br&gt;
Let's deal with them below.&lt;/p&gt;
&lt;h5&gt;
  
  
  1. The Execution Context.
&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;Every code in JavaScript runs inside a wrapper around it called the execution context&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When a JavaScript program runs, a base (global) execution context is created and it wraps around all the codes.&lt;br&gt;
&lt;em&gt;Consider the image below:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1588110041%2FDEV.TO%2Fglobal_execution_context_1_whgiqk.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1588110041%2FDEV.TO%2Fglobal_execution_context_1_whgiqk.png" alt="Alt Execution context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From our image, we can see that the global execution context is made up of the global object, the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable, the variable environment, and the outer environment.&lt;/p&gt;

&lt;p&gt;In other to get a good understanding of closure and even the JavaScript language we need to learn about all these and how they interact when our program runs.&lt;/p&gt;
&lt;h5&gt;
  
  
  The Global Object
&lt;/h5&gt;

&lt;p&gt;This is the &lt;strong&gt;&lt;em&gt;window&lt;/em&gt;&lt;/strong&gt; object. It represents your browser's current tab. If you open another tab, you would get a separate global object because that would create a separate execution context. In a &lt;strong&gt;&lt;em&gt;Node.js&lt;/em&gt;&lt;/strong&gt; environment, however, the global object is not the window object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;In the browser the global object is the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;window&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;object but in&lt;/em&gt; &lt;strong&gt;&lt;em&gt;Node.js&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;the global object is called the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;global&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;object&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kindly run and consider the result of the code below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
console.log(this)

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



&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;When the JavaScript engine runs your code for the first time, the global execution context is created. This would still be created even if the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;.js&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;file is empty&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The runkit program above is an empty &lt;em&gt;.js&lt;/em&gt; file. Notice that the global execution context was still created hence we get the global object in the console. &lt;em&gt;Note runkit is a node.js environment so the global object is called global&lt;/em&gt;&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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1588111063%2FDEV.TO%2FScreenshot_2020-04-28_at_22.56.19_slzpsa.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1588111063%2FDEV.TO%2FScreenshot_2020-04-28_at_22.56.19_slzpsa.png" alt="Alt the global object in console"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  The &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable or keyword
&lt;/h5&gt;

&lt;p&gt;This is a special JavaScript object. I have dealt with it in more detail in one of my articles in the OOP (Object Oriented Programming) in JavaScript series. kindly read more about it below.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/lawrence_eagles" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35543%2Fc9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg" alt="lawrence_eagles"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/lawrence_eagles/understanding-prototypal-inheritance-in-javascript-4f31" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Understanding Prototypal Inheritance In JavaScript&lt;/h2&gt;
      &lt;h3&gt;Lawrence Eagles ・ Apr 23 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#showdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
&lt;br&gt;
All we would say here is that at the global level the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; variable is equal to the global object. &lt;em&gt;It points to it.&lt;/em&gt;
&lt;h5&gt;
  
  
  The Variable Environment
&lt;/h5&gt;

&lt;p&gt;This refers to where the variable lives in memory and how they relate to each other. Each execution context has its own variable environment. For the global execution context, the variable environment is the global object.&lt;/p&gt;
&lt;h5&gt;
  
  
  The outer Environment
&lt;/h5&gt;

&lt;p&gt;When we execute code inside a function, the outer environment is the code outside that function but at the global level, the outer environment is &lt;em&gt;null&lt;/em&gt; because there is nothing outside it. &lt;em&gt;We are at the outermost level&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's elaborate on these by considering some examples.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Kindly examine the code below. &lt;br&gt;
In what order do you expect to see the three&lt;/em&gt; &lt;strong&gt;&lt;em&gt;console.log()&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;results❓&lt;/em&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;A good tip to note as you go through this exercise, is that a new execution context is created whenever a function is called, and this is added to the top of the execution stack. Also whenever a function returns its execution context is removed from the execution stack&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&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;father&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;child&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eagles&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;child&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;father&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Before we run the example above on runkit, let's take a deeper look at how the JavaScript engine would execute this code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At first the global execution context is created and all these functions and variables are added to a place in memory (in the global execution context, this is the global variable).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;There are two phases in the creation of the execution context viz the creation phase and the execution phase. I have covered this in an old article you can access it below.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/lawrence_eagles" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35543%2Fc9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg" alt="lawrence_eagles"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/lawrence_eagles/demystifying-hoisting-in-javascript-4kcj" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;DEMYSTIFYING HOISTING IN JAVASCRIPT&lt;/h2&gt;
      &lt;h3&gt;Lawrence Eagles ・ Apr 4 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;During the execution phase of the global execution context creation, the &lt;em&gt;father()&lt;/em&gt; function is called and this creates a new execution context that is placed on top of the execution stack. The codes within this execution context (literally the codes in this function's code-block) will then be executed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;child()&lt;/em&gt; is called as the codes within the &lt;em&gt;father&lt;/em&gt; function's code block are executed and a new execution context is created and placed on top of the execution stack. &lt;br&gt;
The codes within the &lt;em&gt;child&lt;/em&gt; function's execution context (the execution context on top of the execution stack) will now be executed. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;As a rule if a function is called within a function a new (the inner function's) execution context is created and put on top of the execution stack and until the inner function returns and its execution context is removed from the execution stack the codes of the parent function will not be executed.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;During the execution of the codes in the &lt;em&gt;child&lt;/em&gt; function's execution context, the &lt;em&gt;string&lt;/em&gt; "Lawrence" is assigned to the &lt;em&gt;firstName&lt;/em&gt; variable and that is logged to the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;child&lt;/em&gt; function returns and its execution context is popped off the execution stack (it is removed). The parent function's execution context now sits on top of the execution stack; thus the execution of its code will now continue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, the &lt;em&gt;string&lt;/em&gt; "Eagles" is assigned to the variable &lt;em&gt;lastName&lt;/em&gt; and that is logged to the console. This marks the end of the execution of the &lt;em&gt;parent&lt;/em&gt; function; consequently, its execution context is popped off the execution stack and we have the global execution context left.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only now will the remaining codes in the global execution context be executed. The &lt;em&gt;string&lt;/em&gt; "Lawrence Eagles" is now assigned to the variable &lt;em&gt;fullName&lt;/em&gt; and that would be logged to the console.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the explanation above we expect to get this result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// "Lawrence"&lt;/span&gt;
&lt;span class="c1"&gt;// "Eagles"&lt;/span&gt;
&lt;span class="c1"&gt;// "Lawrence Eagles"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Kindly run and examine the code below.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function father() {
    child();
    let lastName = "Eagles"
    console.log(lastName)
}

function child() {
   let firstname = "Lawrence";
   console.log(firstname)
}

father();
var fullName = "Lawrence Eagles";
console.log(fullName);

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



&lt;h4&gt;
  
  
  3. Scope And Scope Chain &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;As we look at the scope and scope chain in this section, we will elaborate on the &lt;em&gt;variable environment&lt;/em&gt; and the &lt;em&gt;outer environment&lt;/em&gt; with code examples&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Kindly consider the codes below.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;A good tip to note is that when the JavaScript engine does not see a variable in the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;variable environment&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;of an execution context, it would go out to its&lt;/em&gt; &lt;strong&gt;&lt;em&gt;outer environment&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;to look for it.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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;logDevName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;devName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logDevName2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;devName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;devName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;logDevName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;devName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Brendan Eich&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;devName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;logDevName2&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;What do you think would be the values of the devName variable at each&lt;/em&gt; &lt;strong&gt;&lt;em&gt;console.log()&lt;/em&gt;&lt;/strong&gt;❓&lt;/p&gt;

&lt;p&gt;To answer this question let's go through the way the JavaScript engine would execute this code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First the global execution is created and all these functions and variables are added to a place in memory (in the global execution context, this is the global variable).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;During the execution phase of the global execution context creation, the &lt;em&gt;string&lt;/em&gt; "Brendan Eich" is assigned to the variable &lt;em&gt;devName&lt;/em&gt; and that is logged to the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then the &lt;em&gt;logDevName2&lt;/em&gt; function is called and a new execution context is created and put on top of the execution stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the execution of the &lt;em&gt;logDevName2&lt;/em&gt; function, the &lt;em&gt;string&lt;/em&gt; "Lawrence Eagles" is assigned to the variable &lt;em&gt;devName&lt;/em&gt; and that is logged to the console; thus, &lt;em&gt;devName&lt;/em&gt; in this execution context is "Lawrence Eagles".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, the &lt;em&gt;logDevName&lt;/em&gt; function is called and a new execution context is created and put on top of the execution stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;During the execution of this function, the variable &lt;em&gt;devName&lt;/em&gt; is logged to the console. But it is not in this local &lt;strong&gt;&lt;em&gt;Scope&lt;/em&gt;&lt;/strong&gt; because it is not in the variable environment of this functions' execution context (&lt;strong&gt;it is not declared within this function&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;A scope is a place where a variable can be found.&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;So the JavaScript engine would go out to its &lt;strong&gt;&lt;em&gt;outer environment&lt;/em&gt;&lt;/strong&gt; to look for this variable; in this case, the &lt;strong&gt;&lt;em&gt;outer environment&lt;/em&gt;&lt;/strong&gt; is the global execution context. This is so because of the &lt;strong&gt;&lt;em&gt;lexical environment&lt;/em&gt;&lt;/strong&gt; of the &lt;em&gt;logDevName&lt;/em&gt; function.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The lexical environment refers to where something is written physically in your code. The JavaScript engine uses this to determine how things would sit in memory and how they would connect to each other. In our example, both the logDevName and the logDevName2 functions are sitting in the global execution context; therefore, it is the outer environment of both functions, even though the logDevName function is called inside the logDevName2 function&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;devName&lt;/em&gt; variable is found in the variable environment of the global execution context and there it is "Brendan Eich" hence the &lt;em&gt;string&lt;/em&gt; "Brendan Eich" is logged to the console.
You can have a second look at the code below, and hopefully, you should now have a better understanding as you run it in runkit to see the result. &lt;/li&gt;
&lt;/ul&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function logDevName() {
   console.log(devName)
}

function logDevName2() {
    var devName = "Lawrence Eagles"
    console.log(devName)
    logDevName()
}

var devName = "Brendan Eich"
console.log(devName)
logDevName2()

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



&lt;blockquote&gt;
&lt;p&gt;💡 &lt;em&gt;The JavaScript engine goes out of a function's execution context to its outer environment using the reference to that outer environment and continues to go up until it reaches the global execution context. This connection of links or references to different outer environments is called the scope chain&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  4. A Second Look At Closure &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Since we now have an understanding of all the fundamental tenets required to grasp the concept of closure, let's revisit the first example and answer our long-standing question.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;Kindly note this when a function returns and its execution context is removed from the execution stack, its inner function will still have access to the variables in its variable environment as the JavaScript engine performs its search up the scope chain&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function getBio(shortDevBio) {
    return function(devName) {
        console.log("Hi, my name is " + devName + " " + shortDevBio);
   }
}

const talkAboutDev = getBio("I am a developer, writer and instructor")
talkAboutDev("Lawrence Eagles") 

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


&lt;p&gt;The inner function is able to get the value of the &lt;em&gt;shortDevBio&lt;/em&gt; variable even after the &lt;em&gt;getBio&lt;/em&gt; function has returned and its execution context has been removed from the execution stack because its inner function still holds a reference to its variable environment. Thus, the JavaScript engine is able to find the location of the (free) variables (e.g &lt;em&gt;shortDevBio&lt;/em&gt;) in the variable environment of the &lt;em&gt;getBio&lt;/em&gt; function as it continues its search up the scope chain.&lt;/p&gt;

&lt;p&gt;Therefore, we can say that the execution context of the inner function encloses its outer variables. We can also say that it encloses all the variables that it is supposed to have access to. This phenomenon is referred to as &lt;strong&gt;&lt;em&gt;closure.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It makes possible some very powerful javascript design patterns used in some of the most popular JavaScript frameworks and libraries. &lt;/p&gt;

&lt;p&gt;The code above can be re-written like this using an alternate syntax.&lt;br&gt;
&lt;em&gt;Kindly examine the code below:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getBio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shortDevBio&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;devName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="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;Hi, my name is &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;devName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shortDevBio&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;// uses an alternate syntax to run both functions in one line.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;developerBio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getBio&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a developer, writer and instructor.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;developerBio&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;What do you think the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;console.log()&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;would output❓&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💡 &lt;em&gt;The alternate syntax uses two parentheses. The first () simply calls the outer (getBio) function and since that returns another function the second () calls the inner function, hence we get the same result&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can run the code in runkit below:&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
function getBio(shortDevBio) {
    return function(devName) {
        console.log("Hi, my name is " + devName + " " + shortDevBio);
   }
}

// uses an alternate syntax to run both functions in one line.
const developerBio = getBio("I am a developer, writer and instructor.")("Lawrence Eagles") 

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



&lt;p&gt;We see this pattern in action when we work with React and Redux using the &lt;a href="https://react-redux.js.org/" rel="noopener noreferrer"&gt;&lt;em&gt;React-Redux&lt;/em&gt;&lt;/a&gt; library.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;✨ &lt;em&gt;React-Redux is a library that connects React and Redux together. It poses itself as the official React bindings for Redux and it is maintained by the Redux team.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Below is an extract from an example in their &lt;a href="https://react-redux.js.org/api/connect" rel="noopener noreferrer"&gt;official doc&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt;
&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;TodoApp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The details of what is going on here are out of the scope of this article but I just want to point out the way the &lt;em&gt;connect&lt;/em&gt; function is called with two parentheses. The first takes &lt;em&gt;null&lt;/em&gt; and &lt;em&gt;mapDispatchToProps&lt;/em&gt; while the second takes the &lt;em&gt;TodoApp&lt;/em&gt; component as its argument the result is then exported. &lt;br&gt;
This pattern is made possible because of &lt;strong&gt;&lt;em&gt;closure&lt;/em&gt;&lt;/strong&gt; in JavaScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Closing Thoughts &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;It has really been a long article and if you got here you are appreciated. &lt;br&gt;
I do hope that at this point, you can see the benefits of our long discussion and at least got a thing or two from this article. If so I would be looking forward to hearing your opinions, comments, questions, or requests (in case anything is not clear) at the comments section below. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Prototypal Inheritance In JavaScript</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Thu, 23 Apr 2020 11:38:02 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/understanding-prototypal-inheritance-in-javascript-4f31</link>
      <guid>https://dev.to/lawrence_eagles/understanding-prototypal-inheritance-in-javascript-4f31</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What Is Object-oriented Programming (OOP)&lt;/li&gt;
&lt;li&gt;Classical vs Prototypal Inheritance&lt;/li&gt;
&lt;li&gt;The Prototype Object And The Prototype Chain&lt;/li&gt;
&lt;li&gt;Exposing &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; Keyword in JavaScript&lt;/li&gt;
&lt;li&gt;JavaScript Base Object, The Prototype Of All Prototypes&lt;/li&gt;
&lt;li&gt;The Power Of Prototypal Inheritance&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. What Is Object-Oriented Programming (OOP) &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Object-oriented programming is a programming paradigm that involves organizing codes into object definitions. These are sometimes called classes. &lt;/p&gt;

&lt;p&gt;In object-oriented programming, we use objects to model real-world things that we want to represent inside our programs. These objects can contain (&lt;strong&gt;&lt;em&gt;encapsulate&lt;/em&gt;&lt;/strong&gt;) related information which is the properties and methods (functions) stored in the objects. These are often the properties and behaviours of the real-life object we are modeling.&lt;/p&gt;

&lt;h4&gt;
  
  
  Classical vs Prototypal Inheritance &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;JavaScript is a very unique, beautiful, sometimes weird, and incredibly powerful programming language. Object-oriented JavaScript uses prototypal inheritance. Here we find JavaScript making popular a concept that is in many ways better than what is already in use in other programming languages like Java, C#, and C++ (&lt;strong&gt;&lt;em&gt;Classical inheritance&lt;/em&gt;&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;The classical inheritance or class bases inheritance involves writing classes; these are like blueprints of objects to be created. Classes can inherit from classes and even create subclasses. This method is solid and battle-tested as it is what powers many popular programming languages like Java and C++ as mentioned above but it has its downside.&lt;/p&gt;

&lt;p&gt;One of the downsides of classical inheritance is that it is very verbose and you can quickly end up with a huge mass of collection and trees of objects that interact, that it can become very hard to figure out what is going on even if you use good practice. Plus you would have to learn and use a lot of intimidating keywords viz: &lt;strong&gt;&lt;em&gt;friend, protected, private, interface, etc&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The class keyword, however, has been introduced to JavaScript in ES2015, but it is just syntactical sugar,&lt;/em&gt; &lt;strong&gt;&lt;em&gt;JavaScript remains prototype-based&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Prototypal inheritance is a much simpler approach. It is flexible, extensible, and very easy to understand. &lt;em&gt;It is not a silver bullet anyway&lt;/em&gt; but it is in many ways better than class-based inheritance and it would be our focus going forward.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. The Prototype Object And The Prototype Chain &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;To understand prototypal inheritance we need to understand these three key concepts viz: &lt;em&gt;inheritance, prototype, prototype chain&lt;/em&gt;&lt;br&gt;
Inheritance refers to a process whereby one object gets access to the properties and methods of another object. &lt;br&gt;
Let's deal with these keywords with examples.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Remember in OOP we use objects to model real-world things that we want to represent inside our programs&lt;/em&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    on () {
       console.log("Turning on your " + this.name + " device")
    },
    off () {
       console.log("Turning off your " + this.name + " device")
    }

}
console.log(AppleInc)

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


&lt;p&gt;In the small contrived example above I have modeled the Apple company. It has a &lt;em&gt;name, logo, and operating_system&lt;/em&gt; property, both an &lt;strong&gt;&lt;em&gt;on()&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;off&lt;/em&gt;&lt;/strong&gt; methods(which are functions, meant to describe the behaviours of Apple devices). &lt;/p&gt;

&lt;p&gt;We will proceed to model some products of Apple and have them inherit these properties. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All Apple products are unique and each reflects the image of the company. &lt;br&gt;
You can certainly tell it is an Apple product even without seeing the logo; thus we can say in real-life, each Apple product inherits properties like design, operating system, etc from the company. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;We will try to express this concept in codes as we demystify prototypal inheritance&lt;/em&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    on () {
       console.log("Turning on your " + this.name + " device")
    },
    off () {
       console.log("Turning off your " + this.name + " device")
    }

}

const iPhone = {
    name: "iPhone",
    operating_system: "ios"
}

console.log(iPhone) 
iPhone.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
console.log(iPhone) // logs iPhone with AppleInc as its prototype.
console.log(iPhone.on())
console.log(iPhone.off())

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


&lt;blockquote&gt;
&lt;p&gt;Kindly run the program in runkit by clicking the green run button to the right and click open the result of each &lt;strong&gt;&lt;em&gt;console.log()&lt;/em&gt;&lt;/strong&gt;. &lt;br&gt;
You would see an image like this:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587582767%2FScreenshot_2020-04-22_at_20.07.53_eyuey2.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587582767%2FScreenshot_2020-04-22_at_20.07.53_eyuey2.png" alt="alt console.log() result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice at the first &lt;strong&gt;&lt;em&gt;console.log()&lt;/em&gt;&lt;/strong&gt; iPhone does not have a &lt;strong&gt;&lt;em&gt;proto&lt;/em&gt;&lt;/strong&gt; object property. But after we assigned AppleInc as its prototype, in the second &lt;strong&gt;&lt;em&gt;console.log()&lt;/em&gt;&lt;/strong&gt; we can see a &lt;strong&gt;&lt;em&gt;proto&lt;/em&gt;&lt;/strong&gt; property, which is the AppleInc object.&lt;/p&gt;

&lt;p&gt;Modern browsers allow us to set an object's prototype like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AppleInc&lt;/span&gt; &lt;span class="c1"&gt;// sets AppleInc to be the prototype of the iPhone object.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;💥 &lt;em&gt;Kindly note, I am setting this just for demonstration purposes. Do not set your object's prototype like this. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto" rel="noopener noreferrer"&gt;Read more from MDN&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;✨ &lt;em&gt;I would also be writing about the recommended ways to set an object's prototype in JavaScript in the upcoming articles in this series&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also notice that somehow we could call the &lt;strong&gt;&lt;em&gt;on()&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;off()&lt;/em&gt;&lt;/strong&gt; methods from our &lt;em&gt;iPhone&lt;/em&gt; object. (But they are not originally there!).&lt;/p&gt;

&lt;p&gt;We can see that the &lt;em&gt;iPhone&lt;/em&gt; object has &lt;strong&gt;&lt;em&gt;inherited&lt;/em&gt;&lt;/strong&gt; the properties and methods of its &lt;strong&gt;&lt;em&gt;prototype object.&lt;/em&gt;&lt;/strong&gt; (An object from which another object inherits properties and methods. It lives as a property in that object with the name &lt;strong&gt;&lt;em&gt;__proto__&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As a result of this, we can call the &lt;strong&gt;&lt;em&gt;on()&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;off()&lt;/em&gt;&lt;/strong&gt; methods even when they are not originally in the &lt;em&gt;iPhone&lt;/em&gt; object. This is possible because JavaScript holds a reference to the prototype of the &lt;em&gt;iPhone&lt;/em&gt; object and when we try to access a property or method, it looks for it in the &lt;em&gt;iPhone&lt;/em&gt; object first, and if it cannot find it there, it goes to its prototype (&lt;strong&gt;The __proto__ object property as seen&lt;/strong&gt;) and looks for it there. &lt;br&gt;
&lt;em&gt;It returns the property or method once it finds it and stops the search.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;This explains why:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="c1"&gt;// returns iPhone and not Apple&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    on () {
       console.log("Turning on your " + this.name + " device")
    },
    off () {
       console.log("Turning off your " + this.name + " device")
    }

}

const iPhone = {
    name: "iPhone",
    operating_system: "ios"
}

iPhone.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
console.log(iPhone.name)

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



&lt;p&gt;The JavaScript engine finds the name property in the &lt;em&gt;iPhone&lt;/em&gt; object returns it, and ends the search. &lt;/p&gt;

&lt;p&gt;In JavaScript, a prototype can have its own prototype. So the &lt;em&gt;AppleInc&lt;/em&gt; object can have its own prototype which can, in turn, have its own prototype and the process can go on. So when the JavaScript engine looks for a property or method in an object and cannot find it, it would go to its prototype and look for it there, if it does not find it, it would go to the prototype of that prototype and continue to go down the &lt;strong&gt;&lt;em&gt;prototype chain&lt;/em&gt;&lt;/strong&gt; until it finds it. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This chain of links or object references between an object, it's prototype, and the prototype of its prototype all the way down to the last prototype is called the prototype chain&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const Company = {
    category: "Technology"
}

const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    on () {
       console.log("Turning on your " + this.name + " device")
    },
    off () {
       console.log("Turning off your " + this.name + " device")
    }

}

const iPhone = {
    name: "iPhone",
    operating_system: "ios"
}

AppleInc.__proto__ = Company // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
iPhone.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
console.log(iPhone.category) // gets this from the search down the prototype chain

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


&lt;p&gt;Run the code above and see that we can access the category property even from the &lt;em&gt;iPhone&lt;/em&gt; object because of the &lt;em&gt;search down the prototype chain.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Exposing &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; Keyword in JavaScript &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;You might be surprised by this title, but I see an opening in this post to deal a little on the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword which has a reputation of being confusing in our beautiful language; JavaScript. &lt;br&gt;
Let's visit our example again:&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    on () {
       console.log("Turning on your " + this.name + " device")
    },
    off () {
       console.log("Turning off your " + this.name + " device")
    }

}

const iPhone = {
    name: "iPhone",
    operating_system: "ios"
}

iPhone.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
console.log(iPhone.on())
console.log(iPhone.off())

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


&lt;p&gt;From our code above we understood that we are able to call the &lt;strong&gt;&lt;em&gt;on()&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;off()&lt;/em&gt;&lt;/strong&gt; methods from the &lt;em&gt;iPhone&lt;/em&gt; object because of JavaScript's prototypal inheritance. (where we explained &lt;em&gt;inheritance, prototype, and the prototype chain&lt;/em&gt;). &lt;br&gt;
But why does this work correctly?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// returns Turning on your iPhone device&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="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;off&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// returns Turning on your iPhone device&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;How does it know the correct name is &lt;em&gt;iPhone&lt;/em&gt; when the method is actually on the &lt;em&gt;AppleInc&lt;/em&gt; object which is the prototype of the &lt;em&gt;iPhone&lt;/em&gt; object and has its own name property? &lt;br&gt;
It is because of the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword. It is pointing to the &lt;em&gt;iPhone&lt;/em&gt; object; thus it gets the name property from it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Take note, the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;keyword in cases like this, starts its search from the object that originates the call, and since it found the name property in the iPhone object it points to it there.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hold unto the above &lt;em&gt;rule of thumb&lt;/em&gt; as we take a deeper look at &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; below:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword is a very interesting aspect of JavaScript it is extremely powerful and can sometimes be a little confusing. Below is the general &lt;em&gt;rule of thumb&lt;/em&gt; to help you further understand the behaviour of the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kindly run the example code below and consider the result. &lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
// New additions
let name  = "Brendan Eich"

function sayName() {
 console.log(this.name) 
}

let Person = {
   name: "Lawrence Eagles",
   sayName() { console.log(this.name) }
}

sayName()
Person.sayName()

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



&lt;p&gt;From the result, we can see that when you use the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword in a function it points to the global object but when you use it in a method (a function inside an object), it points to that object.&lt;br&gt;
Following this, let's return to our code. I have added some extra properties to help us understand the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword better and consequently, understand our example more thoroughly.&lt;br&gt;
&lt;em&gt;Kindly run the code below and consider the results.&lt;/em&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const Company = {
    category: "Technology",
    getNews () {
       console.log("viewing " + this.category + " news on my " + this.name + " device")
    }
}

const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    store: "Apple Store",
    on () {
       console.log("Turning on my " + this.name + " device")
    },
    off () {
       console.log("Turning off my " + this.name + " device")
    },
    getDevice() {
       console.log("I just bought my " + this.name + " from " + this.store)
    }
}

const iPhone = {
    name: "iPhone",
    operating_system: "ios"
}

AppleInc.__proto__ = Company // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
iPhone.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE

// let's buy an iPhone from the Apple store, then let's turn on and off our iPhone.
console.log(iPhone.getDevice())
console.log(iPhone.on())
console.log(iPhone.off())
console.log(iPhone.getNews())

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


&lt;p&gt;From the result of the code above we see that when we ran the method to buy an iPhone,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDevice&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; 
&lt;span class="c1"&gt;// returns I just bought my iPhone from Apple Store&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword points to different objects to get the correct property. It starts by pointing to the object that originates the call and since it can find &lt;strong&gt;&lt;em&gt;name property&lt;/em&gt;&lt;/strong&gt; in the &lt;em&gt;iPhone&lt;/em&gt; object it points to that. But it cannot find the &lt;strong&gt;&lt;em&gt;store property&lt;/em&gt;&lt;/strong&gt; in the iPhone object so it points to its prototype and looks for the property there and finds it.&lt;br&gt;
This same principle holds when we tried to turn on/off our iPhone.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// returns Turning on my iPhone device&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="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;off&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// returns Turning off my iPhone device&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; keyword starts searching from the object that originates the call and because it can find the &lt;strong&gt;&lt;em&gt;name property&lt;/em&gt;&lt;/strong&gt; in it, it points to it there. Thus we get the correct device name even though the &lt;strong&gt;&lt;em&gt;on()&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;off()&lt;/em&gt;&lt;/strong&gt; methods are not in that object but in their prototype. &lt;br&gt;
Finally, the result is the same when we try to read news from our iPhone device,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iPhone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDevice&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Notice the &lt;strong&gt;&lt;em&gt;getDevice()&lt;/em&gt;&lt;/strong&gt; method is in the &lt;em&gt;Company&lt;/em&gt; object which is the prototype of the &lt;em&gt;AppleInc&lt;/em&gt; object which is the prototype of the &lt;em&gt;iPhone&lt;/em&gt; object. And because of the prototype chain, we can call &lt;strong&gt;&lt;em&gt;getDevice()&lt;/em&gt;&lt;/strong&gt; from the &lt;em&gt;iPhone&lt;/em&gt; object as if it just sits in it. &lt;/p&gt;

&lt;p&gt;Let's move forward.&lt;/p&gt;
&lt;h4&gt;
  
  
  5. JavaScript Object, The Prototype Of All Prototypes &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;When I said the JavaScript engine looks in an object for a property and if it cannot find it, it keeps going down the prototype chain until it finds it; you might have wondered what will be the last prototype? &lt;br&gt;
Like in our case, what would be the prototype of the &lt;em&gt;Company&lt;/em&gt; object?&lt;br&gt;
Kindly run the code below and consider the result.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const Company = {
    category: "Technology"
}

const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    on () {
       console.log("Turning on your " + this.name + " device")
    },
    off () {
       console.log("Turning off your " + this.name + " device")
    }

}

const iPhone = {
    name: "iPhone",
    operating_system: "ios"
}

AppleInc.__proto__ = Company // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
iPhone.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
console.log(Company.__proto__) // gets this from the search down the prototype chain

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



&lt;p&gt;You can see that the prototype of the &lt;em&gt;Company&lt;/em&gt; object is the &lt;em&gt;built-in JavaScript object.&lt;/em&gt; Since in Javascript everything is an object, all the other data types in JavaScript inherit properties and methods from the base object. So it is the final prototype in JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Kindly open up the console.log() result of the code below from runkit and you would see something like this:&lt;/em&gt;&lt;/strong&gt;&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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587589187%2FScreenshot_2020-04-22_at_21.59.30_te5exx.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587589187%2FScreenshot_2020-04-22_at_21.59.30_te5exx.png" alt="object prototype image"&gt;&lt;/a&gt;&lt;br&gt;
Notice some familiar names like the &lt;em&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" rel="noopener noreferrer"&gt;hasOwnProperty&lt;/a&gt; and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf" rel="noopener noreferrer"&gt;isPrototypeOf&lt;/a&gt; methods, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor" rel="noopener noreferrer"&gt;the constructor&lt;/a&gt; etc&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So the base object in JavaScript is the prototype of all data types in JavaScript, be it arrays, strings, numbers, functions, etc. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;All data types in JavaScript have a prototype object that inherits its property and methods from the base object. Let's look at some of them in the next section.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  6. The Power Of Prototypal Inheritance &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Welcome to the power of prototypal inheritance. I am sure you would have already seen how flexible, extensible, and how easily objects can share properties and methods when using prototypal inheritance.&lt;/p&gt;

&lt;p&gt;Let's look at some code examples for more:&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Kindly run the codes below and open up each console.log() in runkit&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const simpleArray = []
const simpleFunction = function simpleFunction () {}
const simpleString = ""

console.log(simpleArray.__proto__)
console.log(simpleFunction.__proto__)
console.log(simpleString.__proto__)

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


&lt;p&gt;If you open up the first &lt;em&gt;console in runkit&lt;/em&gt; you would notice that it has an &lt;strong&gt;&lt;em&gt;Array Prototype&lt;/em&gt;&lt;/strong&gt; which have a huge collection of properties and methods as seen in the image below.&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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587590157%2FDEV.TO%2FScreenshot_2020-04-22_at_22.15.36_j0dzyu.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587590157%2FDEV.TO%2FScreenshot_2020-04-22_at_22.15.36_j0dzyu.png" alt="alt Array prototype properties"&gt;&lt;/a&gt;&lt;br&gt;
Notice some familiar names here viz: &lt;em&gt;concat(), every(), filter(), find(), pop(), map(), reduce() etc you can scroll up or down on runkit for more of them.&lt;/em&gt; &lt;br&gt;
These are methods we use every day in JavaScript when we implement our own array. Notice they all sit in the &lt;strong&gt;&lt;em&gt;Array Prototype Object&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Think of how much hassle it would be if we would have to write out all these properties and methods every time we create an array in our program!&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you open up the next &lt;em&gt;console in runkit&lt;/em&gt;, you would get something 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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587590627%2FDEV.TO%2FScreenshot_2020-04-22_at_22.23.10_hiff4h.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587590627%2FDEV.TO%2FScreenshot_2020-04-22_at_22.23.10_hiff4h.png" alt="alt Function prototype properties"&gt;&lt;/a&gt;&lt;br&gt;
Notice some familiar names like the &lt;em&gt;call(), apply() and bind()&lt;/em&gt; methods, all present in the &lt;strong&gt;&lt;em&gt;Function Prototype Object&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
The prototype of all JavaScript functions is called the &lt;strong&gt;&lt;em&gt;Function Prototype.&lt;/em&gt;&lt;/strong&gt; It is actually an &lt;em&gt;empty function. (a function with nothing in its code "{}" block&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Finally, open up the last &lt;em&gt;console in runkit&lt;/em&gt; and you would get something 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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587590807%2FDEV.TO%2FScreenshot_2020-04-22_at_22.26.30_ecxa2m.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1587590807%2FDEV.TO%2FScreenshot_2020-04-22_at_22.26.30_ecxa2m.png" alt="alt String prototype properties"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice some familiar names like the &lt;em&gt;length property, the split(), indexOf(), substring() methods and lot's more you can scroll down to see all&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The prototype of all strings in JavaScript is called the &lt;strong&gt;&lt;em&gt;String Prototype.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What do you think would be the prototype of all these prototypes viz:&lt;br&gt;
&lt;em&gt;array Prototype, Function Prototype, and the String Prototype?&lt;/em&gt;&lt;br&gt;
Let's answer this with some more code examples.&lt;br&gt;
Kindly run the codes below and consider the results in the &lt;em&gt;console in runkit&lt;/em&gt;.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const simpleArray = []
const simpleFunction = function simpleFunction () {}
const simpleString = ""

console.log(simpleArray.__proto__) // array prototype
console.log(simpleArray.__proto__.__proto__) // base object
console.log(simpleFunction.__proto__) // function prototype 
console.log(simpleFunction.__proto__.__proto__) // base object
console.log(simpleString.__proto__) // string prototype 
console.log(simpleString.__proto__.__proto__) // base object

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


&lt;p&gt;From the above results, it is clear that the prototype of all prototypes in JavaScript is the base object.&lt;/p&gt;

&lt;p&gt;We can also see a powerful pattern here. Prototypal inheritance allows us to write our properties and methods in one place and share them with other objects in our application. Hence both the &lt;em&gt;array, function, and string prototype&lt;/em&gt; contains a huge list of properties and methods that is passed on to every array, function, and string declaration respectively in our program. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This saves us memory and time.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can also use this power to create other Apple devices and have them get some properties and methods from the AppleInc object.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const Company = {
    category: "Technology"
}

const AppleInc = {
    name: "Apple",
    logo: "Apple fruit",
    operating_system: "Apple Software",
    on () {
       console.log("Turning on your " + this.name + " device")
    },
    off () {
       console.log("Turning off your " + this.name + " device")
    }

}

const iPhone = {
    name: "iPhone",
    operating_system: "ios"
}

const iPad = {
    name: "iPad",
    operating_system: "ios"
}

const laptop = {
    name: "mac",
    operating_system: "mac os x"
}

AppleInc.__proto__ = Company // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
iPhone.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
iPad.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE
laptop.__proto__ = AppleInc // NEVER DO THIS IN REAL-LIFE. ONLY FOR DEMONSTRATION PURPOSE

// let's turn on and off all our apple devices
console.log(iPhone.on())
console.log(iPad.on())
console.log(laptop.on())
console.log(iPhone.off())
console.log(iPad.off())
console.log(laptop.off())

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


&lt;h4&gt;
  
  
  7. Closing Thoughts &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;I do hope you followed through to this point. You are appreciated. It is a pretty long post but I want to believe that you got a thing or two. If you are not clear on any point or you have an addition, in case I miss anything, I would be looking forward to hearing from you in the comment section below. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
    <item>
      <title>MODERN METHODS TO REMOVE ITEMS FROM ARRAYS IN JAVASCRIPT</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Mon, 20 Apr 2020 22:41:41 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/modern-methods-to-remove-items-from-arrays-in-javascript-3eed</link>
      <guid>https://dev.to/lawrence_eagles/modern-methods-to-remove-items-from-arrays-in-javascript-3eed</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Arrays In JavaScript&lt;/li&gt;
&lt;li&gt;Array.prototype.shift()&lt;/li&gt;
&lt;li&gt;Array.prototype.pop()&lt;/li&gt;
&lt;li&gt;The Delete Operator&lt;/li&gt;
&lt;li&gt;Array.prototype.splice()&lt;/li&gt;
&lt;li&gt;Array.prototype.filter()&lt;/li&gt;
&lt;li&gt;A Masterpiece&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. Arrays In JavaScript &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Arrays in JavaScript are special objects that store items sequentially, that is, they store items one after another in memory. They are high-level, list-like objects.  You can actually think of them as lists of items. &lt;/p&gt;

&lt;p&gt;All items in an array have an index which indicates the position of that item in the array. The item in the first position has an index of 0 then next has an index of 1 etc. For this reason, JavaScript's array indexes are zero-based.&lt;br&gt;
Items in JavaScript arrays can be accessed using their index.&lt;br&gt;
We can find the index of an item in an array using a special method called the &lt;em&gt;indexOf&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;You can simply think of JavaScript arrays as an ordered set of values that you refer to with a name and an index&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const listOfAnimals = [ "dog", "cat", "rat", "fish", "rabbit" ]

console.log(listOfAnimals) // returns the listOfAnimals array
console.log(listOfAnimals[0]) // gets the item at index 0 
console.log(listOfAnimals[2]) // gets the third item which is the item at index 2

let itemPosition = listOfAnimals.indexOf("rat") // gets the index of the item passed. here "rat"

console.log(itemPosition) // displays the index of "rat" in the listOfAnimals array

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


&lt;p&gt;The total number of items in an array is the length of that array.&lt;br&gt;
The length property is special. It always returns the index of the last element plus one.&lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const evenNumbers = [2,4,6,8,10,12]
console.log(evenNumbers.length) // returns the length of the evenNumbers array

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


&lt;p&gt;JavaScript arrays are the most used data structure and because they organize items sequentially, it is super easy to access the first and last item. Hence deleting these items are very easy in JavaScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Array.prototype.shift() &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;shift()&lt;/em&gt; method removes the first element in an array (that is the item at the index of zero). It also re-orders the remaining elements in the array and decrements the array length by one. Finally, it returns the removed item.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const languages = [ "PHP", "Python", "Rust", "C++", "C", "Ruby", "JavaScript" ]
const removedLanguage = languages.shift()
console.log(removedLanguage) // returns "PHP"
console.log(languages) // returns new array without "PHP"

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


&lt;h4&gt;
  
  
  3. Array.prototype.pop() &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;em&gt;pop()&lt;/em&gt; method is the opposite of the &lt;em&gt;shift()&lt;/em&gt;. It removes the last element of the array.&lt;br&gt;
The index of the last element in the array is the length of the array minus one. The &lt;em&gt;pop()&lt;/em&gt; method, also decrements the array length by one and returns the removed element.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const frameworks = [ "React.js", "Angular", "Vue.js", "Express.js", "Ember.js" ]
const removedFramework = frameworks.pop()
console.log(removedFramework) // returns "Ember.js"
console.log(frameworks) // returns new array without "Ember.js"

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


&lt;h4&gt;
  
  
  4. The Delete Operator &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Both the &lt;em&gt;pop()&lt;/em&gt; and the &lt;em&gt;shift()&lt;/em&gt; methods give us a way to remove elements from an array from pre-set position viz: the last or first positions respectively. While they are great, we do not get the freedom of deleting elements from any other position. What if we want to delete elements at a certain index which is neither the first nor the last?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;delete operator&lt;/strong&gt; is great for this.&lt;br&gt;
Unlike &lt;strong&gt;pop()&lt;/strong&gt; and &lt;strong&gt;shift()&lt;/strong&gt; the &lt;strong&gt;delete operator&lt;/strong&gt; returns the new array. &lt;br&gt;
&lt;/p&gt;
&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const items = ["eggs", "meat", "vegetables", "salad", "rice", "fish" ]
delete items[3]

console.log(items)
console.log(items.length)
console.log(items[3])

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


&lt;p&gt;&lt;em&gt;One thing that must be noted before one should use the delete method is that it does not change the length of the array as seen above. It removes the specified element and adds&lt;/em&gt; &lt;strong&gt;&lt;em&gt;undefined&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;in its place.&lt;/em&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  5. Array.prototype.splice() &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;If the &lt;strong&gt;delete operator&lt;/strong&gt; is not very suitable for you because it does not update the array length, another built-in array method you can use is the &lt;strong&gt;splice()&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;splice()&lt;/strong&gt; method is a very powerful built-in array method that can be used to remove array elements at any index. It can also be used to add elements or replace an existing element. &lt;em&gt;But we will just stick to removing elements from an array.&lt;/em&gt; It can remove multiple elements from an array unlike &lt;strong&gt;pop()&lt;/strong&gt; and &lt;strong&gt;shift()&lt;/strong&gt; that removes one element at a time.&lt;br&gt;
Lastly the &lt;strong&gt;splice()&lt;/strong&gt; method returns a new array containing the deleted element/elements. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The&lt;/em&gt; &lt;strong&gt;&lt;em&gt;splice()&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;method can take up to three parameters for our use case it needs only two. The first specifies the index to start deleting from the second specifies how many elements to remove from the array&lt;/em&gt;&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
//remove single element
const numbersInWords = [ "one", "two", "three", "four", "five", "dozen", "six" ]
const arrayWithDeletedItems = numbersInWords.splice(5, 1) //returns ["dozen"]

// remove multiple elements
const mammals = ["man", "dog", "rat", "cat", "fish", "snake", "fly"]
const nonMammals = mammals.splice(4, 3) // return ["fish", "snake", "fly"]

console.log(numbersInWords)
console.log(arrayWithDeletedItems)
console.log(mammals)
console.log(nonMammals)

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


&lt;h4&gt;
  
  
  6. Array.prototype.filter() &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;We saw the power of  &lt;strong&gt;splice()&lt;/strong&gt; above as we used it to elements from any index in an array. But with &lt;strong&gt;splice()&lt;/strong&gt; we could only delete multiple elements in series. &lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const numList = [1,2,3,4,5,6,"Half-dozen",7,8,9,10,11,12,"one-dozen",13,14,15,16,17,18,19,20]
const result = numList.splice(7, 5)

console.log(result)
console.log(numList)

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


&lt;p&gt;What if we want to delete all the words from our numList array above? since the words are not in series in the above array &lt;strong&gt;splice()&lt;/strong&gt; is not the best fit. Except we are implementing our own &lt;strong&gt;remove()&lt;/strong&gt; method where we can use it under the hood. &lt;em&gt;A good option is to use the&lt;/em&gt; &lt;strong&gt;filter()&lt;/strong&gt; &lt;em&gt;since we are deleting all instance of a string in the array.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;filter()&lt;/strong&gt; methods calls a provided callback function once for each element in an array and returns a new array of elements that passes the test implemented in the callback function.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const numList = [1,2,3,4,5,6,"Half-dozen",7,8,9,10,11,12,"one-dozen",13,14,15,16,17,18,19,20]
pureNumList = numList.filter(item =&amp;gt; typeof item !== "string") // returns a list of numbers
pureEvenNumber = numList.filter(item =&amp;gt; typeof item !=="string" &amp;amp;&amp;amp; item % 2 === 0) // returns a list of only even numbers. 

console.log(numList)
console.log(pureNumList)
console.log(pureEvenNumber)

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


&lt;p&gt;Noticed the power of the &lt;strong&gt;filter()&lt;/strong&gt; method, how it allows us to remove multiple elements from an array regardless of their index. Also, notice how we can combine conditions in the callback function implementation to target multiple elements.&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
const numList = [1,2,3,4,5,6,"Half-dozen",7,8,9,10,11,12,"one-dozen",13,14,15,16,17,18,19,20]

pureEvenNumber = numList.filter(item =&amp;gt; typeof item !=="string" &amp;amp;&amp;amp; item % 2 === 0) // returns a list of only even numbers. 

console.log(pureEvenNumber)

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


&lt;h4&gt;
  
  
  A Masterpiece &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;There are still other ways to do this in JavaScript, truly at this point, it should boil down to a developer's implementation of his own custom function. Maybe you dream of a &lt;strong&gt;remove()&lt;/strong&gt; method of your own. I will leave you with a masterpiece from &lt;strong&gt;John Resig&lt;/strong&gt;. (The creator of &lt;em&gt;jQuery&lt;/em&gt;).&lt;/p&gt;


&lt;div class="runkit-element"&gt;
  &lt;code&gt;
    
  &lt;/code&gt;
  &lt;code&gt;
    
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from &amp;lt; 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

const numList = [1,2,3,4,5,6,"Half-dozen",7,8,9,10,11,12,"one-dozen",13,14,15,16,17,18,19,20]

numList.remove(13)
numList.remove(6)
console.log(numList) // returns a new array with only numbers.

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


&lt;h4&gt;
  
  
  8. Closing Thoughts &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;These are a nice collection of ways to remove items from an array but it is by no means an exhaustive list. As I said developers can always come up with something novel. If you do have a custom helper function to achieve this task, you are more than welcome to share it in the comment section below. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>UNDERSTANDING FIRST-CLASS FUNCTIONS AND ANONYMOUS FUNCTIONS IN JAVASCRIPT</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Mon, 13 Apr 2020 09:56:03 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/understanding-first-class-functions-and-anonymous-functions-in-javascript-b2d</link>
      <guid>https://dev.to/lawrence_eagles/understanding-first-class-functions-and-anonymous-functions-in-javascript-b2d</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Functions And Objects&lt;/li&gt;
&lt;li&gt;The Anatomy Of A JavaScript Function&lt;/li&gt;
&lt;li&gt;Statements, Expressions, And Anonymous Functions&lt;/li&gt;
&lt;li&gt;First-class Functions&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. FUNCTIONS AND OBJECTS &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;You might be a little surprised at this subtitle. This is because &lt;strong&gt;in JavaScript functions are objects.&lt;/strong&gt;&lt;br&gt;
They are actually a special kind of objects with some unique properties in them. It is often a subject of debate in JavaScript, on what to teach first between functions and objects. I am of the school of thought that both should be taught at the same time. This is because in JavaScript, functions and objects are very intertwined and in many ways they are similar.&lt;br&gt;
With that said, I would like to do a quick refresher on objects in JavaScript before proceeding to functions.&lt;/p&gt;
&lt;h5&gt;
  
  
  Objects in JavaScript
&lt;/h5&gt;

&lt;p&gt;They are just a collection of key-value pairs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&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="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="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="c1"&gt;// a dummy example&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;An object property's value can be any primitive data type in JavaScript e.g: Strings, Numbers, Boolean. It can even be another object. In all of these cases, they are referred to as the object property. On some occasions, the value can be a function. when the value is a function that function is called a method.&lt;br&gt;
we can access an object property's value by passing the key to special operators in JavaScript viz &lt;strong&gt;&lt;em&gt;the member access operator and the computed member access operator&lt;/em&gt;&lt;/strong&gt;. Let's take a quick look at this.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;// creates an empty dog object&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;legs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// adds a leg property using member access operator&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bark&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="s2"&gt;woaf woaf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// adds a bark property using computed member access operator&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;details&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// adds details property which is another object using the member access operator&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;run&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="kd"&gt;function&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;running dog&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// adds a method using the computed member access operator &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="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns &lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;legs&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="nx"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;woaf woaf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;run&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="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;running dog&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the example above I have used both the member access operator (which is just the dot) and the computed member access operator (the square braces []) to add properties and a method to the object. Both operators look for the property to be added in the object and if they cannot find it they would create it there. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;One key point to note and carry along is that we could have easily created the dog object with all the properties on the fly.&lt;/em&gt; Like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;legs&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="na"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;woaf woaf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;details&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;run&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="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;running dog&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="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="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;legs&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="nx"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;woaf woaf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;run&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="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;running dog&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="c1"&gt;// we still get the same result but this is faster.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Another important thing to note and carry along is that an object holds the references (addresses) of all its properties and methods in your computer's memory. It knows where they all sit in memory&lt;/em&gt;&lt;br&gt;
As a result of this, we can access them using these same operators. Hence:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;legs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// returns 4&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;lg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;details&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// returns { owner: "Tim", name: "Jack" }&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="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns "woaf woaf"&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="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// return function(){console.log("running dog") }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  2. THE ANATOMY OF A JAVASCRIPT FUNCTION &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Javascript functions are special objects. They have the same properties as other objects but have some extra properties that make them &lt;strong&gt;&lt;em&gt;first-class objects&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;first-class citizens&lt;/em&gt;&lt;/strong&gt; as some call it. Two of these are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Name property&lt;/li&gt;
&lt;li&gt;Code property&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A function object has a name and a code property.&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;ageTeller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;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="s2"&gt;`my age is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;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="nx"&gt;ageTeller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//returns ageTeller&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="nx"&gt;ageTeller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns length of the function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The function's code property is an object that holds all that function's code that you wrote. It is not accessible publicly and it is stored in an internal property &lt;strong&gt;[[Code]]&lt;/strong&gt;. &lt;a href="http://www.ecma-international.org/ecma-262/5.1/#sec-13.2" rel="noopener noreferrer"&gt;Read more from the ecma-international.org&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;In other words, the code you wrote is not the function itself but sits in the code property of the function. A function is just a special object in JavaScript&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  TWO IMPORTANT TIPS:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;This code property of a function is invokable. &lt;em&gt;And this is how a function gets called or invoked in JavaScript.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;A function in JavaScript must not necessarily have a name. Hence we can create a function without giving it a name. In this case, that function is said to be &lt;em&gt;anonymous&lt;/em&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;anonymousAgeTeller&lt;/span&gt; &lt;span class="o"&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;age&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="s2"&gt;`my age is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;// A function without a name!?&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;es6AnonymousAgeTeller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`my age is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// An es6 arrow function without a name!?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  3. STATEMENTS, EXPRESSIONS, AND ANONYMOUS FUNCTIONS &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;The functions above do not have a name. Both are the same but the latter &lt;em&gt;es6AnonymousAgeTeller&lt;/em&gt;, uses modern JavaScript syntax. And that is what will be using as we go on.&lt;/p&gt;

&lt;p&gt;It is interesting to see that we are assigning a function expression to a variable &lt;em&gt;es6AnonymousAgeTeller&lt;/em&gt;. This is perfectly valid JavaScript and it opens the door for some very powerful coding patterns.&lt;/p&gt;

&lt;p&gt;An expression is a unit of code that returns a value.&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="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;// returns 4 &lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;// returns true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We can trap this returned value in a variable. Hence&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sumOfTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&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="nx"&gt;sumOfTwo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A statement, on the other hand, is a unit of code that does work. It does not return a value. &lt;strong&gt;Take Note.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sumOfTwo&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&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="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// a function statement does not return a value.&lt;/span&gt;
&lt;span class="c1"&gt;// A value is only returned when the function is invoked/called&lt;/span&gt;
&lt;span class="nf"&gt;sumOfTwo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;we cannot assign a statement to a variable because it does not return anything.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;the result is true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// wrong JavaScript code and should not be done!!!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;But we can do this instead:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sumOfTwo&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="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="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sumOfTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Above I wrote a function expression which is a function that does not have a name and because it is an expression it returns the reference of that function (its address in your computer's memory, at this time the function is not called so the reference is returned) and this is stored in the variable &lt;strong&gt;&lt;em&gt;sumOfTwo&lt;/em&gt;&lt;/strong&gt;. We can now invoke/call this function's code property using the &lt;strong&gt;&lt;em&gt;sumOfTwo&lt;/em&gt;&lt;/strong&gt; variable since this variable now holds a reference to the function in memory. Hence:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sumOfTwo&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// returns 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;These kinds of functions that do not have names in JavaScript are called Anonymous functions.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  4. FIRST-CLASS FUNCTIONS &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Anonymous functions can be stored in a variable, object, or array, passed as an argument to a function and can even be returned from a function. Hence they are called first-class functions or first-class object or as some call it first-class citizens in Javascript&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;In a nutshell, they can be treated and used as you would use any primitive JavaScript data type&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This makes JavaScript extremely powerful. Below are some examples to buttress this idea.&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;logItem&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function&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;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="nf"&gt;item&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;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="nx"&gt;item&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;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="nf"&gt;logItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// returns "Lawrence Eagles"&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="nf"&gt;logItem&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Earth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt; &lt;span class="c1"&gt;// returns {name : "Lawrence Eagles", location: "Earth"}&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="nf"&gt;logItem&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;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;I am a first-class citizen in JavaScript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}))&lt;/span&gt; &lt;span class="c1"&gt;// returns "I am a first-class citizen in JavaScript!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  Let's breakdown the function above.
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;The function name is &lt;em&gt;logItem&lt;/em&gt; and it takes a parameter called &lt;em&gt;item&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;we get the data type of the parameter using the &lt;strong&gt;&lt;em&gt;typeof operator&lt;/em&gt;&lt;/strong&gt;. The &lt;strong&gt;&lt;em&gt;typeof operator&lt;/em&gt;&lt;/strong&gt; returns a string indicating the type of the unevaluated operand.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;
&lt;span class="c1"&gt;// this is an expression so it returns a value depending on the data type of item.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;We take that returned data type and check if it is equal to  "function"
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;span class="c1"&gt;// take note typeof returns it's result as string. so we check if the result is equal to a "function".&lt;/span&gt;
&lt;span class="c1"&gt;// This is another expression and it would return true or false in this case.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If &lt;em&gt;true&lt;/em&gt;, we know that a &lt;strong&gt;&lt;em&gt;first-class anonymous function&lt;/em&gt;&lt;/strong&gt; was passed and its reference would now be stored in the &lt;em&gt;logItem function's parameter&lt;/em&gt;. So we call that first-class function using the &lt;em&gt;logItem parameter&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// item is going to hold any argument passed to the function. &lt;/span&gt;
&lt;span class="c1"&gt;// If a primitive is passed it would hold that primitive but if a function is passed it would hold a reference to the function in memory. &lt;/span&gt;
&lt;span class="c1"&gt;// So we can invoke the code property of that function using this parameter.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;If the data type is not a function we log that item to the console.

&lt;code&gt;console.log(item)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also Read:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/lawrence_eagles" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35543%2Fc9e1d142-e674-4d5d-8e92-f745d124cf0a.jpg" alt="lawrence_eagles"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/lawrence_eagles/demystifying-hoisting-in-javascript-4kcj" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;DEMYSTIFYING HOISTING IN JAVASCRIPT&lt;/h2&gt;
      &lt;h3&gt;Lawrence Eagles ・ Apr 4 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#tutorial&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;h4&gt;
  
  
  5. CLOSING THOUGHTS &lt;a&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This concept of first-class functions opens up a whole new programming paradigm in JavaScript called &lt;strong&gt;&lt;em&gt;functional programming&lt;/em&gt;&lt;/strong&gt;. This gives JavaScript superpowers and makes it a very good language for functional programming. I do hope you got a thing or two from this article and I am very much looking forward to your additions or questions in the comment section below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>HOW TO PROPERLY SET INITIAL STATE IN REDUX</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Sat, 11 Apr 2020 10:48:57 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/how-to-properly-set-initial-state-in-redux-78m</link>
      <guid>https://dev.to/lawrence_eagles/how-to-properly-set-initial-state-in-redux-78m</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Initializing State&lt;/li&gt;
&lt;li&gt;createStore Pattern&lt;/li&gt;
&lt;li&gt;Reducer Pattern&lt;/li&gt;
&lt;li&gt;Closing Thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. Initializing State
&lt;/h4&gt;

&lt;p&gt;In Redux, all your application state is held in the store; which is an object that holds the complete state tree of your app. There is only one way to change its state and that is by dispatching actions.&lt;br&gt;
Actions are objects that consist of a type and a payload property. They are created and dispatched by special functions called action creators.&lt;br&gt;
see a small contrived and detailed example below:&lt;br&gt;
First creating the Redux store:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createStore&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;redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;todosReducer&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="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADD_TODO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&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;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todosReducer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Next updating the store&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ADD_TODO&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add_todo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// creates the action type&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blog on dev.to&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;todoActionCreator&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTodo&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;action&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;ADD_TODO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Like I said my examples are small and contrived and are aimed at clearing the air, around the aspect of Redux related to our discussion. So, kindly see them as a refresher. I am assuming you already have some knowledge of Redux and are familiar with some patterns in a React Redux environment.&lt;/em&gt; &lt;br&gt;
&lt;strong&gt;&lt;em&gt;However regardless of your professional experience, I would not be surprised if you find a hoe on these premises because too many people know too much but actually know nothing at all. &lt;a href="https://dev.to/lawrence_eagles/demystifying-hoisting-in-javascript-4kcj"&gt;Like in my old article on hoisting in JavaScript&lt;/a&gt;, a lot of developers where making thunderclaps around the web about hoisting, but they were all explaining the concept wrongly.&lt;br&gt;
So my advice is kindly to take it and weed around your own premises. If you do I am sure you would come out with a better understanding of React and Redux.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
When a store is created, Redux dispatches a dummy action to your reducer to populate the store with the initial state. You are not meant to handle the dummy action directly. Just remember that your reducer should return some kind of initial state if the state given to it as the first argument is undefined.&lt;br&gt;
But you don't want your initial application state to be undefined, so you have to initialize the state yourself. There are two ways or patterns to do this viz: the createStore pattern and the reducers pattern.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. createStore Pattern
&lt;/h4&gt;

&lt;p&gt;The createStore method can accept an optional preloadedState value as its second argument. In our example, we called createStore without passing this value. When a value is passed to the preloadedState it becomes the initial state.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="c1"&gt;// in a real-world, is way better to initialize state with {} (an object) instead of an array.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todosReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;let's say we have a compulsory todo list for everyone and we can add new tasks to this list later. In this case, we would initialize state like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;eat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;code&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sleep&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// compulsory todo list&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todosReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;
&lt;h4&gt;
  
  
  3. Reducer Pattern
&lt;/h4&gt;

&lt;p&gt;Reducers can also specify an initial state value by looking for an incoming state argument that is undefined, and returning the value they'd like to use as a default. In our example above our todoReducer already does this.&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;todosReducer&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="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ADD_TODO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// sets initial state to []. But would only take effect if the initial state is undefined, which means it was not set using createStore().&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;But there is a drawback to this method. In our contrived example, it can be great but what of in a large application where we have about 10 or 20 reducers and we want to initialize state not just with an empty array or object literal, but with some data. It would take a lot of repetition to get this done with the reducers and it is going to be tedious if we decide to change that initial state data at some point. &lt;br&gt;
That's a really boring thing todo:&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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1586602018%2Fboring_dcg3ik.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1586602018%2Fboring_dcg3ik.png" alt="alt a boring person"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Closing Thoughts:
&lt;/h4&gt;

&lt;p&gt;You might be wondering which method is best to use. I have already pointed out a drawback with the reducer pattern. &lt;br&gt;
&lt;strong&gt;&lt;em&gt;In general, preloadedState wins over the state specified by the reducer. This lets reducers specify initial data that makes sense to them as default arguments, but also allows loading existing data (fully or partially) when you're hydrating the store from some persistent storage or the server.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Also Note:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Reducers whose initial state is populated using preloadedState will still need to provide a default value to handle when passed a state of undefined. All reducers are passed undefined on initialization, so they should be written such that when given undefined, some value should be returned. This can be any non-undefined value; there's no need to duplicate the section of preloadedState here as the default&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I have seen developers use both methods but as touching the drawback I noted about using reducers, I would love to hear your opinions and experiences in the comment section below.&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>DEMYSTIFYING HOISTING IN JAVASCRIPT</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Sat, 04 Apr 2020 10:15:38 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/demystifying-hoisting-in-javascript-4kcj</link>
      <guid>https://dev.to/lawrence_eagles/demystifying-hoisting-in-javascript-4kcj</guid>
      <description>&lt;h2&gt;
  
  
  Table of content contents:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Hoisting and the general misconception in the JavaScript world&lt;/li&gt;
&lt;li&gt;How JavaScript works under the Hood&lt;/li&gt;
&lt;li&gt;The true concept of hoisting&lt;/li&gt;
&lt;li&gt;The dangers of hoisting&lt;/li&gt;
&lt;li&gt;Closing thoughts&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  1. HOISTING AND THE GENERAL MISCONCEPTION IN THE JAVASCRIPT WORLD
&lt;/h4&gt;

&lt;h5&gt;
  
  
  What is Hoisting in Javascript?
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;Hoisting is a JavaScript’s default behavior of moving declarations to the top.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The above definition of hoisting has spread like wildfire throughout the internet and the JavaScript community. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HOWEVER, THIS IS JUST A HUGE MISCONCEPTION ABOUT HOISTING&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;As a JavaScript developer, it is important you truly understand hoisting, not only because it gives you a better understanding of the language but also because this can easily earn or cost you a point during an interview section.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Kindly note, I would be using JS and JavaScript interchangeably in this article.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  HOISTING HEAD FIRST
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// calls function&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="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs coderName to the console&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declares the coderName variable&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logName&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// declares the logName function&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;Brendan Eich&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, you would certainly believe the above definition (&lt;em&gt;rather misconception&lt;/em&gt;) Is correct.&lt;/p&gt;

&lt;p&gt;However, let us debunk this as we demystify hoisting by digesting these examples. &lt;br&gt;
The above code examples would output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns Brendan Eich &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="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key to understanding this, is the result of logging the variable. The function call most certainly behaves as if it was moved up. &lt;br&gt;
In which case the processed code would be:&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;var&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declares the coderName variable&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logName&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// declares the logName function&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;Brendan Eich&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="nf"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// calls function&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="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs coderName to the console&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if this was the case the result should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// logName() returns Brendan Eich&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// console.log(coderName) returns Lawrence Eagles (not undefined)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To understand what is happening here we need to understand how JavaScript really works.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. HOW JAVASCRIPT WORKS UNDER THE HOOD
&lt;/h4&gt;

&lt;p&gt;We have established a fact from our small contrived example above that if the variable and functions were indeed moved &lt;em&gt;to the top&lt;/em&gt; by the JS engine then the results should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// logName() returns Brendan Eich&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// console.log(coderName) returns Lawrence Eagles&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns Brendan Eich&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="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The key thing to note here is that the hoisted version of the code is not behaving as if the variable was &lt;em&gt;moved to the top&lt;/em&gt; but rather as if it was declared without a value. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hence it behaves as if the code was:&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;var&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declares the coderName variable without a value&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logName&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// declares the logName function&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;Brendan Eich&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="nf"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// calls function&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="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs coderName to the console&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here indeed we get a result consistent with the hoisted version of the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// returns Brendan Eich&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="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  HOW JAVASCRIPT WORK UNDER THE HOOD.
&lt;/h5&gt;

&lt;p&gt;When the JS engine processes your code, it creates something called the execution context. This is a wrapper around your currently running code. It consists of the global variable, the &lt;strong&gt;&lt;em&gt;this&lt;/em&gt;&lt;/strong&gt; object (&lt;em&gt;you may know it as this keyword&lt;/em&gt;), and the variable environment of that execution context.&lt;br&gt;
We would not be going in-depth into the execution context but we will dwell sufficiently on it, for here lies the buried treasure.&lt;/p&gt;

&lt;p&gt;There are two processes involved in the creation of the execution context viz:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The creation phase: During this phase, the variables and the functions are added to memory. Here the JS engine goes through your code line by line and adds all your variables to your computer's memory &lt;strong&gt;but it does not assign them values just yet&lt;/strong&gt;. The functions, however, are added to memory in their entirety. That is the whole function (name and code block) is added to memory in this phase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second phase is the execution phase: During this phase values are assigned to variables and functions are called. so even if you initialize a variable with a value it is in this second phase that that value is assigned to it. In the first phase, the value is not assigned to the variable. It is added to memory and initialized with &lt;em&gt;undefined&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  3. THE TRUE CONCEPT OF HOISTING
&lt;/h4&gt;

&lt;p&gt;In case you don't already see it, the whole misconception occurs because of the first (creation) phase of the execution context. Before the functions are eventually executed they are already in memory during the creation phase of the execution context so the Javascript engine knows where that function, in its entirety sits in memory. &lt;strong&gt;It did not move it to the top.&lt;/strong&gt;&lt;br&gt;
Hence:&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;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logName&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// declares the logName function&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;Brendan Eich&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// returns Brendan Eich&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Works correctly as if the function declaration was moved to the top. But that is not the case. The function in its entirety was already added to memory during the creation phase of the execution context. So during the execution phase, the JS engine already knows where the function sits in memory and calls it.&lt;/p&gt;

&lt;h5&gt;
  
  
  What about variables?
&lt;/h5&gt;

&lt;p&gt;The process is the same but the implementation is a little bit different. Like I noted above the variables are also added in memory during the creation phase of the execution context but no values are assigned to them. In javascript when a variable is declared without value the JS engine automatically adds a placeholder value to it which is undefined.&lt;br&gt;
This explains why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs coderName to the console&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declares the coderName variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&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;var&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declares the coderName variable without a value&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="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs coderName to the console&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;have the same result: &lt;strong&gt;undefined&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;For this same reason anonymous functions assigned to variables are not hoisted&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  UNDEFINED IN JAVASCRIPT
&lt;/h5&gt;

&lt;p&gt;Undefined is a primitive type in JavaScript just like String, Boolean, and Numbers. It means you have not explicitly assign any value to that variable. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined" rel="noopener noreferrer"&gt;further you reading at MDN&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Take note it is not an error&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Take note it is not null&lt;/strong&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null" rel="noopener noreferrer"&gt;Read about null here&lt;/a&gt;&lt;br&gt;
We can make this point clearer by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;without declaring the variable. That is without any of the codes below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would throw an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// returns VM275:1 Uncaught ReferenceError: coderName is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note this is different from undefined. This is an error telling you that you are trying to log a variable that is not defined anywhere. The JavaScript interpreter is saying I did not see that variable at all in memory.&lt;br&gt;
However, in the case of hoisting the variable is in memory but because it is put there during the creation phase of the execution context, the JS engine assigns a special value to it which is called undefined. &lt;br&gt;
&lt;strong&gt;It is this whole process that is confused and misunderstood by many who think that the JS engine actually moves the variable or function declaration to the top for them.&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  4. THE DANGERS OF HOISTING
&lt;/h4&gt;

&lt;p&gt;Hoisting offers a false security. Many developers believe that the JS engine is going to move their function and variable declarations up and so consciously write bad codes. But in reality, the JavaScript engine does not do that, we can now clearly see that the JavaScript engine is only making decisions with our codes based on how it has been implemented. This can be very dangerous because one can get &lt;strong&gt;undefined&lt;/strong&gt; when a real value is expected and this can be a very nasty bug, so difficult to track that I am sure you won't like the picture by the time you finish debugging. &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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1586004368%2Ferrors_image_g4xwik.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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1586004368%2Ferrors_image_g4xwik.png" alt="alt Stressed programmer"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Pro Tip: Think twice and code once, save yourself the debugging stress&lt;/em&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  MODERN JAVASCRIPT AND HOISTING
&lt;/h5&gt;

&lt;p&gt;From es6 and above there are new ways to declare variables in JavaScript, which makes the language now safer as both &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt; (&lt;em&gt;the new ways to declare variables in Javascript&lt;/em&gt;) do not support hoisting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs coderName to the console&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declares the coderName variable&lt;/span&gt;
&lt;span class="c1"&gt;// returns VM269:1 Uncaught ReferenceError: coderName is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coderName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs coderName to the console&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;coderName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lawrence Eagles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// declares the coderName variable&lt;/span&gt;
&lt;span class="c1"&gt;// returns VM397:1 Uncaught ReferenceError: Cannot access 'coderName' before initialization&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;would not work. They both throw errors. You are hereby encouraged to write good codes.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. CLOSING THOUGHTS
&lt;/h4&gt;

&lt;p&gt;I do hope you learned a thing or two from reading this. I would also like to hear from you in the comment section below.&lt;br&gt;
Kindly visit MDN for &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="noopener noreferrer"&gt;further your reading on hoisting&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Causes of Heroku H10-App Crashed Error And How To Solve Them</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Sat, 12 Oct 2019 05:22:16 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/causes-of-heroku-h10-app-crashed-error-and-how-to-solve-them-3jnl</link>
      <guid>https://dev.to/lawrence_eagles/causes-of-heroku-h10-app-crashed-error-and-how-to-solve-them-3jnl</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Programming is bittersweet.&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
&lt;em&gt;It is very tedious and many a time, it can leave developers very frustrated with little zeal for another &lt;code&gt;keypress&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;However, developers are always having a whale of a time while coding, and they are full of pride and joy, once all their codes run successfully without errors.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That being said no one wants to be frustrated while coding hence no one wants bugs in their codes.&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%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1570852852%2FDEV.TO%2Fbug_feature_oza9nk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdrquzbncy%2Fimage%2Fupload%2Fv1570852852%2FDEV.TO%2Fbug_feature_oza9nk.jpg" title="Bug in my code? it is feature" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Developers, often do not get it all right ("&lt;em&gt;code-wise&lt;/em&gt;") at first try; consequently, the software development cycle involves debugging, refactoring, upgrading, and many other enhancement operations. &lt;br&gt;
In addition, developers know that many a time we all have to deal with bugs in our code. Hence debugging is an invaluable skill in a developer's repertoire. And to be successful at debugging, we need a good understanding of error messages. &lt;/p&gt;

&lt;p&gt;Error codes or messages are the ways computers inform us that there are bugs in our codes, and sometimes it tells us where they are. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The paradox is that error codes; although are very helpful, they are not plain English and can be very confusing and cryptic&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Below are error codes from a crashed app on Heroku.&lt;/p&gt;

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

2019-10-11T11:27:28.019054+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=realtylabapp.herokuapp.com request_id=74165d9f-db2a-46bd-ab9c-1a01403bd00f fwd="129.205.113.108" dyno= connect= service= status=503 bytes= protocol=https
2019-10-11T11:27:29.919409+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=realtylabapp.herokuapp.com request_id=fa48efa2-4ddf-41e6-a633-a62cb02314bd fwd="129.205.113.108" dyno= connect= service= status=503 bytes= protocol=https


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

&lt;/div&gt;

&lt;p&gt;Heroku uses a unique alphanumeric error code combo to communicate to the developer the kind of error that was found in their code. &lt;/p&gt;

&lt;p&gt;This alphanumeric error code ranges from:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;H10&lt;/code&gt; to &lt;code&gt;H99&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;R10&lt;/code&gt; to &lt;code&gt;R99&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;L10&lt;/code&gt; to &lt;code&gt;L15&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get a detailed list here: &lt;a href="https://devcenter.heroku.com/articles/error-codes#h10-app-crashed" rel="noopener noreferrer"&gt;Complete Heroku Error Codes &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article is focused on the &lt;code&gt;Heroku H10-App crashed&lt;/code&gt; error code which is thrown when an application crashes on Heroku. This is as much as it goes. It does not help us with the reason our app crashed and that's where this article comes in. Below are some causes of this error and I guarantee you that some of them would surprise you greatly!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bug in Procfile&lt;/strong&gt;&lt;br&gt;
A very interesting discovery for me. A bug in your Procfile can crash your app. If your Procfile is pointing to the wrong server file. e.g If your server is in server.js and your Procfile points to app.js this would definitely crash your app and Heroku would greet you with the H10-App crashed error code message. &lt;br&gt;
Secondly, a buggy Procfile can also come in the form of &lt;em&gt;wrong spacing&lt;/em&gt;. e.g &lt;br&gt;&lt;strong&gt;Wrong:&lt;/strong&gt; web : node index.js &lt;br&gt;
&lt;strong&gt;Correct&lt;/strong&gt; web: node index.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Setting a PORT as a Heroku environment variable&lt;/strong&gt;&lt;br&gt;
This would surely crash your app. Heroku automatically sets a Port that can be accessed via &lt;code&gt;process.env.PORT&lt;/code&gt;. Setting a port yourself would crash your app. Surprisingly, the command &lt;code&gt;heroku config&lt;/code&gt; does not display the preset Heroku port so one might be tempted to set another port as an environment variable. &lt;br&gt;
To see all the preset Heroku environment variables, use the command &lt;code&gt;heroku run printenv&lt;/code&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Missing Required Environment Variable&lt;/strong&gt; while setting a port would cause this error because Heroku already sets a port internally, failing to set any required environment variable (e.g your database), would prompt Heroku to greet you with this error.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Missing Required Scripts&lt;/strong&gt;&lt;br&gt;
This error is thrown in a Node.js environment if you forget to set a &lt;code&gt;start script&lt;/code&gt;. Heroku uses this script to start your app so if it is missing, it would throw an &lt;code&gt;H10-App crashed&lt;/code&gt; error code message.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This can be solved by setting a &lt;code&gt;start script&lt;/code&gt; in the &lt;code&gt;package.json&lt;/code&gt;. e.g&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node index.js&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;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
If none of the above solved your problem, you can make a last-ditch attempt by updating all your packages. If this doesn't help and you are in a &lt;code&gt;Node.js&lt;/code&gt; environment, your last resort would be setting a &lt;code&gt;node version&lt;/code&gt; in the engine section of your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myapp&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a really cool app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1.0.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;engines&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;12.11.1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The Unthinkable&lt;/strong&gt;&lt;br&gt;
Lastly, if your bug defiles all solution, and by this time you are grasping at straws; I leave you in the able hands of &lt;code&gt;heroku restart&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let me know if you have experienced any other cause of this error in the comment below.&lt;/p&gt;

</description>
      <category>heroku</category>
      <category>node</category>
      <category>webdev</category>
      <category>devops</category>
    </item>
    <item>
      <title>How a broken love relationship between PHP giants Wordpress &amp; Facebook helped save react.js</title>
      <dc:creator>Lawrence Eagles</dc:creator>
      <pubDate>Tue, 17 Oct 2017 14:12:23 +0000</pubDate>
      <link>https://dev.to/lawrence_eagles/how-broken-love-relationship-between-php-giants-wordpress--facebook-helped-saved-reactjs-single-glitch-1k</link>
      <guid>https://dev.to/lawrence_eagles/how-broken-love-relationship-between-php-giants-wordpress--facebook-helped-saved-reactjs-single-glitch-1k</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ffjk0qvq2ch753huhyxia.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%2Ffjk0qvq2ch753huhyxia.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Facebook's industry leading javascript library, react js, is no doubt a game changer in the world of front-end development. A ground breaking awesome product, track blazer that shipped with myriads of innovation; think JSX, component based approach, virtual DOM, mobile app development with react native, to name a few.&lt;/p&gt;

&lt;p&gt;The simplicity, performance improvements and developer goodies brought by react were so ground breaking that it made its predecessors a story of the past, that developers are happy to forget.&lt;br&gt;
The likes of Backbone.js, Knockout.js, Ember.js are now old stories, developer are willing to tell.&lt;/p&gt;

&lt;p&gt;And Angular Js, react's single, biggest competitor, backed by IT giants google, was forced to do a complete rewrite, adopting reacts components based approach and mirroring several other best innovations from react, in other to stay relevant.&lt;br&gt;
&lt;a href="https://news.ycombinator.com/item?id=8507833" rel="noopener noreferrer"&gt;https://news.ycombinator.com/item?id=8507833&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But despite all these react.js is still taking other the world: &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%2Fogey90hpet6q3ueqqppo.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%2Fogey90hpet6q3ueqqppo.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, with all of this react goodness; as it is with everything created by man, react has a glitch. No! Not in the library itself, but yes! In its licensing. This seems to be a major object of concern that may sometimes force developers, startup and companies to think twice before adopting the library into their technology stack.&lt;br&gt;
This is so because Facebook chose to use a BSD-derived license that contained some &lt;a href="https://thenextweb.com/dd/2017/09/19/should-developers-be-afraid-of-zuckerbergs-bearing-gifts/" rel="noopener noreferrer"&gt;troubling litigation issue&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm not a legal practitioner but if you where to consider the number of &lt;a href="https://github.com/facebook/react/issues/10191" rel="noopener noreferrer"&gt;github issues raised on facebook's react.js licensing&lt;/a&gt; and do some reading on &lt;a href="https://code.facebook.com/posts/112130496157735/explaining-react-s-license/" rel="noopener noreferrer"&gt;the react licensing&lt;/a&gt;, added with the the number of developer backlash on this issue already, I don't know about you bro, but my eyebrows are raised!&lt;/p&gt;

&lt;p&gt;A good read on this issue: &lt;a href="https://hackernoon.com/facebooks-bsd-patents-license-and-how-it-affects-you-66088e052845" rel="noopener noreferrer"&gt;https://hackernoon.com/facebooks-bsd-patents-license-and-how-it-affects-you-66088e052845&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following the licensing one would have to double check to know if he is about to shoot himself in the foot or not, because react.js was looking, something like a diamond with a rotten core. Well that would be, if you manage to run into the licensing issue. But why the unnecessary risk?&lt;/p&gt;

&lt;p&gt;Regardless of all the pressures, Facebook however has refused to change the BSD+Patents open source license used on react.js. Not even when a company in the caliber of apache, announced that Facebook's BSD+Patents open source license has been disallowed for inclusion with Apache products. &lt;br&gt;
Facebook appeared nothing dauntless.&lt;/p&gt;

&lt;p&gt;It is therefore a shockingly, happy moment when you read that Facebook has now agreed to re-license not only react.js but several other of its open source products.  viz: Jest, Flow, and Immutable.js&lt;br&gt;
&lt;a href="https://code.facebook.com/posts/300798627056246/relicensing-react-jest-flow-and-immutable-js/" rel="noopener noreferrer"&gt;https://code.facebook.com/posts/300798627056246/relicensing-react-jest-flow-and-immutable-js/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wow! What could have moved this mountain?&lt;/p&gt;

&lt;p&gt;I think of no other reason but the announcement of wordpress dropping react; not only on Gutenberg (their new project) but also on calypso (wordpress.com). &lt;br&gt;
Curling from mat mullenweg's blog: &lt;a href="https://ma.tt/" rel="noopener noreferrer"&gt;https://ma.tt/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;"I am surprised and excited to see the news that Facebook is going to drop the patent clause that I wrote about last week. They’ve announced that with React 16 the license will just be regular MIT with no patent addition."&lt;/p&gt;

&lt;p&gt;And more after considering react's licensing:&lt;/p&gt;

&lt;blockquote&gt;"I'm here to say that the Gutenberg team is going to take a step back and rewrite Gutenberg using a different library. It will likely delay Gutenberg at least a few weeks, and may push the release into next year.&lt;/blockquote&gt;

&lt;blockquote&gt;Automattic will also use whatever we choose for Gutenberg to rewrite Calypso – that will take a lot longer, and Automattic still has no issue with the patents clause, but the long-term consistency with core is worth more than a short-term hit to Automattic’s business from a rewrite. Core WordPress updates go out to over a quarter of all websites, having them all inherit the patents clause isn’t something I’m comfortable with."&lt;/blockquote&gt;

&lt;p&gt;For Facebook, wordpress adopting react.js for the wordpress.com dashboard named calypso is a big coup for react.js. Considering wordpress owns about 28% of the web right now, and the viral talk of wordpress adopting React js &amp;amp; Node.js fully; after successfully using in developing calypso, was beginning to become popular among JS developers, who would rather use twig to avoid writing that ugly PHP syntax, when developing with wordpress.&lt;/p&gt;

&lt;p&gt;A collapse of the romance between these two PHP giants would have been too much for Facebook to handle. The loss of 27% of the web and the possibility of many other companies following suite, with Vue.js now playing catch-up, I don't care about Facebook's reason of changing the licensing, we found it here :-). &lt;/p&gt;

&lt;p&gt;Although Facebook's reason for changing the license was given by their engineering director Adam Wolff, quoting him:&lt;/p&gt;

&lt;blockquote&gt;“React is the foundation of a broad ecosystem of open source software for the web, and we don’t want to hold back forward progress for nontechnical reasons.”&lt;/blockquote&gt;

&lt;p&gt;I don't know about you, but as a developer who believes wordpress and not Facebook saved PHP, I certainly believe, wordpress, dropping react in all their projects was instrumental in Facebook changing their licensing. &lt;br&gt;
Not suggesting wordpress alone, but every single backlash and pressure, down to the wordpress moment of course was instrumental. However, I believe the wordpress blow just pushes the pressure past the threshold level. And of course the irrevocable has been revoked.&lt;/p&gt;

&lt;p&gt;Congratulations Facebook, Thank you wordpress, apache and all other developers who spoke out. &lt;/p&gt;

&lt;p&gt;Wow! Now we can use our favourite front-end development tool without an iota of worry in our hearts...&lt;/p&gt;

</description>
      <category>react</category>
      <category>discuss</category>
      <category>watercooler</category>
      <category>wordpress</category>
    </item>
  </channel>
</rss>
