<?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: arnav-aggarwal</title>
    <description>The latest articles on DEV Community by arnav-aggarwal (@arnavaggarwal).</description>
    <link>https://dev.to/arnavaggarwal</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%2F29946%2Ff76c2f74-aadc-46ae-a590-1dc9f9506d77.png</url>
      <title>DEV Community: arnav-aggarwal</title>
      <link>https://dev.to/arnavaggarwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arnavaggarwal"/>
    <language>en</language>
    <item>
      <title>10 JavaScript concepts you need to know for interviews</title>
      <dc:creator>arnav-aggarwal</dc:creator>
      <pubDate>Mon, 21 Aug 2017 18:17:45 +0000</pubDate>
      <link>https://dev.to/arnavaggarwal/10-javascript-concepts-you-need-to-know-for-interviews</link>
      <guid>https://dev.to/arnavaggarwal/10-javascript-concepts-you-need-to-know-for-interviews</guid>
      <description>&lt;h3&gt;
  
  
  Self-Learning
&lt;/h3&gt;

&lt;p&gt;There are thousands of people learning JavaScript and web development in the hopes of getting a job. Often, self-learning leaves gaps in people’s understanding of the JavaScript language itself.&lt;/p&gt;

&lt;p&gt;It’s actually surprising how little of the language is needed to make complex web pages. People making entire sites on their own often don’t have a good grasp of the fundamentals of JavaScript.&lt;/p&gt;

&lt;p&gt;It’s rather easy to avoid the complex topics and implement features using basic skills. It’s also easy to create a website by relying on Stack Overflow without understanding the code being copied.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you’re looking to master JavaScript interviews, check out &lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal"&gt;Step Up Your JS: A Comprehensive Guide to Intermediate JavaScript&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Interviews
&lt;/h3&gt;

&lt;p&gt;The problem is that questions testing your understanding of JS are exactly what many tech companies ask in their interviews. It becomes clear very quickly when an applicant knows just enough to have scraped by, but doesn’t have a solid understanding the language.&lt;/p&gt;

&lt;p&gt;Here are concepts that are frequently asked about in web development interviews. This is assuming you already know the basics such as loops, functions, and callbacks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concepts
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://www.educative.io/collection/page/5679346740101120/5707702298738688/5685265389584384/"&gt;Value vs. Reference&lt;/a&gt; — Understand how objects, arrays, and functions are copied and passed into functions. Know that the reference is what's being copied. Understand that primitives are copied and passed by copying the value.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://scotch.io/tutorials/understanding-scope-in-javascript#toc-scope-in-javascript"&gt;Scope&lt;/a&gt; — Understand the difference between global scope, function scope, and block scope. Understand which variables are available where. Know how the JavaScript engine performs a variable lookup.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/"&gt;Hoisting&lt;/a&gt; — Understand that variable and function declarations are hoisted to the top of their available scope. Understand that function expressions are not hoisted.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://javascriptissexy.com/understand-javascript-closures-with-ease/"&gt;Closures&lt;/a&gt; — Know that a function retains access to the scope that it was created in. Know what this lets us do, such as data hiding, memoization, and dynamic function generation.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.educative.io/collection/page/5679346740101120/5707702298738688/5676830073815040"&gt;&lt;code&gt;this&lt;/code&gt;&lt;/a&gt; — Know the rules of &lt;code&gt;this&lt;/code&gt; binding. Know how it works, know how to figure out what it will be equal to in a function, and know why it’s useful.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://codeburst.io/javascripts-new-keyword-explained-as-simply-as-possible-fec0d87b2741"&gt;&lt;code&gt;new&lt;/code&gt;&lt;/a&gt; — Know how it relates to object oriented programming. Know what happens to a function called with &lt;code&gt;new&lt;/code&gt;. Understand how the object generated by using &lt;code&gt;new&lt;/code&gt; &lt;em&gt;inherits&lt;/em&gt; from the function’s &lt;code&gt;prototype&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://codeplanet.io/javascript-apply-vs-call-vs-bind/"&gt;&lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt;, &lt;code&gt;bind&lt;/code&gt;&lt;/a&gt; — Know how each of these functions work. Know how to use them. Know what they do to &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://codeburst.io/master-javascript-prototypes-inheritance-d0a9a5a75c4e"&gt;Prototypes &amp;amp; Inheritance&lt;/a&gt; — Understand that inheritance in JavaScript works through the &lt;code&gt;[[Prototype]]&lt;/code&gt; chain. Understand how to set up inheritance through functions and objects and how &lt;code&gt;new&lt;/code&gt; helps us implement it. Know what the &lt;code&gt;__proto__&lt;/code&gt; and &lt;code&gt;prototype&lt;/code&gt; properties are and what they do.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ&amp;amp;t=948s"&gt;Asynchronous JS&lt;/a&gt; — Understand the event loop. Understand how the browser deals with user input, web requests, and events in general. Know how to recognize and correctly implement asynchronous code. Understand how JavaScript is both asynchronous and single-threaded.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.sitepoint.com/higher-order-functions-javascript/"&gt;Higher Order Functions&lt;/a&gt; — Understand that functions are first-class objects in JavaScript and what that means. Know that returning a function from another function is perfectly legal. Understand the techniques that closures and higher order functions allow us to use.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  More Resources
&lt;/h3&gt;

