<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ inheritence - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ inheritence - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 27 Jul 2026 17:32:38 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/inheritence/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How __proto__, prototype, and Inheritance Actually Work in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever wondered why everything in JavaScript acts like an object? Or how inheritance actually works behind the scenes? What's the difference between __proto__ and prototype? If these questions have crossed your mind, you're not alone. These ar... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-proto-prototype-and-inheritance-actually-work-in-javascript/</link>
                <guid isPermaLink="false">690a2d839d070c31fadbc25e</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ inheritence ]]>
                    </category>
                
                    <category>
                        <![CDATA[ prototypeal-inheritance ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Object Oriented Programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Shejan Mahamud ]]>
                </dc:creator>
                <pubDate>Tue, 04 Nov 2025 16:44:51 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1762210692821/651a67f9-cbe5-4f09-b048-957caaa5e1ac.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever wondered why everything in JavaScript acts like an object? Or how inheritance actually works behind the scenes? What's the difference between <code>__proto__</code> and <code>prototype</code>?</p>
<p>If these questions have crossed your mind, you're not alone. These are some of the most fundamental concepts in JavaScript, yet they often confuse developers.</p>
<p>In this tutorial, we'll demystify prototypes, prototype chains, and inheritance in JavaScript. By the end, you'll understand the "what," "why," and "how" of JavaScript's prototype system.</p>
<h3 id="heading-heres-what-ill-cover">Here’s what I’ll cover:</h3>
<ul>
<li><p><a class="post-section-overview" href="#heading-the-string-method-mystery">The String Method Mystery</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-objects-work-internally">How Objects Work Internally</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-understanding-the-prototype-chain">Understanding the Prototype Chain</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-everything-in-javascript-is-an-object">Why Everything in JavaScript is an Object</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-difference-between-proto-and-prototype">The Difference Between <strong>proto</strong> and prototype</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-prototypes-work-with-functions">How Prototypes Work with Functions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-prototypes-work-with-classes">How Prototypes Work with Classes</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>To get the most out of this tutorial, you should have:</p>
<ul>
<li><p>Basic understanding of JavaScript fundamentals</p>
</li>
<li><p>Familiarity with objects, functions, and classes in JavaScript</p>
</li>
<li><p>Knowledge of how to declare and use variables</p>
</li>
<li><p>Experience working with the <code>new</code> keyword (helpful but not required)</p>
</li>
</ul>
<h2 id="heading-the-string-method-mystery">The String Method Mystery</h2>
<p>Let's start with a simple example that demonstrates something interesting about JavaScript:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> name = <span class="hljs-string">"Shejan Mahamud"</span>;
</code></pre>
<p>After declaring this variable, we can use String methods like:</p>
<pre><code class="lang-javascript">name.toLowerCase(); <span class="hljs-comment">// "shejan mahamud"</span>
name.toUpperCase(); <span class="hljs-comment">// "SHEJAN MAHAMUD"</span>
</code></pre>
<p>This seems normal at first glance, but wait – something unusual is happening. Notice anything odd here? We're using dot notation on a string primitive.</p>
<p>Here's the puzzling part: we know that strings are primitive types in JavaScript, not objects. So how can we use dot notation to access methods? After all, dot notation typically only works with objects.</p>
<p>The answer to this mystery lies in understanding how JavaScript handles primitives and prototypes. But before we get there, let's first look at how objects work internally.</p>
<h2 id="heading-how-objects-work-internally">How Objects Work Internally</h2>
<p>When you create an object in JavaScript like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> info1 = {
  <span class="hljs-attr">fName</span>: <span class="hljs-string">"Shejan"</span>,
  <span class="hljs-attr">lName</span>: <span class="hljs-string">"Mahamud"</span>
};
</code></pre>
<p>JavaScript does something interesting behind the scenes. It automatically adds a hidden property called <code>__proto__</code> to your object. This property points to <code>Object.prototype</code>, which is the prototype of the built-in Object class.</p>
<p>You might wonder: does <code>Object.prototype</code> also have a <code>__proto__</code>? Yes, it does, but its value is <code>null</code>. This is because <code>Object.prototype</code> is at the top of the prototype chain and doesn't inherit from anything else.</p>
<p>Let's look at a more complex example to understand this better:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> info1 = {
  <span class="hljs-attr">fName1</span>: <span class="hljs-string">"Shejan"</span>,
  <span class="hljs-attr">lName1</span>: <span class="hljs-string">"Mahamud"</span>
};