&lt;p&gt;If the links included aren’t enough, there are countless resources out there to help you learn these concepts.&lt;/p&gt;

&lt;p&gt;I personally created &lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal"&gt;Step Up Your JS: A Comprehensive Guide to Intermediate JavaScript&lt;/a&gt; to help developers advance their knowledge. It covers all of these concepts and many more.&lt;/p&gt;

&lt;p&gt;Here are resources which I’ve read or watched at least some of and can recommend.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/getify/You-Dont-Know-JS"&gt;You Don’t Know JS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://javascriptissexy.com/16-javascript-concepts-you-must-know-well/"&gt;JavaScript is Sexy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.javascript.com/resources"&gt;javascript.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://frontendmasters.com/"&gt;Frontend Masters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://eloquentjavascript.net/"&gt;Eloquent JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good luck on your interviews.&lt;/p&gt;

&lt;h4&gt;
  
  
  If you found this useful, please give it a clap below so others see it as well.
&lt;/h4&gt;

&lt;p&gt;Feel free to check out some of my recent work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal"&gt;Step Up Your JS: A Comprehensive Guide to Intermediate JavaScript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codeburst.io/what-i-learned-from-attending-a-coding-bootcamp-and-teaching-another-one-65addec715fd"&gt;What I learned from attending a coding bootcamp and teaching another one&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codeburst.io/react-ecosystem-setup-step-by-step-walkthrough-721ff45a7fc1"&gt;React Ecosystem Setup — Step-By-Step Walkthrough&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Master JavaScript Prototypes &amp; Inheritance</title>
      <dc:creator>arnav-aggarwal</dc:creator>
      <pubDate>Fri, 18 Aug 2017 04:12:24 +0000</pubDate>
      <link>https://dev.to/arnavaggarwal/master-javascript-prototypes--inheritance</link>
      <guid>https://dev.to/arnavaggarwal/master-javascript-prototypes--inheritance</guid>
      <description>&lt;p&gt;This article is taken from my course, &lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal" rel="noopener noreferrer"&gt;Step Up Your JS: A Comprehensive Guide to Intermediate JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Inheritance refers to an object’s ability to access methods and other properties from another object. Objects can &lt;em&gt;inherit&lt;/em&gt; things from other objects. Inheritance in JavaScript works through something called prototypes and this form of inheritance is often called &lt;em&gt;prototypal inheritance&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we’ll cover a lot of seemingly unrelated topics and tie them together at the end. There’s also a TL;DR at the end for those who want the short version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object, Array, and Function
&lt;/h2&gt;

&lt;p&gt;JavaScript gives us access to three global functions: &lt;code&gt;Object&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, and &lt;code&gt;Function&lt;/code&gt;. Yes, these are all functions.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(Object); // -&amp;gt; ƒ Object() { [native code] }
console.log(Array); // -&amp;gt; ƒ Array() { [native code] }
console.log(Function); // -&amp;gt; ƒ Function() { [native code] }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You don’t know it, but every time you create an object literal, the JavaScript engine is effectively calling &lt;code&gt;new Object()&lt;/code&gt;. An object literal is an object created by writing &lt;code&gt;{}&lt;/code&gt;, as in &lt;code&gt;var obj = {};&lt;/code&gt;. So an object literal is an implicit call to &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Same goes for arrays and functions. We can think of an array as coming from the &lt;code&gt;Array&lt;/code&gt; constructor and a function as coming from the &lt;code&gt;Function&lt;/code&gt; constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object Prototypes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  __proto__
&lt;/h3&gt;

&lt;p&gt;All JavaScript objects have a prototype. Browsers implement prototypes through the &lt;code&gt;__proto__&lt;/code&gt; property and this is how we’ll refer to it. This is often called the &lt;em&gt;dunder proto&lt;/em&gt;, short for double underscore prototype. Don’t EVER reassign this property or use it directly. The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto" rel="noopener noreferrer"&gt;MDN page&lt;/a&gt; for &lt;code&gt;__proto__&lt;/code&gt; warns us in big red blocks to never do this.&lt;/p&gt;

&lt;h3&gt;
  
  
  prototype
&lt;/h3&gt;

&lt;p&gt;Functions also have a &lt;code&gt;prototype&lt;/code&gt; property. This is distinct from their &lt;code&gt;__proto__&lt;/code&gt; property. This makes discussion rather confusing, so I’ll spell out the syntax I’ll be using. When I refer to a prototype and the word “prototype isn’t highlighted grey, I’m referring to the &lt;code&gt;__proto__&lt;/code&gt; property. When I use &lt;code&gt;prototype&lt;/code&gt; in grey, I’m talking about a function’s &lt;code&gt;prototype&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;If we were to log the &lt;code&gt;prototype&lt;/code&gt; of an object in Chrome, this is what we’d see.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var obj = {};
console.log(obj.__proto__);
// -&amp;gt; {constructor: ƒ, __defineGetter__: ƒ, …}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;__proto__&lt;/code&gt; property is a &lt;a href="https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0" rel="noopener noreferrer"&gt;reference&lt;/a&gt; to another object that has several properties on it. Every object literal we create has this &lt;code&gt;__proto__&lt;/code&gt; property pointing to this same object.&lt;/p&gt;

&lt;p&gt;There are a couple of important points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;__proto__&lt;/code&gt; of an object literal is equal to Object.prototype&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;__proto__&lt;/code&gt; of Object.prototype is null&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ll explain why soon.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Prototype Chain
&lt;/h2&gt;

&lt;p&gt;To understand object prototypes, we need to discuss object lookup behavior. When we look for a property of an object, the JavaScript engine will first check the object itself for the existence of the property. If not found, it’ll go to the object’s prototype and check that object. If found, it’ll use that property.&lt;/p&gt;

&lt;p&gt;If not found, it’ll go to the prototype’s prototype, and on and on until it finds an object with a &lt;code&gt;__proto__&lt;/code&gt; property equal to &lt;code&gt;null&lt;/code&gt;. So if we were to attempt to look up the property &lt;code&gt;someProperty&lt;/code&gt; on our obj object from above, the engine would first check the object itself.&lt;/p&gt;

&lt;p&gt;It wouldn’t find it and would then jump to its &lt;code&gt;__proto__&lt;/code&gt; object which is equal to &lt;code&gt;Object.prototype&lt;/code&gt;. It wouldn’t find it there either and upon seeing that the next &lt;code&gt;__proto__&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;, it would return &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is called the prototype chain. It’s normally described as a chain going downwards, with &lt;code&gt;null&lt;/code&gt; at the very top and the object we’re using at the bottom.&lt;/p&gt;

&lt;p&gt;When performing a lookup, the engine will traverse up the chain looking for the property and return the first one it finds, or &lt;code&gt;undefined&lt;/code&gt;if it’s not present in the prototype chain.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;__proto__ === null
|
|
__proto__ === Object.prototype
|
|
{ object literal }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This can be demonstrated. Here we’re going to work with &lt;code&gt;__proto__&lt;/code&gt; directly for the purpose of demonstration. Again, don’t ever do it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var obj = {};
obj.__proto__.testValue = 'Hello!';

console.log(obj); // -&amp;gt; {}
console.log(obj.testValue); // -&amp;gt; Hello!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This prototype chain is depicted below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;__proto__ === null
|
|
__proto__ === Object.prototype -&amp;gt; testValue: 'Hello!'
|
|
obj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When we log &lt;code&gt;obj&lt;/code&gt;, we get an empty object because the property &lt;code&gt;testValue&lt;/code&gt; isn’t present directly on the object. However, logging &lt;code&gt;obj.testValue&lt;/code&gt; triggers a lookup. The engine goes up the prototype chain and finds &lt;code&gt;testValue&lt;/code&gt; present on the object’s prototype and we see that value printing out.&lt;/p&gt;

&lt;h2&gt;
  
  
  hasOwnProperty
&lt;/h2&gt;

&lt;p&gt;There’s a method available on objects called &lt;code&gt;hasOwnProperty&lt;/code&gt;. It’ll return &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; based on whether the object itself contains the property being tested. Testing for &lt;code&gt;__proto__&lt;/code&gt;, however, will ALWAYS return false.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var obj = {};
obj.__proto__.testValue = 'Hello!';

console.log(obj.hasOwnProperty('testValue'));
// -&amp;gt; false

console.log(obj.__proto__.hasOwnProperty('testValue'));
// -&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Function prototypes
&lt;/h2&gt;

&lt;p&gt;As mentioned, functions all have a prototype property distinct from their &lt;code&gt;__proto__&lt;/code&gt; property. It’s an object. &lt;strong&gt;A function’s prototype's &lt;code&gt;__proto__&lt;/code&gt; property is equal to &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/strong&gt;. In other words:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fn() {}
console.log(fn.prototype.__proto__ === Object.prototype);
// -&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Function Prototypes and 'new'
&lt;/h3&gt;

&lt;p&gt;A function’s &lt;code&gt;prototype&lt;/code&gt; property shows its usefulness in object oriented programming. When we &lt;a href="https://codeburst.io/javascripts-new-keyword-explained-as-simply-as-possible-fec0d87b2741" rel="noopener noreferrer"&gt;invoke a function using &lt;code&gt;new&lt;/code&gt;&lt;/a&gt;, the object bound to this in the constructor function is special. The new keyword sets the object’s &lt;code&gt;__proto__&lt;/code&gt; to be the prototype property of the constructing function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When we call a function with &lt;code&gt;new&lt;/code&gt;, it sets the returned object’s &lt;code&gt;__proto__&lt;/code&gt; property equal to the function’s &lt;code&gt;prototype&lt;/code&gt; property.&lt;/strong&gt; This is the key to inheritance.&lt;/p&gt;

&lt;p&gt;We’ve assembled a few points so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;__proto__&lt;/code&gt; of an object created by calling a function with &lt;code&gt;new&lt;/code&gt; is equal to the &lt;code&gt;prototype&lt;/code&gt; of that function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;__proto__&lt;/code&gt; of a function’s &lt;code&gt;prototype&lt;/code&gt; is equal to &lt;code&gt;Object.prototype&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;__proto__&lt;/code&gt; of &lt;code&gt;Object.prototype&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This lets us assemble the following prototype chain.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Fn() {}
var obj = new Fn();

console.log(obj.__proto__ === Fn.prototype);
// -&amp;gt; true

console.log(obj.__proto__.__proto__=== Object.prototype);
// -&amp;gt; true

console.log(obj.__proto__.__proto__.__proto__ === null);
// -&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Visually drawn:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;__proto__ === null
|
|             
__proto__ === Object.prototype
|
|
__proto__ === Fn.prototype
|
|
obj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Implementing Inheritance
&lt;/h2&gt;