<span class="hljs-keyword">const</span> info2 = {
  <span class="hljs-attr">fName2</span>: <span class="hljs-string">"Boltu"</span>,
  <span class="hljs-attr">lName2</span>: <span class="hljs-string">"Mia"</span>,
  <span class="hljs-attr">__proto__</span>: info1
};

<span class="hljs-keyword">const</span> info3 = {
  <span class="hljs-attr">fName3</span>: <span class="hljs-string">"Habu"</span>,
  <span class="hljs-attr">lName3</span>: <span class="hljs-string">"Mia"</span>,
  <span class="hljs-attr">__proto__</span>: info2
};
</code></pre>
<p>In this example, we've intentionally set the <code>__proto__</code> property for <code>info2</code> and <code>info3</code>. Now here's an interesting question: can we access <code>fName1</code> from <code>info3</code>?</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(info3.fName1); <span class="hljs-comment">// "Shejan"</span>
</code></pre>
<p>Yes, we can! Let's understand how this works.</p>
<h2 id="heading-understanding-the-prototype-chain">Understanding the Prototype Chain</h2>
<p>When you try to access a property on an object, JavaScript follows a specific lookup process:</p>
<ol>
<li><p>First, it searches for the property in the object itself (the base object)</p>
</li>
<li><p>If it doesn't find it there, it looks in the object's <code>__proto__</code></p>
</li>
<li><p>If it still doesn't find it, it continues up the chain, checking each <code>__proto__</code> until it either finds the property or reaches <code>null</code></p>
</li>
</ol>
<p>In our example with <code>info3.fName1</code>:</p>
<ul>
<li><p>JavaScript first looks in <code>info3</code> – and it doesn't find <code>fName1</code></p>
</li>
<li><p>Then it checks <code>info3.__proto__</code>, which points to <code>info2</code> – it doesn't find <code>fName1</code> there, either</p>
</li>
<li><p>Next it checks <code>info2.__proto__</code>, which points to <code>info1</code> – and it finds <code>fName1</code> here!</p>
</li>
</ul>
<p>This is called the <strong>prototype chain</strong>, and this is how inheritance works in JavaScript. Here's a visual representation:</p>
<pre><code class="lang-javascript">┌────────────┐
│  info3     │
│ fName3     │
│ lName3     │
└────┬───────┘
     │ __proto__
     ▼
┌────────────┐
│  info2     │
│ fName2     │
│ lName2     │
└────┬───────┘
     │ __proto__
     ▼
┌────────────┐
│  info1     │
│ fName1     │
│ lName1     │
└────┬───────┘
     │ __proto__
     ▼
┌─────────────────┐
│ <span class="hljs-built_in">Object</span>.prototype│
└────┬────────────┘
     ▼
    <span class="hljs-literal">null</span>