&lt;p&gt;We can work with a function’s &lt;code&gt;prototype&lt;/code&gt; property directly and safely. By placing methods and other properties on a function’s &lt;code&gt;prototype&lt;/code&gt;, we enable all objects created by that function (using &lt;code&gt;new&lt;/code&gt;) to access those properties through inheritance.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Fn() {}

Fn.prototype.print = function() {
    console.log("Calling Fn.prototype's print method");
};

var obj = new Fn();
obj.print(); // -&amp;gt; Calling Fn.prototype's print method
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You might be wondering what the point of this is. We can just attach this method inside the constructing function itself, like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Fn() {
    this.print = function() {
        console.log("Calling the object's print method");
    };
}

var obj = new Fn();
obj.print(); // -&amp;gt; Calling the object's print method
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You’re right, this works. The difference is that this way, each object created by calling &lt;code&gt;new Fn()&lt;/code&gt; will have its &lt;em&gt;own version&lt;/em&gt; of &lt;code&gt;print&lt;/code&gt; placed directly on the object. They’ll be distinct functions in memory. The problem with this is performance and memory usage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;There may be times when you need thousands of new objects created from a constructor function. Using this second way of attaching &lt;code&gt;print&lt;/code&gt;, we now have thousands of copies of &lt;code&gt;print&lt;/code&gt;, each one attached to one of the objects.&lt;/p&gt;

&lt;p&gt;Using the prototype chain, no matter how many objects we create out of &lt;code&gt;Fn&lt;/code&gt;, we have one &lt;code&gt;print&lt;/code&gt; sitting on &lt;code&gt;Fn.prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One method is not a big deal. Large programs, however, often have tens of methods that objects need. If an object needs access to 20 methods and we create 100,000 objects, the JavaScript engine has created 2,000,000 new functions.&lt;/p&gt;

&lt;p&gt;If this needs to happen multiple times, this will cause noticeable speed and memory issues. Compare this to having a total of 20 functions and giving each object the ability to use the same functions through the prototype chain. Much more scalable.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;console.time&lt;/code&gt; and &lt;code&gt;console.timeEnd&lt;/code&gt;, we can directly show the difference in how long it takes. Here’s the time difference of creating 2 million objects with functions directly on them vs. on the prototype. We’re storing all the objects in an array.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl9vl0r8vb87fz6fhvvc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl9vl0r8vb87fz6fhvvc.png" alt="Creating new functions (left) vs. using prototypal inheritance (right)"&gt;&lt;/a&gt;&lt;em&gt;Creating new functions (left) vs. using prototypal inheritance (right)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As we can see, putting the print method on the &lt;code&gt;prototype&lt;/code&gt; takes about half the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;__proto__&lt;/code&gt; of Literals
&lt;/h2&gt;

&lt;p&gt;As mentioned, an object’s &lt;code&gt;__proto__&lt;/code&gt; is equal to the &lt;code&gt;prototype&lt;/code&gt; of the function that created the object. This rule applies to literals also. Remember that object literals come from &lt;code&gt;Object&lt;/code&gt;, arrays come from &lt;code&gt;Array&lt;/code&gt;, and functions come from &lt;code&gt;Function&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var obj = {};
var arr = [];
function fn() {}

console.log(obj.__proto__ === Object.prototype); // -&amp;gt; true
console.log(arr.__proto__ === Array.prototype); // -&amp;gt; true
console.log(fn.__proto__ === Function.prototype); // -&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can now explain why we’re able to call methods on arrays and objects. If we have an array &lt;code&gt;arr&lt;/code&gt;, we can &lt;code&gt;call arr.map()&lt;/code&gt; because the method &lt;code&gt;map&lt;/code&gt; is present on &lt;code&gt;Array.prototyp&lt;/code&gt;e. We can call &lt;code&gt;obj.hasOwnProperty()&lt;/code&gt; because &lt;code&gt;hasOwnProperty&lt;/code&gt; is present on &lt;code&gt;Object.prototype&lt;/code&gt;. We’ve been using inheritance the whole time and didn’t even know it.&lt;/p&gt;

&lt;p&gt;The end of the &lt;code&gt;__proto__&lt;/code&gt; chain of both &lt;code&gt;Array&lt;/code&gt; and &lt;code&gt;Function&lt;/code&gt; is equal to &lt;code&gt;Object.prototype&lt;/code&gt;. They all derive from the same thing. This is why arrays, functions, and objects are all considered first-class objects in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  constructor
&lt;/h2&gt;

&lt;p&gt;We’ve thrown the word constructor around a few times. Let’s explain what it is. Every function’s &lt;code&gt;prototype&lt;/code&gt; has a &lt;code&gt;constructor&lt;/code&gt; property on it that points back to the function itself. This is something the engine does for every function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Fn() {}
console.log(Fn.prototype.constructor === Fn);
// -&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;An object created by running &lt;code&gt;new Fn()&lt;/code&gt; will have its &lt;code&gt;__proto__&lt;/code&gt; equal to Fn.prototype. So if we were to attempt to log the constructor property of that object, the engine would give us &lt;code&gt;Fn&lt;/code&gt; through its lookup process.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Fn() {}
var obj = new Fn();
console.log(obj.constructor); // -&amp;gt; ƒ Fn(){}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Why it’s Useful
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;constructor&lt;/code&gt; property on an object is useful because it can tell us how an object was created. Logging the &lt;code&gt;constructor&lt;/code&gt; property directly on an object will tell us exactly which function created our object.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Fn() {};

var normalObj = {};
var fnObj = new Fn();

console.log(normalObj.constructor);
// -&amp;gt; ƒ Object() { [native code] }

console.log(fnObj.constructor);
// -&amp;gt; ƒ Fn() {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Object.create
&lt;/h2&gt;

&lt;p&gt;There’s a way to set the prototype of an object manually. &lt;code&gt;Object.create&lt;/code&gt;. This function will take in an object as a parameter. It’ll return a brand new object whose &lt;code&gt;__proto__&lt;/code&gt; property is equal to the object that was passed in.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var prototypeObj = {
    testValue: 'Hello!'
};

var obj = Object.create(prototypeObj);
console.log(obj); // -&amp;gt; {}
console.log(obj.__proto__ === prototypeObj); // -&amp;gt; true
console.log(obj.testValue); // -&amp;gt; 'Hello!'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This gives us an easy way to extend the prototype chain. We can make objects inherit from any object we like, not just a function’s &lt;code&gt;prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you’d like more information and examples, the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create" rel="noopener noreferrer"&gt;MDN page for Object.create&lt;/a&gt; is a great resource.&lt;/p&gt;

&lt;h3&gt;
  
  
  Phew.
&lt;/h3&gt;

&lt;p&gt;That was a &lt;em&gt;lot&lt;/em&gt;. I know. However, you now have a deep understanding of inheritance in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototypes Summary
&lt;/h2&gt;

&lt;p&gt;In short, inheritance in JavaScript is implemented through the prototype chain. Every normally created object, array, and function has a prototype chain of &lt;code&gt;__proto__&lt;/code&gt; properties ending with &lt;code&gt;Object.prototype&lt;/code&gt; at the top. This is why they’re all considered first-class objects in JavaScript.&lt;/p&gt;

&lt;p&gt;Functions have a &lt;code&gt;prototype&lt;/code&gt; property in addition to the &lt;code&gt;__proto__&lt;/code&gt; property. When using a constructor function with &lt;code&gt;new&lt;/code&gt;, it’s good practice to place methods on the function’s &lt;code&gt;prototype&lt;/code&gt; instead of on the object itself. The returned object’s &lt;code&gt;__proto__&lt;/code&gt; will be equal to the function’s &lt;code&gt;prototype&lt;/code&gt; so it will inherit all methods on the function’s &lt;code&gt;prototype&lt;/code&gt;. This prevents unnecessary memory usage and improves speed.&lt;/p&gt;

&lt;p&gt;We can check if an object has its own property by using the &lt;code&gt;hasOwnProperty&lt;/code&gt; method. We can manually set up inheritance by using &lt;code&gt;Object.create&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  That’s It. If this was helpful, please hit the heart so this story reaches more people. Also feel free to check out my other work.
&lt;/h3&gt;

&lt;h2&gt;
  
  
  My Work
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Online Course
&lt;/h3&gt;

&lt;p&gt;I’ve created an &lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal" rel="noopener noreferrer"&gt;online course&lt;/a&gt; covering intermediate JavaScript topics such as scope, closures, OOP, this, new, apply/call/bind, asynchronous code, array and object manipulation, and ES2015+.&lt;br&gt;
&lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal" rel="noopener noreferrer"&gt;Step Up Your JS: A Comprehensive Guide to Intermediate JavaScript&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Recent Articles
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0" rel="noopener noreferrer"&gt;Explaining Value vs. Reference in Javascript&lt;/a&gt;&lt;br&gt;
&lt;a href="https://codeburst.io/react-ecosystem-setup-step-by-step-walkthrough-721ff45a7fc1" rel="noopener noreferrer"&gt;React Ecosystem Setup — Step-By-Step Walkthrough&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>software</category>
    </item>
    <item>
      <title>Master value vs reference in JavaScript</title>
      <dc:creator>arnav-aggarwal</dc:creator>
      <pubDate>Mon, 14 Aug 2017 19:55:04 +0000</pubDate>
      <link>https://dev.to/arnavaggarwal/master-value-vs-reference-in-javascript</link>
      <guid>https://dev.to/arnavaggarwal/master-value-vs-reference-in-javascript</guid>
      <description>&lt;p&gt;This is taken from my new course, &lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal" rel="noopener noreferrer"&gt;Step Up Your JS: A Comprehensive Guide to Intermediate JavaScript&lt;/a&gt;. Feel free to &lt;a href="https://www.educative.io/collection/page/5679346740101120/5707702298738688/5685265389584384" rel="noopener noreferrer"&gt;check it out there&lt;/a&gt; for interactive code playgrounds and practice problems.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fqcp16fvpc9sr20cfesdd.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fqcp16fvpc9sr20cfesdd.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JavaScript has 5 data types that are copied by &lt;strong&gt;&lt;em&gt;value&lt;/em&gt;&lt;/strong&gt;: &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, and &lt;code&gt;Number&lt;/code&gt;. We’ll call these &lt;strong&gt;primitive types&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;JavaScript has 3 data types that are copied by &lt;strong&gt;&lt;em&gt;reference&lt;/em&gt;&lt;/strong&gt;: &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Function&lt;/code&gt;, and &lt;code&gt;Object&lt;/code&gt;. These are all technically Objects, so we’ll refer to them collectively as &lt;strong&gt;Objects&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Primitives