</code></pre>
<p>Each object points to the next object in the chain through its <code>__proto__</code> property. This chain continues until it reaches <code>null</code>.</p>
<h2 id="heading-why-everything-in-javascript-is-an-object">Why Everything in JavaScript is an Object</h2>
<p>Now let's solve the mystery we started with: how can primitive types use object methods?</p>
<p>In JavaScript, almost everything behaves like an object, even though primitive types (like strings, numbers, and booleans) technically aren't objects. This works through a process called <strong>auto-boxing</strong> or <strong>wrapper objects</strong>.</p>
<p>Let's see this in action:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> yourName = <span class="hljs-string">"Boltu"</span>;
</code></pre>
<p>When you try to use a method on this string:</p>
<pre><code class="lang-javascript">yourName.toLowerCase();
</code></pre>
<p>Here's what JavaScript does behind the scenes:</p>
<ol>
<li><p>It temporarily wraps the primitive value in a wrapper object: <code>new String("Boltu")</code></p>
</li>
<li><p>This temporary object's <code>__proto__</code> automatically points to <code>String.prototype</code></p>
</li>
<li><p>The method is found in <code>String.prototype</code> and executed</p>
</li>
<li><p>After the operation completes, the wrapper object is discarded</p>
</li>
<li><p><code>yourName</code> returns to being a simple primitive value</p>
</li>
</ol>
<p>This is why you can use methods on primitives even though they're not objects. JavaScript creates a temporary object, uses it to access the method, then throws it away.</p>
<p>The same process happens with other primitive types:</p>
<ul>
<li><p>Numbers use <code>Number.prototype</code></p>
</li>
<li><p>Booleans use <code>Boolean.prototype</code></p>
</li>
</ul>
<p>And so on. This elegant system is why developers often say "everything in JavaScript is an object" – even when it's not technically true, it behaves that way when needed.</p>
<h2 id="heading-the-difference-between-proto-and-prototype">The Difference Between <code>__proto__</code> and <code>prototype</code></h2>
<p>This is one of the most confusing aspects of JavaScript for many developers. Let's break it down clearly.</p>
<h3 id="heading-what-is-prototype">What is <code>prototype</code>?</h3>
<p>When you create a function or class in JavaScript, the language automatically creates a blueprint object called <code>prototype</code>. This happens behind the scenes.</p>
<p>Here's an example:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name</span>) </span>{
  <span class="hljs-built_in">this</span>.name = name;
}
</code></pre>
<p>When JavaScript sees this function, it internally does this:</p>
<pre><code class="lang-javascript">Person.prototype = { 
  <span class="hljs-attr">constructor</span>: Person 
};
</code></pre>
<p>The <code>Person</code> function now has a hidden property called <code>prototype</code>, which is an object containing a <code>constructor</code> property.</p>
<p>You can add methods to this prototype object:</p>
<pre><code class="lang-javascript">Person.prototype.sayHi = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hi, I'm "</span> + <span class="hljs-built_in">this</span>.name);
};
</code></pre>
<h3 id="heading-what-is-proto">What is <code>__proto__</code>?</h3>
<p><code>__proto__</code> is a property that exists on every object instance (arrays, functions, objects – everything). It's an internal reference or pointer that indicates which prototype the object inherits from.</p>
<p>By default, when you create an object, its <code>__proto__</code> points to <code>Object.prototype</code>.</p>
<h3 id="heading-how-they-work-together">How They Work Together</h3>
<p>When you use the <code>new</code> keyword:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> p1 = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Shejan"</span>);
</code></pre>
<p>JavaScript performs these steps internally:</p>
<ol>
<li><p>Creates a new empty object: <code>p1 = {}</code></p>
</li>
<li><p>Sets the object's <code>__proto__</code>: <code>p1.__proto__ = Person.prototype</code></p>
</li>
<li><p>Calls the constructor function with the new object: <code>Person.call(p1, "Shejan")</code></p>
</li>
<li><p>Returns the object: <code>return p1</code></p>
</li>
</ol>
<p>Now when you try to access a method:</p>
<pre><code class="lang-javascript">p1.sayHi(); <span class="hljs-comment">// "Hi, I'm Shejan"</span>
</code></pre>
<p>JavaScript looks for <code>sayHi</code> in <code>p1</code> first. When it doesn't find it, it checks <code>p1.__proto__</code>, which points to <code>Person.prototype</code>, where the method is defined.</p>
<p>The relationship can be expressed as:</p>
<pre><code class="lang-javascript">p1.__proto__ === Person.prototype; <span class="hljs-comment">// true</span>
Person.prototype.constructor === Person; <span class="hljs-comment">// true</span>
</code></pre>
<p><strong>In summary:</strong></p>
<ul>
<li><p><code>prototype</code> is a property of functions/classes that serves as a blueprint for instances</p>
</li>
<li><p><code>__proto__</code> is a property of object instances that points to the prototype they inherit from</p>
</li>
</ul>
<h2 id="heading-how-prototypes-work-with-functions">How Prototypes Work with Functions</h2>
<p>Let's see a complete example with functions:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name, age</span>) </span>{
  <span class="hljs-built_in">this</span>.name = name;
  <span class="hljs-built_in">this</span>.age = age;
}

<span class="hljs-comment">// Adding a method to the prototype</span>
Person.prototype.introduce = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hi, I'm <span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span> and I'm <span class="hljs-subst">${<span class="hljs-built_in">this</span>.age}</span> years old.`</span>);
};

<span class="hljs-comment">// Creating instances</span>
<span class="hljs-keyword">const</span> person1 = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Alice"</span>, <span class="hljs-number">25</span>);
<span class="hljs-keyword">const</span> person2 = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Bob"</span>, <span class="hljs-number">30</span>);

person1.introduce(); <span class="hljs-comment">// "Hi, I'm Alice and I'm 25 years old."</span>
person2.introduce(); <span class="hljs-comment">// "Hi, I'm Bob and I'm 30 years old."</span>

<span class="hljs-comment">// Both instances share the same prototype</span>
<span class="hljs-built_in">console</span>.log(person1.__proto__ === Person.prototype); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(person2.__proto__ === Person.prototype); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(person1.__proto__ === person2.__proto__); <span class="hljs-comment">// true</span>
</code></pre>
<p>The key benefit here is memory efficiency: the <code>introduce</code> method exists only once in <code>Person.prototype</code>, but all instances can access it through the prototype chain.</p>
<h2 id="heading-how-prototypes-work-with-classes">How Prototypes Work with Classes</h2>
<p>ES6 introduced the <code>class</code> syntax, which looks different but works the same way under the hood:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> </span>{
  <span class="hljs-keyword">constructor</span>(name) {
    <span class="hljs-built_in">this</span>.name = name;
  }

  sayHi() {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hi, I'm <span class="hljs-subst">${<span class="hljs-built_in">this</span>.name}</span>`</span>);
  }
}

<span class="hljs-keyword">const</span> user1 = <span class="hljs-keyword">new</span> User(<span class="hljs-string">"Charlie"</span>);
user1.sayHi(); <span class="hljs-comment">// "Hi, I'm Charlie"</span>

<span class="hljs-comment">// Let's check what's really happening</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> User); <span class="hljs-comment">// "function"</span>
<span class="hljs-built_in">console</span>.log(User.prototype); <span class="hljs-comment">// { sayHi: f, constructor: f User() }</span>
<span class="hljs-built_in">console</span>.log(user1.__proto__ === User.prototype); <span class="hljs-comment">// true</span>
</code></pre>
<p>Classes are essentially syntactic sugar over JavaScript's prototype-based inheritance. Internally:</p>
<ul>
<li><p>A class is still a constructor function</p>
</li>
<li><p>Methods defined in the class are added to <code>ClassName.prototype</code></p>
</li>
<li><p>Instances created with <code>new</code> have their <code>__proto__</code> set to the class's prototype</p>
</li>
</ul>
<p>This means everything we learned about function prototypes applies to classes as well.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Understanding prototypes and the prototype chain is fundamental to mastering JavaScript. These concepts form the foundation of how JavaScript implements inheritance and object-oriented programming.</p>
<h3 id="heading-key-takeaways">Key Takeaways</h3>
<p>Let's recap what we've learned:</p>
<ol>
<li><p><strong>Every object has</strong> <code>__proto__</code>: This property points to the prototype the object inherits from, enabling the prototype chain lookup mechanism.</p>
</li>
<li><p><strong>Functions and classes have</strong> <code>prototype</code>: This property serves as a blueprint for instances created with the <code>new</code> keyword.</p>
</li>
<li><p><strong>The prototype chain enables inheritance</strong>: When JavaScript can't find a property on an object, it walks up the prototype chain until it finds the property or reaches <code>null</code>.</p>
</li>
<li><p><strong>Primitives use wrapper objects</strong>: Even though primitives aren't objects, JavaScript temporarily wraps them in objects to provide access to methods.</p>
</li>
<li><p><strong>Classes are syntactic sugar</strong>: The modern <code>class</code> syntax is cleaner, but it still uses prototypes under the hood.</p>
</li>
</ol>
<p>JavaScript might seem quirky at first, but once you understand how it works under the hood, you'll appreciate its elegant and flexible design.</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