&lt;/h2&gt;

&lt;p&gt;If a primitive type is assigned to a variable, we can think of that variable as &lt;em&gt;containing&lt;/em&gt; the primitive value.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 10;
var y = 'abc';
var z = null;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;x&lt;/code&gt; &lt;em&gt;contains&lt;/em&gt; &lt;code&gt;10&lt;/code&gt;. &lt;code&gt;y&lt;/code&gt; &lt;em&gt;contains&lt;/em&gt; &lt;code&gt;'abc'&lt;/code&gt;. To cement this idea, we’ll maintain an image of what these variables and their respective values look like in memory.&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%2Fcdn-images-1.medium.com%2Fmax%2F4684%2F1%2APdKLlT7zUrmDBZUOBsZh7w.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%2Fcdn-images-1.medium.com%2Fmax%2F4684%2F1%2APdKLlT7zUrmDBZUOBsZh7w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we assign these variables to other variables using &lt;code&gt;=&lt;/code&gt;, we &lt;strong&gt;copy&lt;/strong&gt; the value to the new variable. They are copied by value.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 10;
var y = 'abc';

var a = x;
var b = y;

console.log(x, y, a, b);
// -&amp;gt; 10, 'abc', 10, 'abc'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Both &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;x&lt;/code&gt; now contain &lt;code&gt;10&lt;/code&gt;. Both b and y now contain &lt;code&gt;'abc'&lt;/code&gt;. They’re separate, as the values themselves were copied.&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%2Fcdn-images-1.medium.com%2Fmax%2F4682%2F1%2AMZ3AcwELYZ2ONYFg3LiTXQ.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%2Fcdn-images-1.medium.com%2Fmax%2F4682%2F1%2AMZ3AcwELYZ2ONYFg3LiTXQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Changing one does not change the other. Think of the variables as having no relationship to each other.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 10;
var y = 'abc';

var a = x;
var b = y;

a = 5;
b = 'def';

console.log(x, y, a, b); // -&amp;gt; 10, 'abc', 5, 'def'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Objects
&lt;/h2&gt;

&lt;p&gt;This will feel confusing, but bear with me and read through it. Once you get through it, it’ll seem easy.&lt;/p&gt;

&lt;p&gt;Variables that are assigned a non-primitive value are given a &lt;em&gt;reference&lt;/em&gt; to that value. That reference points to the object’s location in memory. The variables don’t actually contain the value.&lt;/p&gt;

&lt;p&gt;Objects are created at some location in our computer’s memory. When we write &lt;code&gt;arr = []&lt;/code&gt;, we’ve created an array in memory. What the variable &lt;code&gt;arr&lt;/code&gt; now contains is the address, the location, of that array.&lt;/p&gt;

&lt;p&gt;Let’s pretend that &lt;code&gt;address&lt;/code&gt; is a new data type that is passed by value, just like number or string. An address points to the location, in memory, of a value that is passed by reference. Just like a string is denoted by quotation marks (&lt;code&gt;''&lt;/code&gt; or &lt;code&gt;""&lt;/code&gt;), an address will be denoted by arrow brackets, &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When we assign and use a reference-type variable, what we write and see is:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) var arr = [];
2) arr.push(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A representation of lines 1 and 2 above in memory is:&lt;/p&gt;

&lt;p&gt;1.&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%2Fcdn-images-1.medium.com%2Fmax%2F4680%2F1%2Ah1aXuPwCyhu6GKwgeFMLDw.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%2Fcdn-images-1.medium.com%2Fmax%2F4680%2F1%2Ah1aXuPwCyhu6GKwgeFMLDw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2.&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%2Fcdn-images-1.medium.com%2Fmax%2F4678%2F1%2AHaemMnuU05EW1b3BZPubIg.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%2Fcdn-images-1.medium.com%2Fmax%2F4678%2F1%2AHaemMnuU05EW1b3BZPubIg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that the value, the address, contained in the variable &lt;code&gt;arr&lt;/code&gt; is &lt;strong&gt;static&lt;/strong&gt;. The array in memory is what changes. When we use &lt;code&gt;arr&lt;/code&gt; to do something, such as pushing a value, the JavaScript engine goes to the location of arr in memory and works with the information stored there.&lt;/p&gt;

&lt;h3&gt;
  
  
  Assigning by Reference
&lt;/h3&gt;

&lt;p&gt;When a reference type value, an object, is copied to another variable using &lt;code&gt;=&lt;/code&gt;, the address of that value is what’s actually copied over &lt;em&gt;as if it were a primitive&lt;/em&gt;. &lt;strong&gt;Objects are copied by reference&lt;/strong&gt; instead of by value. The object itself is unchanged and static. The only thing copied is the reference, the address, of the object.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var reference = [1];
var refCopy = reference;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code above looks like this in memory.&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%2Fcdn-images-1.medium.com%2Fmax%2F4670%2F1%2Ad2W3ulHbHRGrFQ-c1SG5gA.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%2Fcdn-images-1.medium.com%2Fmax%2F4670%2F1%2Ad2W3ulHbHRGrFQ-c1SG5gA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each variable now contains a reference to the &lt;em&gt;same array&lt;/em&gt;. That means that if we alter &lt;code&gt;reference&lt;/code&gt;, &lt;code&gt;refCopy&lt;/code&gt; will see those changes:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reference.push(2);
console.log(reference, refCopy);
// -&amp;gt; [1, 2], [1, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fcdn-images-1.medium.com%2Fmax%2F4666%2F1%2ARFrFRXIperg0yTwXauc97w.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%2Fcdn-images-1.medium.com%2Fmax%2F4666%2F1%2ARFrFRXIperg0yTwXauc97w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ve pushed &lt;code&gt;2&lt;/code&gt; into the array in memory. When we use &lt;code&gt;reference&lt;/code&gt; and &lt;code&gt;refCopy&lt;/code&gt;, we’re pointing to that same array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reassigning a Reference
&lt;/h3&gt;

&lt;p&gt;Reassigning a reference variable replaces the old reference.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var obj = { first: 'reference' };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In memory:&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%2Fcdn-images-1.medium.com%2Fmax%2F4672%2F1%2APWGp9d2zZ_QGg18HXBSq9Q.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%2Fcdn-images-1.medium.com%2Fmax%2F4672%2F1%2APWGp9d2zZ_QGg18HXBSq9Q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we have a second line:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var obj = { first: 'reference' };
obj = { second: 'ref2' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The address stored in &lt;code&gt;obj&lt;/code&gt; changes. The first object is still present in memory, and so is the next object:&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%2Fcdn-images-1.medium.com%2Fmax%2F4674%2F1%2A1h73Wn9IyaiXbhxhmJZmYA.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%2Fcdn-images-1.medium.com%2Fmax%2F4674%2F1%2A1h73Wn9IyaiXbhxhmJZmYA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When there are no references to an object remaining, as we see for the address &lt;code&gt;#234&lt;/code&gt; above, the JavaScript engine can perform garbage collection. This just means that the programmer has lost all references to the object and can’t use the object anymore, so the engine can safely delete it from memory. In this case, the object &lt;code&gt;{ first: 'reference' }&lt;/code&gt; is no longer accessible and is available to the engine for garbage collection.&lt;/p&gt;

&lt;h2&gt;
  
  
  == and ===
&lt;/h2&gt;

&lt;p&gt;When the equality operators, &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;===&lt;/code&gt;, are used on reference-type variables, they check the reference. If the variables contain a reference to the same item, the comparison will result in true.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arrRef = ['Hi!'];
var arrRef2 = arrRef;

console.log(arrRef === arrRef2); // -&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If they’re distinct objects, even if they contain identical properties, the comparison will result in false.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr1 = ['Hi!'];
var arr2 = ['Hi!'];

console.log(arr1 === arr2); // -&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we have two distinct objects and want to see if their properties are the same, the easiest way to do so is to turn them both into strings and then compare the strings. When the equality operators are comparing primitives, they simply check if the values are the same.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr1 = ['Hi!'];
var arr2 = ['Hi!'];

var arr1str = JSON.stringify(arr1);
var arr2str = JSON.stringify(arr2);

console.log(arr1str === arr2str); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that using &lt;code&gt;JSON.stringify&lt;/code&gt; will cause problems if some of the properties are functions or &lt;code&gt;undefined&lt;/code&gt; as it skips those values. A safer option would be to recursively loop through the objects and make sure each of the properties is the same. &lt;/p&gt;

&lt;h2&gt;
  
  
  Passing Parameters through Functions
&lt;/h2&gt;

&lt;p&gt;When we pass primitive values into a function, the function copies the values into its parameters. It’s effectively the same as using &lt;code&gt;=&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var hundred = 100;
var two = 2;

function multiply(x, y) {
    // PAUSE
    return x * y;
}

var twoHundred = multiply(hundred, two);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the example above, we give &lt;code&gt;hundred&lt;/code&gt; the value &lt;code&gt;100&lt;/code&gt;. When we pass it into &lt;code&gt;multiply&lt;/code&gt;, the variable &lt;code&gt;x&lt;/code&gt; gets that value, &lt;code&gt;100&lt;/code&gt;. The value is copied over as if we used an &lt;code&gt;=&lt;/code&gt; assignment. Again, the value of &lt;code&gt;hundred&lt;/code&gt; is not affected. Here is a snapshot of what the memory looks like right at the &lt;code&gt;// PAUSE&lt;/code&gt; comment line in &lt;code&gt;multiply&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F4536%2F1%2A3AYcflNTwTTGTgCul0DgBw.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%2Fcdn-images-1.medium.com%2Fmax%2F4536%2F1%2A3AYcflNTwTTGTgCul0DgBw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pure Functions
&lt;/h3&gt;

&lt;p&gt;We refer to functions that don’t affect anything in the outside scope as &lt;em&gt;pure functions&lt;/em&gt;. As long as a function only takes primitive values as parameters and doesn’t use any variables in its surrounding scope, it is automatically pure, as it can’t affect anything in the outside scope. All variables created inside are garbage-collected as soon as the function returns.&lt;/p&gt;

&lt;p&gt;A function that takes in an Object, however, can mutate the state of its surrounding scope. If a function takes in an array reference and alters the array that it points to, perhaps by pushing to it, variables in the surrounding scope that reference that array see that change. After the function returns, the changes it makes persist in the outer scope. This can cause undesired side effects that can be difficult to track down.&lt;/p&gt;

&lt;p&gt;Many native array functions, including Array.map and Array.filter, are therefore written as pure functions. They take in an array reference and internally, they copy the array and work with the copy instead of the original. This makes it so the original is untouched, the outer scope is unaffected, and we’re returned a reference to a brand new array.&lt;/p&gt;

&lt;p&gt;Let’s go into an example of a pure vs. impure function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function changeAgeImpure(person) {
    person.age = 25;
    return person;
}

var alex = {
    name: 'Alex',
    age: 30
};

var changedAlex = changeAgeImpure(alex);

console.log(alex); // -&amp;gt; { name: 'Alex', age: 25 }
console.log(changedAlex); // -&amp;gt; { name: 'Alex', age: 25 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This impure function takes in an object and changes the property age on that object to be &lt;code&gt;25&lt;/code&gt;. Because it acts on the reference it was given, it directly changes the object &lt;code&gt;alex&lt;/code&gt;. Note that when it returns the &lt;code&gt;person&lt;/code&gt; object, it is returning the exact same object that was passed in. &lt;code&gt;alex&lt;/code&gt; and &lt;code&gt;alexChanged&lt;/code&gt; contain the same reference. It’s redundant to return the &lt;code&gt;person&lt;/code&gt; variable and to store that reference in a new variable.&lt;/p&gt;

&lt;p&gt;Let’s look at a pure function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function changeAgePure(person) {
    var newPersonObj = JSON.parse(JSON.stringify(person));
    newPersonObj.age = 25;
    return newPersonObj;
}

var alex = {
    name: 'Alex',
    age: 30
};

var alexChanged = changeAgePure(alex);

console.log(alex); // -&amp;gt; { name: 'Alex', age: 30 }
console.log(alexChanged); // -&amp;gt; { name: 'Alex', age: 25 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this function, we use &lt;code&gt;JSON.stringify&lt;/code&gt; to transform the object we’re passed into a string, and then parse it back into an object with &lt;code&gt;JSON.parse&lt;/code&gt;. By performing this transformation and storing the result in a new variable, we’ve created a new object. The new object has the same properties as the original but it is a distinctly separate object in memory.&lt;/p&gt;

&lt;p&gt;When we change the &lt;code&gt;age&lt;/code&gt; property on this new object, the original is unaffected. This function is now pure. It can’t affect any object outside its own scope, not even the object that was passed in. The new object needs to be returned and stored in a new variable or else it gets garbage collected once the function completes, as the object is no longer in scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test Yourself
&lt;/h3&gt;

&lt;p&gt;Value vs. reference is a concept often tested in coding interviews. Try to figure out for yourself what’s logged here.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function changeAgeAndReference(person) {
    person.age = 25;
    person = {
      name: 'John',
      age: 50
    };

    return person;
}

var personObj1 = {
    name: 'Alex',
    age: 30
};

var personObj2 = changeAgeAndReference(personObj1);

console.log(personObj1); // -&amp;gt; ?
console.log(personObj2); // -&amp;gt; ?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The function first changes the property age on the original object it was passed in. It then reassigns the variable to a brand new object and returns that object. Here’s what the two objects are logged out.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(personObj1); // -&amp;gt; { name: 'Alex', age: 25 }
console.log(personObj2); // -&amp;gt; { name: 'John', age: 50 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Remember that assignment through function parameters is essentially the same as an assignment with &lt;code&gt;=&lt;/code&gt;. The variable &lt;code&gt;person&lt;/code&gt; in the function contains a reference to the &lt;code&gt;personObj1&lt;/code&gt; object, so it initially acts directly on that object. Once we reassign person to a new object, it stops affecting the original.&lt;/p&gt;

&lt;p&gt;This reassignment does not change the object that &lt;code&gt;personObj1&lt;/code&gt; points to in the outer scope. &lt;code&gt;person&lt;/code&gt; has a new reference because it was reassigned but this reassignment doesn’t change &lt;code&gt;personObj1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An equivalent piece of code to the above block would be:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var personObj1 = {
    name: 'Alex',
    age: 30
};

var person = personObj1;
person.age = 25;

person = {
    name: 'John',
    age: 50
};

var personObj2 = person;

console.log(personObj1); // -&amp;gt; { name: 'Alex', age: 25 }
console.log(personObj2); // -&amp;gt; { name: 'John', age: 50 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The only difference is that when we use the function, &lt;code&gt;person&lt;/code&gt; is no longer in scope once the function ends.&lt;/p&gt;

&lt;h3&gt;
  
  
  That’s it.
&lt;/h3&gt;




&lt;p&gt;If you enjoyed this, please hit the heart so that it reaches more people.&lt;/p&gt;

&lt;p&gt;Again, for interactive code samples, feel free to read this article for free through my course. There are other free articles available to read.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.educative.io/collection/5679346740101120/5707702298738688?authorName=Arnav%20Aggarwal" rel="noopener noreferrer"&gt;Step Up Your JS: A Comprehensive Guide to Intermediate JavaScript&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;a href="https://twitter.com/farm_fresh_js" rel="noopener noreferrer"&gt;Twitter - @farm_fresh_js&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@arnav_aggarwal" rel="noopener noreferrer"&gt;Medium - @arnav_aggarwal&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.quora.com/profile/Arnav-Aggarwal" rel="noopener noreferrer"&gt;Quora - Arnav Aggarwal&lt;/a&gt;&lt;/p&gt;

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