<?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>Forem: Hasan Rifat</title>
    <description>The latest articles on Forem by Hasan Rifat (@hasan_rifat).</description>
    <link>https://forem.com/hasan_rifat</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%2F1993708%2Feb099db7-b706-4664-ae0b-e30381b25dd4.jpg</url>
      <title>Forem: Hasan Rifat</title>
      <link>https://forem.com/hasan_rifat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hasan_rifat"/>
    <language>en</language>
    <item>
      <title>Mastering JavaScript Prototypes: Why They Make JS Awesome</title>
      <dc:creator>Hasan Rifat</dc:creator>
      <pubDate>Sat, 19 Apr 2025 12:42:56 +0000</pubDate>
      <link>https://forem.com/hasan_rifat/mastering-javascript-prototypes-why-they-make-js-awesome-57cj</link>
      <guid>https://forem.com/hasan_rifat/mastering-javascript-prototypes-why-they-make-js-awesome-57cj</guid>
      <description>&lt;p&gt;Hey JavaScript enthusiasts!👋 Ever wondered how JavaScript handles inheritance or why you can call methods like &lt;code&gt;.toString()&lt;/code&gt; on any object? The secrete is prototypes, a core feature that makes JavaScript super flexible and powerful. In this blog, i will deep dive into prototypes—what they are, why they exist, and how they help JavaScript shine. With step-by-step examples, we’ll unravel this concept and show you why it’s a game-changer.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;What Are Prototypes?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
In simple words &lt;code&gt;Prototype is a property of javascript any function (or method) which points to an object.&lt;/code&gt;&lt;br&gt;
In JavaScript, every object has a prototype, a special object that acts like a blueprint. Prototypes allow objects to inherit properties and methods from other objects, creating a chain of shared behavior. This is JavaScript’s way of handling inheritance also, and which is called &lt;code&gt;prototype-based inheritance.&lt;br&gt;
&lt;/code&gt; Think of a prototype as a family tree: if you don’t have a certain trait(property or method), JavaScript looks up the tree to your “parent” (prototype) to find it. This makes code reusable and efficient.&lt;br&gt;
Here’s the key: every object is linked to a prototype via its prototype chain, and this chain goes all the way up to &lt;code&gt;Object.prototype&lt;/code&gt;, the grandparent of all objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Why Do Prototypes Exist in JavaScript?&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
JavaScript was created in 1995 by Brendan Eich to add interactivity to web pages. Unlike languages like Java, which use class-based inheritance, JavaScript went with prototype-based inheritance for simplicity and flexibility. &lt;code&gt;Why?&lt;/code&gt; Because:&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Dynamic Behavior:&lt;/code&gt;&lt;/strong&gt; You can add or modify properties/methods at runtime, making JavaScript adaptable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Memory Efficiency:&lt;/code&gt;&lt;/strong&gt; Prototypes allow shared methods across objects, saving memory compared to duplicating them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;How Prototypes Work: The Basics&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉Let’s break down the core concepts step by step.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Every Object Has a Prototype&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you create an object, it’s automatically linked to a prototype. For plain objects, that’s &lt;strong&gt;&lt;code&gt;Object.prototype&lt;/code&gt;&lt;/strong&gt;. For arrays, it’s &lt;code&gt;Array.prototype&lt;/code&gt;, and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myObj = {};
console.log(Object.getPrototypeOf(myObj) === Object.prototype); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;myObj is a plain object.&lt;br&gt;
Its prototype is Object.prototype, which gives it methods like .toString() and .hasOwnProperty().&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;2. The Prototype Chain&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
If you try to access a property or method that doesn’t exist on an object, JavaScript looks up the prototype chain to find it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = { name: "Alice" };
console.log(person.toString()); // "[object Object]"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;person doesn’t have a toString method.&lt;br&gt;
JavaScript checks person’s prototype (Object.prototype), finds toString, and calls it.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This chain ensures objects inherit shared behavior without duplicating code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;3. Constructor Functions and Prototypes&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Constructor functions create objects with shared behavior. Each constructor has a prototype property, and objects created by it inherit from that prototype.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Dog(name) {
  this.name = name;
}
Dog.prototype.bark = function() {
  console.log(`${this.name} says Woof!`);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dog1 = new Dog("Max");
const dog2 = new Dog("Bella");
dog1.bark(); // "Max says Woof!"
dog2.bark(); // "Bella says Woof!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Demonstration✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Dog is a constructor function.&lt;br&gt;
Dog.prototype holds shared methods like bark.&lt;br&gt;
dog1 and dog2 inherit bark via their prototype, saving memory.&lt;/code&gt;&lt;br&gt;
The new keyword links each instance to Dog.prototype.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Why Prototypes Make JavaScript Great&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Prototypes are a big reason JavaScript is so powerful. Here’s how they shine:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;1. Code Reusability&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Prototypes let you define methods once and share them across all instances, making your code DRY (Don’t Repeat Yourself).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Car(model) {
  this.model = model;
}
Car.prototype.drive = function() {
  console.log(`${this.model} is driving!`);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const car1 = new Car("Toyota");
const car2 = new Car("Honda");
car1.drive(); // "Toyota is driving!"
car2.drive(); // "Honda is driving!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**** &lt;code&gt;The drive method lives on Car.prototype, not on each car, saving memory and keeping code clean.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;2. Dynamic Modifications&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
You can add or change prototype properties at runtime, and all objects inheriting from that prototype get the update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Student(name) {
  this.name = name;
}
const student = new Student("Alice");

Student.prototype.sayHello = function() {
  console.log(`Hi, I’m ${this.name}!`);
};

student.sayHello(); // "Hi, I’m Alice!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why It’s Great:&lt;/strong&gt; This flexibility lets you extend functionality without rewriting code, perfect for dynamic apps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;3. Foundation for Modern JavaScript&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Prototypes power ES6 classes, which are syntactic sugar over constructor functions. Understanding prototypes unlocks deeper insights into classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Cat {
  constructor(name) {
    this.name = name;
  }
  meow() {
    console.log(`${this.name} says Meow!`);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const kitty = new Cat("Whiskers");
kitty.meow(); // "Whiskers says Meow!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why It’s Great:&lt;/strong&gt; Under the hood, Cat uses prototypes (Cat.prototype.meow), so knowing prototypes helps you master modern JavaScript.&lt;br&gt;
Common Gotchas and How to Avoid Them&lt;/p&gt;

&lt;p&gt;Prototypes are awesome but can trip you up. Here are two common pitfalls:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gotcha 1: Modifying Shared Prototypes&lt;/strong&gt;&lt;br&gt;
Since prototypes are shared, changing a prototype property affects all instances.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(name) {
  this.name = name;
}
Person.prototype.skills = ["coding"];

const p1 = new Person("Alice");
const p2 = new Person("Bob");
p1.skills.push("design");

console.log(p1.skills); // ["coding", "design"]
console.log(p2.skills); // ["coding", "design"] (Unexpected!)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix: Avoid mutating shared objects on prototypes. Use instance-specific properties instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(name) {
  this.name = name;
  this.skills = ["coding"];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Gotcha 2: Overriding Prototype Methods *&lt;/em&gt;&lt;br&gt;
If an instance defines a property that matches a prototype method, the instance’s property takes precedence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Animal(name) {
  this.name = name;
}
Animal.prototype.speak = function() {
  console.log(`${this.name} makes a sound`);
};

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dog = new Animal("Rex");
dog.speak = function() {
  console.log(`${this.name} barks!`);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dog.speak(); // "Rex barks!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix: Be intentional when overriding. Use unique names or explicitly call the prototype method with &lt;code&gt;Animal.prototype.speak.call(this).&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Prototypes Help JavaScript Stand Out&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;&lt;code&gt;Memory Efficiency:&lt;/code&gt;&lt;/strong&gt; Shared methods on prototypes reduce memory usage, critical for large-scale apps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Inheritance Simplicity:&lt;/code&gt;&lt;/strong&gt; Prototype chains are straightforward compared to class-based systems, making JavaScript accessible yet powerful.&lt;/p&gt;

&lt;p&gt;Ecosystem Power: Libraries like Node.js and frameworks like Vue rely on prototypes for extensible, modular code.&lt;/p&gt;

&lt;p&gt;Without prototypes, JavaScript wouldn’t be the versatile language powering the web today.&lt;br&gt;
Now let’s tie it all together with a practical example—a mini library system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Book(title, author) {
  this.title = title;
  this.author = author;
}

Book.prototype.getDetails = function() {
  return `${this.title} by ${this.author}`;
};

Book.prototype.borrow = function() {
  console.log(`${this.title} has been borrowed`);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const book1 = new Book("1984", "George Orwell");
const book2 = new Book("Pride and Prejudice", "Jane Austen");

console.log(book1.getDetails()); // "1984 by George Orwell"
book2.borrow(); // "Pride and Prejudice has been borrowed"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Book constructor sets instance-specific properties (title, author).&lt;/p&gt;

&lt;p&gt;Book.prototype holds shared methods (getDetails, borrow).&lt;/p&gt;

&lt;p&gt;book1 and book2 inherit these methods via the prototype chain.&lt;/p&gt;

&lt;p&gt;Calling book1.getDetails() looks up the method on Book.prototype.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it’s great:&lt;/strong&gt; &lt;br&gt;
This setup is memory-efficient and scalable, perfect for real-world apps like library management.&lt;br&gt;
&lt;strong&gt;Ok so far so good👍&lt;/strong&gt;&lt;br&gt;
Now, let’s get to the juicy part: inheritance.😎&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inheritance with Prototypes: The Vanilla JS Way&lt;/strong&gt;&lt;br&gt;
Imagine a superhero family where a child hero inherits powers from their parent, plus adds their own. We’ll use prototypes to make this happen.&lt;/p&gt;

&lt;p&gt;Scenario: We have a ParentHero (the parent) with powers like superStrength and a method fightCrime. A ChildHero (the child) inherits these, plus adds their own power, laserEyes, and a method saveTheDay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Parent constructor
function ParentHero(name, strength) {
  this.name = name;
  this.strength = strength;
}
ParentHero.prototype.fightCrime = function() {
  console.log(`${this.name} battles villains with ${this.strength} strength!`);
};

// Child constructor
function ChildHero(name, strength, laserPower) {
  ParentHero.call(this, name, strength); // Inherit parent properties
  this.laserPower = laserPower;
}

// Set up inheritance
ChildHero.prototype = Object.create(ParentHero.prototype);
ChildHero.prototype.constructor = ChildHero; // Fix constructor

// Add child-specific method
ChildHero.prototype.saveTheDay = function() {
  console.log(`${this.name} saves the day with ${this.laserPower} laser power!`);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a child instance
const kidHero = new ChildHero("Super Kid", 100, "mega");
kidHero.fightCrime(); // "Super Kid battles villains with 100 strength!"
kidHero.saveTheDay(); // "Super Kid saves the day with mega laser power!"
console.log(kidHero instanceof ChildHero); // true
console.log(kidHero instanceof ParentHero); // true

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

&lt;/div&gt;



&lt;p&gt;Explanation✍️:&lt;br&gt;
&lt;strong&gt;Parent Setup:&lt;/strong&gt; ParentHero sets name and strength, with fightCrime on its prototype.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Child Setup:&lt;/strong&gt; ChildHero calls ParentHero with .call(this, ...) to inherit name and strength, then adds laserPower.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Link Prototypes:&lt;/strong&gt; ChildHero.prototype = Object.create(ParentHero.prototype) makes ChildHero’s prototype inherit from ParentHero’s, so kidHero can access fightCrime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix Constructor:&lt;/strong&gt; ChildHero.prototype.constructor = ChildHero ensures kidHero’s constructor points to ChildHero.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add Child Method:&lt;/strong&gt; saveTheDay is added to ChildHero.prototype.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create Instance:&lt;/strong&gt; kidHero accesses both parent (fightCrime) and child (saveTheDay) methods via the prototype chain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why It’s Cool:&lt;/strong&gt; This setup lets ChildHero inherit powers without duplicating code, like a kid getting their parent’s strength plus their own laser eyes. It’s memory-efficient and flexible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inheritance with ES6 Classes: The Modern Way&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ES6 (2015) introduced classes, a cleaner syntax for prototypes. The class and extends keywords are syntactic sugar—they still use prototypes under the hood but feel more like traditional OOP. Let’s recreate our superhero family with classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Parent class
class ParentHero {
  constructor(name, strength) {
    this.name = name;
    this.strength = strength;
  }
  fightCrime() {
    console.log(`${this.name} battles villains with ${this.strength} strength!`);
  }
}

// Child class
class ChildHero extends ParentHero {
  constructor(name, strength, laserPower) {
    super(name, strength); // Call parent constructor
    this.laserPower = laserPower;
  }
  saveTheDay() {
    console.log(`${this.name} saves the day with ${this.laserPower} laser power!`);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a child instance
const kidHero = new ChildHero("Super Kid", 100, "mega");
kidHero.fightCrime(); // "Super Kid battles villains with 100 strength!"
kidHero.saveTheDay(); // "Super Kid saves the day with mega laser power!"
console.log(kidHero instanceof ChildHero); // true
console.log(kidHero instanceof ParentHero); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation✍️:&lt;br&gt;
&lt;strong&gt;Parent Class:&lt;/strong&gt; ParentHero defines name, strength, and fightCrime (automatically on ParentHero.prototype).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Child Class:&lt;/strong&gt; ChildHero extends ParentHero sets up inheritance, linking ChildHero.prototype to ParentHero.prototype.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Super Call:&lt;/strong&gt; super(name, strength) calls the parent’s constructor to set name and strength.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Child Properties/Methods:&lt;/strong&gt; laserPower and saveTheDay are added to ChildHero.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create Instance:&lt;/strong&gt; kidHero accesses parent (fightCrime) and child (saveTheDay) methods via the prototype chain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why It’s Cool:&lt;/strong&gt; The class syntax is cleaner and more readable, but it’s still prototypes at heart. extends and super make inheritance feel intuitive, like a kid hero saying, “Thanks, Mom, for the strength—now check out my lasers!”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Practices for Working with Prototypes&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;To make the most of prototypes:&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Use Prototypes for Shared Methods:&lt;/strong&gt; Put reusable methods on the prototype to save memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid Mutating Shared Objects:&lt;/strong&gt; Keep prototype properties immutable or instance-specific.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using ES6 Classes:&lt;/strong&gt; Use classes for cleaner syntax, but understand they rely on prototypes.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>15 Must-Know Hoisting Questions to Crush Your Next Interview🚀</title>
      <dc:creator>Hasan Rifat</dc:creator>
      <pubDate>Wed, 16 Apr 2025 20:22:04 +0000</pubDate>
      <link>https://forem.com/hasan_rifat/15-must-know-hoisting-questions-to-crush-your-next-interview-1c3b</link>
      <guid>https://forem.com/hasan_rifat/15-must-know-hoisting-questions-to-crush-your-next-interview-1c3b</guid>
      <description>&lt;p&gt;(** to Catapult You to Senior JavaScript Developer **)&lt;/p&gt;

&lt;p&gt;✅1.What is hoisting in JavaScript, and how does it affect variable and function declarations?&lt;/p&gt;

&lt;p&gt;✅2.How does hoisting differ between var, let, and const?&lt;/p&gt;

&lt;p&gt;✅3.Why does this code log undefined instead of throwing an error?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
console.log(x);
var x = 42;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅4.What happens when you try to access a let variable before its declaration?&lt;/p&gt;

&lt;p&gt;✅5.Explain why this function call works before the function is defined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
greet(); // "Hello!"
function greet() {
  console.log("Hello!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅6.Why does this function expression throw an error?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
callMe(); // TypeError
var callMe = function() {
  console.log("Hey!");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅7.What’s the output of this code involving variable shadowing?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
var name = "Alice";
function greet() {
  console.log(name); // undefined
  var name = "Bob";
}
greet();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅8.Why does this code prioritize the function over the variable?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
console.log(myFunc); // [Function: myFunc]
var myFunc = "I’m a string!";
function myFunc() {
  console.log("I’m a function!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅9.What happens when you redeclare a variable with var in the same scope?&lt;/p&gt;

&lt;p&gt;✅10.Why does this loop with var cause unexpected output?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
for (var i = 0; i &amp;lt; 3; i++) {
  setTimeout(() =&amp;gt; console.log(i), 1000);
}
// Outputs: 3, 3, 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅11.How does hoisting behave in a function’s block scope?&lt;/p&gt;

&lt;p&gt;✅12.What’s the output of this code with multiple declarations?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
var x = 1;
function x() {}
console.log(x); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅13.Why does this IIFE behave unexpectedly with var?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
var x = 10;
(function() {
  console.log(x); // undefined
  var x = 20;
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅14.How does hoisting affect arrow functions?&lt;/p&gt;

&lt;p&gt;✅15.How can you avoid hoisting-related bugs in production code?&lt;/p&gt;

&lt;p&gt;"Let’s dive into the answers to these 15 hoisting questions, packed with examples to boost your JavaScript mastery! 🚀"&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉1.What is hoisting in JavaScript, and how does it affect variable and function declarations?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Hoisting is JavaScript’s behavior of moving variable and function declarations to the top of their scope (global or function) during the compilation phase, before code execution. Only declarations are hoisted, not initializations. For variables, this means they exist but are undefined until assigned. For function declarations, the entire function is hoisted, making it callable anywhere in the scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(x); // undefined
var x = 5;
sayHello(); // "Hello!"
function sayHello() {
  console.log("Hello!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The var x declaration is hoisted, setting x to undefined. The sayHello function is fully hoisted, so it’s callable early. This shows hoisting’s dual behavior, a core concept for senior developers to explain clearly.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉2.How does hoisting differ between var, let, and const?&lt;br&gt;
&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
All three are hoisted, but var is initialized as undefined, while let and const are placed in the &lt;code&gt;Temporal Dead Zone&lt;/code&gt; (TDZ), making them inaccessible until their declaration. Accessing let/const early throws a ReferenceError.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a); // undefined
var a = 10;
console.log(b); // ReferenceError
let b = 20;
console.log(c); // ReferenceError
const c = 30;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
var a is hoisted and usable (as undefined). let b and const c are hoisted but in the TDZ, enforcing stricter scoping. Senior developers prefer let/const to avoid hoisting-related bugs, showing modern JavaScript mastery.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉3.Why does this code log undefined instead of throwing an error?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➡️Code:
console.log(x);
var x = 42;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answers✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The var x declaration is hoisted to the top of the scope, initializing x as undefined. The assignment x = 42 happens later, so console.log(x) outputs undefined instead of an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Actual code
console.log(x); // undefined
var x = 42;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// How it behaves
var x;
console.log(x); // undefined
x = 42;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This tests your understanding of var hoisting and initialization. A senior developer can explain why no error occurs and advocate for let to avoid such quirks.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉4.What happens when you try to access a let variable before its declaration?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Accessing a let variable before its declaration throws a ReferenceError because it’s in the Temporal Dead Zone (TDZ). Although hoisted, let variables are not initialized until their declaration line.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 100;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The TDZ enforces strict scoping, preventing early access. This question tests your grasp of modern JavaScript and why let is safer than var.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉5.Explain why this function call works before the function is defined&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code:
greet(); // "Hello!"
function greet() {
  console.log("Hello!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Function declarations are fully hoisted, including their name and body. The JavaScript engine moves the entire greet function to the top of the scope, making it callable anywhere.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Actual code
greet(); // "Hello!"
function greet() {
  console.log("Hello!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// How it behaves
function greet() {
  console.log("Hello!");
}
greet();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This highlights hoisting’s power with functions, a key point for senior developers to articulate, especially when discussing code organization.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉6.Why does this function expression throw an error?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code:
callMe(); // TypeError
var callMe = function() {
  console.log("Hey!");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Function expressions are treated as variable declarations. The var callMe is hoisted, initialized as undefined, but the function assignment happens later. Calling undefined() causes a TypeError.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Actual code
callMe(); // TypeError
var callMe = function() {
  console.log("Hey!");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// How it behaves
var callMe;
callMe(); // TypeError
callMe = function() {
  console.log("Hey!");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This tests your ability to differentiate function declarations from expressions, a nuanced skill for senior roles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉7.What’s the output of this code involving variable shadowing?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code:
var name = "Alice";
function greet() {
  console.log(name); // undefined
  var name = "Bob";
}
greet();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The var name inside greet is hoisted to the top of the function, shadowing the outer name. It’s undefined until assigned, so console.log(name) outputs undefined.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// How it behaves
var name = "Alice";
function greet() {
  var name;
  console.log(name); // undefined
  name = "Bob";
}
greet();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This tests scoping and hoisting interactions, a common senior-level topic. Using let avoids shadowing issues, showing modern coding practices.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;8.Why does this code prioritize the function over the variable?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code:
console.log(myFunc); // [Function: myFunc]
var myFunc = "I’m a string!";
function myFunc() {
  console.log("I’m a function!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Function declarations are hoisted before variable declarations. The function myFunc is hoisted first, then var myFunc (which doesn’t overwrite it initially). The assignment myFunc = "I’m a string!" happens later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// How it behaves
function myFunc() {
  console.log("I’m a function!");
}
var myFunc;
console.log(myFunc); // [Function: myFunc]
myFunc = "I’m a string!";

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This tests hoisting precedence, a tricky area where senior developers excel by explaining engine behavior and advocating clear naming.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉9.What happens when you redeclare a variable with var in the same scope?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Redeclaring a var variable in the same scope is allowed and doesn’t throw an error. The declarations are merged, and the last assignment wins.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var x = 1;
var x;
console.log(x); // 1
var x = 2;
console.log(x); // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
All var x declarations are hoisted and merged. This flexibility can cause bugs, so senior developers prefer let/const, which throw errors on redeclaration.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉10.Why does this loop with var cause unexpected output?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code:
for (var i = 0; i &amp;lt; 3; i++) {
  setTimeout(() =&amp;gt; console.log(i), 1000);
}
// Outputs: 3, 3, 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
var is function-scoped, so i is shared across all setTimeout callbacks. By the time the callbacks run, the loop has finished, and i is 3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Fix with let:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; 3; i++) {
  setTimeout(() =&amp;gt; console.log(i), 1000);
}
// Outputs: 0, 1, 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
let creates a new i per iteration, fixing the issue. This tests closure and hoisting interactions, a senior-level skill for async code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;👉11.How does hoisting behave in a function’s block scope?&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
var ignores block scope and is hoisted to the function or global scope. let and const respect block scope and are hoisted to the block’s top, staying in the TDZ.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function test() {
  if (true) {
    var x = 10;
    let y = 20;
  }
  console.log(x); // 10
  console.log(y); // ReferenceError
}
test();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
var x is hoisted to the function scope, while let y is block-scoped and inaccessible outside. This shows why let/const are preferred for clarity.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉12.What’s the output of this code with multiple declarations?&lt;br&gt;
&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code:
var x = 1;
function x() {}
console.log(x); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Function declarations are hoisted first, then var declarations. The var x = 1 assignment overwrites the function, so console.log(x) outputs 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// How it behaves
function x() {}
var x;
x = 1;
console.log(x); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This tests hoisting precedence and assignment, a nuanced topic for senior developers to navigate.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉13.Why does this IIFE behave unexpectedly with var?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Code:
var x = 10;
(function() {
  console.log(x); // undefined
  var x = 20;
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The var x inside the IIFE is hoisted to the top of its function scope, shadowing the outer x. It’s undefined until assigned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// How it behaves
var x = 10;
(function() {
  var x;
  console.log(x); // undefined
  x = 20;
})();

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
This tests IIFE scoping and hoisting, common in modular code. Senior developers use let to avoid such surprises.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉14.How does hoisting affect arrow functions?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Arrow functions are function expressions, so their hoisting depends on the variable declaration (var, let, const). They’re not hoisted like function declarations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;callArrow(); // ReferenceError
const callArrow = () =&amp;gt; console.log("Arrow!");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
const callArrow is hoisted but in the TDZ, causing an error. This tests modern JavaScript syntax, critical for senior roles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;👉15.How can you avoid hoisting-related bugs in production code?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Answer✍️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
To prevent hoisting issues:&lt;br&gt;
➡️Use let/const for block scoping and TDZ protection.&lt;/p&gt;

&lt;p&gt;➡️Declare variables at the top of their scope.&lt;/p&gt;

&lt;p&gt;➡️Define functions before calling them.&lt;/p&gt;

&lt;p&gt;➡️Avoid name clashes between variables and functions.&lt;/p&gt;

&lt;p&gt;➡️Use strict mode to catch undeclared variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Example:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";
function safeCode() {
  let x = 10; // Declared first
  console.log(x); // 10
}
safeCode();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👁️‍🗨️:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
These practices ensure predictable code, a senior developer’s responsibility in leading teams and building robust systems.&lt;/p&gt;

&lt;p&gt;Got a hoisting trick that saved your code or a quirky bug you’ve tackled? Share it in the comments—I’d love to hear your story and swap tips! 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>JavaScript Hoisting: 🚀 What’s Going On Behind the Scenes?🤔</title>
      <dc:creator>Hasan Rifat</dc:creator>
      <pubDate>Wed, 16 Apr 2025 07:41:33 +0000</pubDate>
      <link>https://forem.com/hasan_rifat/javascript-hoisting-whats-going-on-behind-the-scenes-2n6m</link>
      <guid>https://forem.com/hasan_rifat/javascript-hoisting-whats-going-on-behind-the-scenes-2n6m</guid>
      <description>&lt;p&gt;Hey👋 JavaScript fans! Ever wondered🙄 why your code behaves in weird ways, like variables working before you declare them? Or why a function runs fine even if you call it before defining it? That’s hoisting at play, and it’s one of JavaScript’s quirkiest features. Don’t worry if it sounds confusing—I’m here to break it down in a fun, easy way with examples that’ll make hoisting crystal clear. By the end, you’ll not only get what hoisting is but also how to avoid its sneaky traps. Let’s dive in!😎&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;What Is Hoisting, Anyway?&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Hoisting is JavaScript’s way of &lt;code&gt;moving variable&lt;/code&gt; and &lt;code&gt;function declarations&lt;/code&gt; to the &lt;code&gt;top of their scope&lt;/code&gt; before the code runs. It’s like the JavaScript engine saying, “Hey👋, I’ll make sure these things exist before you use them!” But here’s the catch: only the declarations get hoisted, not the initializations (the values you assign). This can lead to some head-scratching moments if you’re not careful.&lt;/p&gt;

&lt;p&gt;Think of it like setting up a party: JavaScript puts all the chairs🙂 (declarations) in place before the guests arrive, but it doesn’t fill them with people (values) until the code runs. Let’s see how this works with variables and functions.&lt;/p&gt;

&lt;p&gt;Hoisting with Variables: var, let, and const&lt;/p&gt;

&lt;p&gt;JavaScript has three ways to declare variables—&lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;—and hoisting behaves differently for each. Let’s start with the classic: var.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Hoisting with var&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
When you declare a variable with var, the declaration gets hoisted to the top of its scope (global or function scope). But the value you assign stays where it is. This means the variable exists but is undefined until you assign it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;code&gt;👉Example 1:&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(myVar);//undefined
var myVar = 42;
console.log(myVar); // 42

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;code&gt;What’s Happening🤔?&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
JavaScript hoists the declaration var myVar to the top, so the code behaves like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var myVar; // Hoisted: myVar is undefined&lt;br&gt;
console.log(myVar); // undefined&lt;br&gt;
myVar = 42; // Assignment happens here&lt;br&gt;
console.log(myVar); // 42&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since only the declaration is hoisted, myVar is undefined when we first log it. This is why you can access var variables before declaring them, but you get undefined instead of an error.&lt;/p&gt;

&lt;p&gt;✍️Tips: Always declare var variables at the top of their scope to avoid confusion. Hoisting can make code tricky to read!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Hoisting with let and const&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unlike var, let and const are also hoisted, but there’s a twist: they’re placed in a &lt;code&gt;Temporal Dead Zone (TDZ).&lt;/code&gt; This means you can’t access them before their declaration, or you’ll get a ReferenceError. &lt;code&gt;The TDZ is JavaScript’s way of encouraging better coding practices.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;👉&lt;code&gt;Example 2:&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(myLet); // ReferenceError: Cannot access 'myLet' before initialization
let myLet = 100;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;What’s Happening?🤔&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
The declaration let myLet is hoisted, but it’s in the TDZ until the code reaches the actual declaration. Trying to access it early throws an error. The same applies to const:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(myConst); // ReferenceError
const myConst = "Hello";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;✅Key Difference:&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;var&lt;/strong&gt;: Hoisted and initialized as undefined, so no error but risky.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let/const&lt;/strong&gt;: Hoisted but in the TDZ, so you get an error if accessed early.&lt;/p&gt;

&lt;p&gt;✍️Using let and const helps avoid bugs from hoisting’s surprises, as they enforce stricter rules. Stick with them for modern JavaScript!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;👉Hoisting with Functions&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Functions are where hoisting really shines. JavaScript hoists function declarations fully—both the name and the body—so you can call them before defining them. But function expressions behave differently. Let’s break it down.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;code&gt;Function Declarations&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A function declaration looks like this: &lt;code&gt;function myFunc() {}&lt;/code&gt;. It gets hoisted completely, so you can call it anywhere in its scope.&lt;/p&gt;

&lt;p&gt;Example 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sayHello(); // "Hello, world!"
function sayHello() {
  console.log("Hello, world!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;What’s Happening?🤔&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
JavaScript hoists the entire function to the top, so it’s ready to use before the code runs. It’s like the function was written like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sayHello() {
  console.log("Hello, world!");
}
sayHello(); // "Hello, world!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is super handy but can make code less readable if functions are scattered. Best practice? Define functions before calling them for clarity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Function Expressions&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A function expression assigns a function to a variable, like &lt;code&gt;const myFunc = function() {}&lt;/code&gt;. Here, hoisting depends on the variable declaration (var, let, or const).&lt;/p&gt;

&lt;p&gt;Example 4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myFunc(); // TypeError: myFunc is not a function
var myFunc = function() {
  console.log("I’m a function!");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;What’s Happening?🤔&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
The var myFunc declaration is hoisted, but it’s undefined until the assignment. Calling undefined() causes a TypeError. The code behaves like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myFunc; // Hoisted: undefined
myFunc(); // TypeError
myFunc = function() {
  console.log("I’m a function!");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you use let or const, you’d get a ReferenceError due to the &lt;code&gt;TDZ&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myFunc(); // ReferenceError
let myFunc = function() {
  console.log("I’m a function!");
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉&lt;strong&gt;Takeaway&lt;/strong&gt;: Function declarations are hoisted fully, but function expressions follow variable hoisting rules. Stick to function declarations for early calls or ensure expressions are defined first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Real-World Gotchas:&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;Hoisting in Action&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Hoisting can cause bugs if you’re not careful. Let’s look at a couple of common traps and how to avoid them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;✍Gotcha 1: Variable Shadowing&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
When a variable is redeclared in a different scope, hoisting can lead to confusion.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;Example 5:&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name = "Alice";
function greet() {
  console.log(name); // undefined
  var name = "Bob";
}
greet();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;What’s Happening?🤔&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
The var name inside greet is hoisted to the top of the function, shadowing the outer name. The code behaves like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name = "Alice";
function greet() {
  var name; // Hoisted: undefined
  console.log(name); // undefined
  name = "Bob";
}
greet();

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;code&gt;✅Fix: Avoid redeclaring&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; variables in inner scopes, or use let to limit scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Alice";
function greet() {
  let name = "Bob"; // No shadowing confusion
  console.log(name); // "Bob"
}
greet();
console.log(name); // "Alice"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Gotcha 2: Function vs. Variable Hoisting&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Function declarations take precedence over variable declarations with the same name, but assignments can overwrite them.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;Example 6:&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(myFunc); // [Function: myFunc]
var myFunc = "I’m a string!";
console.log(myFunc); // "I’m a string!"
function myFunc() {
  console.log("I’m a function!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;What’s Happening?🤔&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
JavaScript hoists the function declaration first, then the variable declaration. The code behaves like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunc() {
  console.log("I’m a function!");
}
var myFunc; // Hoisted, but doesn’t overwrite function
console.log(myFunc); // [Function: myFunc]
myFunc = "I’m a string!"; // Assignment overwrites
console.log(myFunc); // "I’m a string!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;✅Fix: Avoid using the same name for functions and variables.&lt;/code&gt;&lt;/strong&gt; It’s confusing and error-prone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;✍️Best Practices to Tame Hoisting&lt;br&gt;
&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Hoisting is cool, but it can make your code unpredictable. Here are some tips to keep things under control:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;✅Use let and const:&lt;/code&gt;&lt;/strong&gt; They’re stricter than var and help avoid undefined surprises.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;✅Declare Variables Up Top:&lt;/code&gt;&lt;/strong&gt; Even with var, put declarations at the start of their scope for clarity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;✅Define Functions First:&lt;/code&gt;&lt;/strong&gt;Call function declarations after defining them to make code readable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;✅Avoid Name Clashes:&lt;/code&gt;&lt;/strong&gt; Don’t reuse names for variables and functions in the same scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;✅Understand Scope:&lt;/code&gt;&lt;/strong&gt; Know whether you’re in global, function, or block scope to predict hoisting behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Why Hoisting Matters&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding hoisting isn’t just about avoiding bugs—it’s about mastering how JavaScript works under the hood. Whether you’re debugging a tricky issue, writing clean code, or acing a technical interview, hoisting knowledge sets you apart. It’s like knowing the &lt;code&gt;secret handshake&lt;/code&gt; of the JavaScript engine!&lt;/p&gt;

&lt;p&gt;Plus, hoisting questions pop up a lot in interviews. If you can explain why console.log(x); var x = 5; logs undefined, you’re showing you’ve got the chops to handle JavaScript’s quirks.&lt;/p&gt;

&lt;p&gt;✍️Hoisting might seem like JavaScript’s way of playing tricks, but once you get it, it’s no big deal. Variables with var are hoisted as undefined, let and const stay in the TDZ, and function declarations are ready to roll from the start. By knowing these rules and avoiding common pitfalls, you’ll write cleaner code and dodge those “Why is this undefined?!” moments.&lt;/p&gt;

&lt;p&gt;Try playing with the examples above in your browser’s console.&lt;br&gt;
Got a hoisting horror story or a cool tip? I’d love to hear it—share in the comments and let’s geek out together!&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>20 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗖𝗹𝗼𝘀𝘂𝗿𝗲 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 𝘁𝗼 𝗡𝗮𝗶𝗹 𝗬𝗼𝘂𝗿 𝗡𝗲𝘅𝘁 𝗧𝗲𝗰𝗵 𝗥𝗼𝗹𝗲🚀</title>
      <dc:creator>Hasan Rifat</dc:creator>
      <pubDate>Sat, 12 Apr 2025 06:54:32 +0000</pubDate>
      <link>https://forem.com/hasan_rifat/20-267j</link>
      <guid>https://forem.com/hasan_rifat/20-267j</guid>
      <description>&lt;p&gt;(** to elevate you from junior to senior role😎**)&lt;/p&gt;

&lt;p&gt;✅How does a closure work?&lt;/p&gt;

&lt;p&gt;✅Can you write a function that creates a private variable using a closure?&lt;/p&gt;

&lt;p&gt;✅Explain why the following code outputs 3 three times, and how would you fix it?&lt;br&gt;
   for (var i = 0; i &amp;lt; 3; i++) {&lt;br&gt;
     setTimeout(function() {&lt;br&gt;
       console.log(i);&lt;br&gt;
     }, 1000);&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;✅How can closures be used to create a memoization function?&lt;/p&gt;

&lt;p&gt;✅Write a function that generates unique IDs using a closure.&lt;/p&gt;

&lt;p&gt;✅What are the potential downsides of using closures?&lt;/p&gt;

&lt;p&gt;✅Can you use a closure to throttle a function?&lt;/p&gt;

&lt;p&gt;✅How would you use a closure to create a module pattern?&lt;/p&gt;

&lt;p&gt;✅How does a closure interact with event listeners?&lt;/p&gt;

&lt;p&gt;✅Explain the difference between a closure and a regular function.&lt;/p&gt;

&lt;p&gt;✅How can you use a closure to create a function that only runs once?&lt;/p&gt;

&lt;p&gt;✅Write a closure-based function to maintain a history of values.&lt;/p&gt;

&lt;p&gt;✅How would you use a closure to create a factory function for generating customized greetings?&lt;/p&gt;

&lt;p&gt;✅Can you explain how closures are used in currying, and provide an example?&lt;/p&gt;

&lt;p&gt;✅How can closures cause memory leaks, and how would you prevent them?&lt;/p&gt;

&lt;p&gt;✅Write a closure to limit the number of times a function can be called.&lt;/p&gt;

&lt;p&gt;✅How would you use a closure to create a toggle function?&lt;/p&gt;

&lt;p&gt;✅Can you create a closure to simulate a class-like structure without using the class keyword?&lt;/p&gt;

&lt;p&gt;✅How do closures interact with asynchronous code, like Promises?&lt;/p&gt;

&lt;p&gt;✅Write a closure-based function to debounce user input.&lt;/p&gt;

&lt;p&gt;Now let's explain all of the given question one by one👋&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;1.What is a closure in JavaScript, and how does it work?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure is a function that remembers the variables in its outer scope, even when executed outside that scope. It’s created due to JavaScript’s lexical scoping, where a function’s scope is defined by where it’s written in the code. When an inner function references outer variables, JavaScript preserves those variables in memory, forming a closure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function outer() {
  let count = 0;
  return function inner() {
    count++;
    return count;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const counter = outer();
console.log(counter()); // 1
console.log(counter()); // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The inner function closes over count, so even after outer finishes, count persists in memory. Each call to counter() increments it, showing how closures maintain state. This is a core concept for interviews, as it &lt;strong&gt;&lt;code&gt;tests your grasp of scope and memory.&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;2.Can you write a function that creates a private variable using a closure?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Closures are perfect for encapsulation, letting you create private variables that can’t be accessed directly. By defining a variable in an outer function and exposing methods to interact with it, you ensure data privacy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createPerson() {
  let name = "Alice"; // Private variable
  return {
    getName: function() {
      return name;
    },
    setName: function(newName) {
      name = newName;
    }
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const person = createPerson();
console.log(person.getName()); // "Alice"
person.setName("Bob");
console.log(person.getName()); // "Bob"
console.log(person.name); // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;br&gt;
name is private because it’s only accessible through getName and setName. This pattern mimics &lt;strong&gt;&lt;code&gt;private properties in OOP, a skill senior developers use to write secure, modular code javaScript&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;3.Explain why the following code outputs 3 three times, and how would you fix it?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;code&gt;for (var i = 0; i &amp;lt; 3; i++) {&lt;br&gt;
  setTimeout(function() {&lt;br&gt;
    console.log(i);&lt;br&gt;
  }, 1000);&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
✍️This outputs 3-three times because &lt;code&gt;var is function-scoped, not block-scoped&lt;/code&gt;. The setTimeout callbacks all reference the same i, which is 3 by the time the loop finishes and the callbacks run.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Fix using let:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; 3; i++) {
  setTimeout(function() {
    console.log(i);
  }, 1000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Outputs: 0, 1, 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Fix using IIFE (if using var):&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var i = 0; i &amp;lt; 3; i++) {
  (function(j) {
    setTimeout(function() {
      console.log(j);
    }, 1000);
  })(i);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Outputs: 0, 1, 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Using let creates a new i for each iteration, so each closure captures the correct value. An IIFE creates a new scope per iteration, passing i as j. This question tests your understanding of scoping and closures in loops—a common interview gotcha.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;4.How can closures be used to create a memoization function?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Memoization optimizes functions by caching results for repeated inputs. A closure can maintain a private cache, storing computed values for reuse.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function memoize(fn) {
  let cache = {};
  return function(...args) {
    let key = JSON.stringify(args);
    if (key in cache) {
      return cache[key];
    }
    cache[key] = fn(...args);
    return cache[key];
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function slowFib(n) {
  if (n &amp;lt;= 1) return n;
  return slowFib(n - 1) + slowFib(n - 2);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoFib = memoize(slowFib);
console.log(memoFib(10)); // 55 (computed)
console.log(memoFib(10)); // 55 (cached)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over cache stores results, making subsequent calls faster. Senior developers use memoization to optimize performance, and this question tests that mindset.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;5.Write a function that generates unique IDs using a closure.&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure can maintain a private counter to generate unique IDs, ensuring no external interference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createIdGenerator() {
  let id = 0;
  return function() {
    return id++;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const generateId = createIdGenerator();
console.log(generateId()); // 0
console.log(generateId()); // 1
console.log(generateId()); // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over id keeps it private and increments it per call. This is useful for generating keys in apps, showing practical closure application.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;6.What are the potential downsides of using closures?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;code&gt;While powerful, closures have trade-offs:&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Memory Usage:&lt;/code&gt;&lt;/strong&gt; Closed-over variables stay in memory until the closure is garbage-collected, potentially causing leaks if mismanaged.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Performance:&lt;/code&gt;&lt;/strong&gt; Creating many closures (e.g., in loops) can increase memory and processing overhead.&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Complexity:&lt;/code&gt;&lt;/strong&gt; Overuse can make code harder to debug or maintain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function leaky() {
  let bigData = new Array(1000000).fill("data");
  return () =&amp;gt; console.log(bigData.length);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, bigData persists unnecessarily, consuming memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Senior developers balance closures with cleanup (like nullifying references or removing event listeners) to avoid issues, a key interview discussion point.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;7.Can you use a closure to throttle a function?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Throttling limits how often a function runs, useful for performance. A closure can track the last execution time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function throttle(fn, delay) {
  let lastCall = 0;
  return function(...args) {
    let now = Date.now();
    if (now - lastCall &amp;gt;= delay) {
      lastCall = now;
      return fn(...args);
    }
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const log = () =&amp;gt; console.log("Throttled!");
const throttledLog = throttle(log, 1000);
setInterval(throttledLog, 200);
// Logs every ~1000ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over lastCall ensures the function runs only after delay has passed. This is common in UI optimizations, showing senior-level thinking.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;8.How would you use a closure to create a module pattern?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️The module pattern uses a closure to create private and public members, mimicking encapsulation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myModule = (function() {
  let privateData = "Secret";
  function privateMethod() {
    return `Accessing ${privateData}`;
  }

  return {
    publicMethod: function() {
      return privateMethod();
    },
    publicData: "Public info"
  };
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(myModule.publicMethod()); // "Accessing Secret"
console.log(myModule.privateData); // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The IIFE creates a closure, keeping privateData and privateMethod hidden. This pattern is used in libraries, testing your modular design skills.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;9.How does a closure interact with event listeners?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Closures in event listeners retain outer variables, enabling dynamic behavior in response to events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setupButton() {
  let count = 0;
  const button = document.createElement("button");
  button.textContent = "Click me";
  button.addEventListener("click", () =&amp;gt; {
    count++;
    console.log(`Clicked ${count} times`);
  });
  document.body.appendChild(button);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;setupButton();&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;Explane listener’s closure over count tracks clicks. Senior developers know to remove listeners to prevent leaks, a nuance interviewers may probe.&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;10. Explain the difference between a closure and a regular function.&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A regular function accesses its own variables and parameters but loses outer variables after their scope ends. A closure retains access to its outer scope’s variables indefinitely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Regular function
function add(a, b) {
  return a + b;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(add(2, 3)); // 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Closure
function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const addFive = makeAdder(5);
console.log(addFive(3)); // 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure in makeAdder remembers x, unlike add, which doesn’t persist state. This tests your ability to articulate core differences.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;11.How can you use a closure to create a function that only runs once?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure can track execution with a flag, ensuring a function runs only once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createOnceRunner() {
  let hasRun = false;
  return function() {
    if (!hasRun) {
      hasRun = true;
      console.log("This runs only once!");
    } else {
      console.log("Already ran!");
    }
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const runOnce = createOnceRunner();
runOnce(); // "This runs only once!"
runOnce(); // "Already ran!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over hasRun controls execution, useful for initialization tasks in app.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;12.Write a closure-based function to maintain a history of values.&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure can store an array to track values over time, keeping it private.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createHistoryTracker() {
  let history = [];
  return function(value) {
    history.push(value);
    return history;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const tracker = createHistoryTracker();
console.log(tracker("Step 1")); // ["Step 1"]
console.log(tracker("Step 2")); // ["Step 1", "Step 2"]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over history preserves the state, a pattern for logging or undo features.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;13.How would you use a closure to create a factory function for generating customized greetings?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure can customize functions by retaining configuration parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createGreeter(greeting) {
  return function(name) {
    return `${greeting}, ${name}!`;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sayHello = createGreeter("Hello");
console.log(sayHello("Alice")); // "Hello, Alice!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Each returned function remembers its greeting, showing how closures enable reusable patterns.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;14.Can you explain how closures are used in currying, and provide an example?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Currying splits a multi-argument function into single-argument functions, using closures to hold intermediate values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function curryAdd(a) {
  return function(b) {
    return function(c) {
      return a + b + c;
    };
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(curryAdd(1)(2)(3)); // 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
Each inner function closes over outer parameters, enabling functional flexibility—a senior-level skill.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;15.How can closures cause memory leaks, and how would you prevent them?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Closures keep variables in memory, which can lead to leaks if not cleaned up, especially with large objects or event listeners.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setupLeak() {
  let heavyData = new Array(1000000).fill("data");
  document.getElementById("btn").addEventListener("click", () =&amp;gt; {
    console.log(heavyData.length);
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Prevention:&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove listeners: removeEventListener.&lt;/li&gt;
&lt;li&gt;Nullify references: heavyData = null.&lt;/li&gt;
&lt;li&gt;Use WeakMap for temporary data.
&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;
Senior developers proactively manage memory, a key interview topic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;16.Write a closure to limit the number of times a function can be called.&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure can track call counts to enforce a limit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function limitCalls(fn, maxCalls) {
  let calls = 0;
  return function(...args) {
    if (calls &amp;lt; maxCalls) {
      calls++;
      return fn(...args);
    }
    return "Max calls reached!";
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const limitedLog = limitCalls(console.log, 2);
limitedLog("First"); // "First"
limitedLog("Second"); // "Second"
limitedLog("Third"); // "Max calls reached!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over calls ensures controlled execution, useful for APIs or trials.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;17.How would you use a closure to create a toggle function?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure can maintain a boolean state for toggling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createToggle() {
  let isOn = false;
  return function() {
    isOn = !isOn;
    return isOn ? "On" : "Off";
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const toggle = createToggle();
console.log(toggle()); // "On"
console.log(toggle()); // "Off"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over isOn tracks state privately, ideal for UI controls.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;18.Can you create a closure to simulate a class-like structure without using the class keyword?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️A closure can encapsulate data and methods, mimicking a class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createStudent(name) {
  let score = 0;
  return {
    getName: () =&amp;gt; name,
    addScore: (points) =&amp;gt; {
      score += points;
      return score;
    },
    getScore: () =&amp;gt; score
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const student = createStudent("Alice");
console.log(student.addScore(10)); // 10
console.log(student.getScore()); // 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure hides score, offering controlled access—a pre-ES6 class alternative.&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;19.How do closures interact with asynchronous code, like Promises?&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Closures let async operations access outer variables after delays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchWithContext(id) {
  let context = `Request-${id}`;
  return new Promise((resolve) =&amp;gt; {
    setTimeout(() =&amp;gt; {
      resolve(`${context}: Done`);
    }, 1000);
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetchWithContext(1).then(console.log); // "Request-1: Done"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The Promise’s closure over context ensures correct data, common in API calls&lt;/p&gt;

&lt;p&gt;👉&lt;em&gt;&lt;strong&gt;&lt;code&gt;20.Write a closure-based function to debounce user input.&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
✍️Debouncing delays function execution until rapid calls stop, using a closure to track timeouts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function debounce(fn, delay) {
  let timeoutId;
  return function(...args) {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() =&amp;gt; fn(...args), delay);
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const logInput = debounce((value) =&amp;gt; console.log(`Input: ${value}`), 500);
logInput("a");
logInput("b");
setTimeout(() =&amp;gt; logInput("c"), 200);
// Logs "Input: c" after ~500ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;Explanation👀:&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The closure over timeoutId ensures only the last call runs, optimizing search or resize handlers.&lt;/p&gt;

&lt;p&gt;Which of these closure questions challenged you the most? Or do you have a closure tip that’s helped you level up? Drop a comment below—I’d love to hear your thoughts and keep the conversation going!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Understanding Closures in JavaScript: A Deep Dive</title>
      <dc:creator>Hasan Rifat</dc:creator>
      <pubDate>Wed, 09 Apr 2025 07:37:24 +0000</pubDate>
      <link>https://forem.com/hasan_rifat/understanding-closures-in-javascript-a-deep-dive-1ma5</link>
      <guid>https://forem.com/hasan_rifat/understanding-closures-in-javascript-a-deep-dive-1ma5</guid>
      <description>&lt;p&gt;👉JavaScript is a language full of surprises, and one of its most intriguing features is closures. If you’ve ever wondered how a function can "remember" variables from its outer scope long after that scope has vanished, you’re about to uncover the magic of closures. In this blog, I will explore what closures are, how they work, and why they’re a game-changer in JavaScript programming. With practical examples, you’ll see closures in action and learn how to wield them effectively in your own code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Is a Closure?&lt;/strong&gt;&lt;br&gt;
A closure is a function that retains access to the variables in its outer (enclosing) scope, even after that scope has finished executing. This behavior stems from JavaScript’s lexical scoping, where a function’s scope is defined by where it’s written in the code, not where it’s called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In simpler terms:&lt;/strong&gt;&lt;br&gt;
A closure is created when an inner function references variables from an outer function.&lt;br&gt;
The inner function "closes over" these variables, preserving them in memory for as long as the function exists.&lt;br&gt;
This might sound abstract, so &lt;code&gt;let’s jump into an example&lt;/code&gt; to make it concrete.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;A Simple Closure Example&lt;br&gt;
Consider this code:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greetUser() {
  let name = "Alice";

  function sayHello() {
    console.log(`Hello, ${name}!`);
  }

  return sayHello;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greeting = greetUser(); // greetUser executes and returns sayHello
greeting(); // Output: "Hello, Alice!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;👉Demonstration👀:&lt;/code&gt;&lt;br&gt;
greetUser defines a variable name and an inner function sayHello.&lt;br&gt;
sayHello references name from its outer scope.&lt;br&gt;
When greetUser finishes executing, it returns sayHello.&lt;br&gt;
Even though greetUser is done, calling greeting() still logs "Hello, Alice!" because sayHello remembers name through a closure.&lt;br&gt;
This "memory" is what makes closures so powerful—and a little mind-bending at first!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✍️How Closures Work Under the Hood&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;To understand closures, you need to grasp two key concepts:&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scope Chain:&lt;/strong&gt; Every function in JavaScript has a scope chain, which includes its own variables and those of its outer scopes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Garbage Collection:&lt;/strong&gt; Normally, when a function finishes executing, its variables are cleaned up from memory. But if an inner function references an outer variable, JavaScript keeps that variable alive as part of the closure.
&lt;code&gt;In our greetUser example&lt;/code&gt;, the sayHello function maintains a reference to name. JavaScript ensures name isn’t garbage-collected, allowing sayHello to use it later.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;Practical Use Case: Building a Private Counter&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
One of the most popular uses of closures is creating private variables—data that can’t be accessed directly from the outside. Let’s build a counter to demonstrate this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createCounter() {
  let count = 0; // Private variable

  return {
    increment: function() {
      count++;
      return count;
    },
    decrement: function() {
      count--;
      return count;
    },
    getCount: function() {
      return count;
    }
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myCounter = createCounter();
console.log(myCounter.increment()); // 1
console.log(myCounter.increment()); // 2
console.log(myCounter.decrement()); // 1
console.log(myCounter.getCount()); // 1frankly
console.log(myCounter.count); // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;**What’s happening here?**&lt;/code&gt;&lt;br&gt;
count is defined inside createCounter and is inaccessible from the outside.&lt;br&gt;
The returned object contains methods (increment, decrement, getCount) that can manipulate count because they form closures over it.&lt;br&gt;
Trying to access myCounter.count directly yields undefined, proving count is private.&lt;br&gt;
This pattern mimics encapsulation, a principle common in object-oriented programming, and it’s all thanks to closures!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;Closures in the Real World: Timers&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Closures shine in asynchronous scenarios, like event handlers or timers. &lt;br&gt;
&lt;em&gt;&lt;strong&gt;&lt;code&gt;Here’s an example using setTimeout:&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function delayedMessage(message) {
  let text = message;

  setTimeout(function() {
    console.log(text);
  }, 2000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;delayedMessage("Hello after 2 seconds!"); // Logs after 2 seconds&lt;/code&gt;&lt;br&gt;
Even though delayedMessage finishes executing immediately, the anonymous function inside setTimeout retains access to text via a closure. When the timer fires two seconds later, it still knows what text is.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;Potential Pitfalls: Watch Your Loops&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Closures can trip you up if you’re not careful, especially in loops. &lt;br&gt;
&lt;em&gt;&lt;strong&gt;&lt;code&gt;Consider this common gotcha:&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var i = 0; i &amp;lt; 3; i++) {
  setTimeout(function() {
    console.log(i);
  }, 1000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;// Output (after 1 second): 3, 3, 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why does this print 3 three times?&lt;/strong&gt; &lt;br&gt;
Because var is function-scoped, and by the time the setTimeout callbacks run, the loop has already finished, leaving i at 3. Each callback shares the same i via a closure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;To fix this, use let (block-scoped) instead:&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; 3; i++) {
  setTimeout(function() {
    console.log(i);
  }, 1000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;// Output (after 1 second): 0, 1, 2&lt;/code&gt;&lt;br&gt;
With let, each iteration creates a new i, and the closure captures the correct value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Closures Matter&lt;/strong&gt;&lt;br&gt;
Closures aren’t just a quirky feature—they’re a cornerstone of JavaScript’s flexibility. &lt;em&gt;&lt;strong&gt;&lt;code&gt;Here’s why they’re worth mastering:&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;1. Data Privacy:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
 Encapsulate logic and protect variables, as seen in the counter example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;2. Callbacks and Events:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
 Power asynchronous code in event-driven applications.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;3. Functional Programming:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Enable advanced techniques like currying and memoization.&lt;br&gt;
&lt;strong&gt;Wrapping Up&lt;/strong&gt;&lt;br&gt;
Closures might feel like a puzzle at first, but once you understand them, they become a superpower in your JavaScript toolkit. They let functions carry their context with them, opening the door to creative and efficient coding patterns. Whether you’re building &lt;code&gt;private APIs&lt;/code&gt;, &lt;code&gt;handling async operations&lt;/code&gt;, or just &lt;code&gt;exploring JavaScript’s depths&lt;/code&gt;, closures are a concept you’ll encounter—and appreciate—time and again.&lt;/p&gt;

&lt;p&gt;So, go experiment! Write a closure, break it🤜, fix it👍, and watch your understanding grow.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;&lt;code&gt;Have a favorite closure use case? I’d love to hear about it in the comments.&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Happy coding!👋&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The Big Picture of React Component</title>
      <dc:creator>Hasan Rifat</dc:creator>
      <pubDate>Tue, 08 Oct 2024 14:35:53 +0000</pubDate>
      <link>https://forem.com/hasan_rifat/the-big-picture-of-react-component-1o1m</link>
      <guid>https://forem.com/hasan_rifat/the-big-picture-of-react-component-1o1m</guid>
      <description>&lt;p&gt;In React, a &lt;code&gt;component&lt;/code&gt; is like a building block for your user interface(UI). Think of it as a small, reusable chunk of code that defines how a particular part of your page should look and behave. These &lt;code&gt;components&lt;/code&gt; allow developers to build complex UI by combining &lt;code&gt;simple&lt;/code&gt;, &lt;code&gt;independent&lt;/code&gt;, and &lt;code&gt;reusable&lt;/code&gt; pieces.&lt;br&gt;
In general, components are essentially &lt;code&gt;self-contained&lt;/code&gt;, &lt;code&gt;reusable pieces&lt;/code&gt; of UI that can be thought of as functions that render some part of the user interface.&lt;br&gt;
I know there might be some conflict about the words &lt;code&gt;self-contained&lt;/code&gt;, &lt;code&gt;reusable pieces&lt;/code&gt;, and &lt;code&gt;renders&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  There are 3 major concerns behind why use Components.
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Reusability&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Separation of concern&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Modularity&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Reusability&lt;/code&gt; ensures create a component once and reuse it multiple times across your application.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Separation of concern&lt;/code&gt; ensures each component focuses on a specific part of the UI, making it easier to &lt;code&gt;manage&lt;/code&gt;and &lt;code&gt;maintain&lt;/code&gt;the code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Modularity&lt;/code&gt; ensures breaking your UI into small, manageable components which makes it easier to &lt;code&gt;test&lt;/code&gt;, &lt;code&gt;debug&lt;/code&gt;,and &lt;code&gt;develop&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's prove one by one by defining components. Opps!😊 I forgot you don't know how to define a simple component. Don't worry I am here🦾 to break down all the things. so let's dive-👋&lt;br&gt;
Suppose you are developing an e-commerce application. In the application in &lt;code&gt;multiple places&lt;/code&gt; or &lt;code&gt;multiple pages&lt;/code&gt; you need to &lt;code&gt;render&lt;/code&gt;a same &lt;code&gt;product card&lt;/code&gt;. &lt;code&gt;Rendering means responding to the UI&lt;/code&gt;. &lt;br&gt;
Now is your time👍 to think🙄 how you can display the &lt;code&gt;product card&lt;/code&gt; in your application in multiple pages.&lt;br&gt;
Traditionally when creating web pages developers marked up their content and added interaction by &lt;code&gt;sprinkling&lt;/code&gt; on some javascript. This worked great when the multiple pages the same marked up didn't replace into multiple pages not too much which makes you sometimes bored and at the same time tiresome as well as hard to &lt;code&gt;debug&lt;/code&gt; and &lt;code&gt;manage&lt;/code&gt;.So, there is a chance to break your code and it's might cumbersome to manage.&lt;br&gt;
Here you will discover the beauty of React component architecture. React let you create component and use as many places as or much as you want.&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;// === Reminder ===&lt;/span&gt;
&lt;span class="nx"&gt;Components&lt;/span&gt; &lt;span class="nx"&gt;are&lt;/span&gt; &lt;span class="nx"&gt;regular&lt;/span&gt; &lt;span class="nx"&gt;javascript&lt;/span&gt; &lt;span class="nx"&gt;functions&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt; &lt;span class="nx"&gt;can&lt;/span&gt; &lt;span class="nx"&gt;sprinkled&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;javascript&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductCard&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;title&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="c1"&gt;// rest of info&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's break it-&lt;br&gt;
&lt;code&gt;step-1:&lt;/code&gt; Declare a simple javascript function as ProductCard name&lt;br&gt;
&lt;code&gt;step-2:&lt;/code&gt; The component/function returns an &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; tag consisting some other tag.&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;// === Reminder ===&lt;/span&gt;
&lt;span class="nx"&gt;All&lt;/span&gt; &lt;span class="nx"&gt;tags&lt;/span&gt; &lt;span class="nx"&gt;must&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;under&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;using&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="nf"&gt;fragment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&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;// === Reminder ===&lt;/span&gt;
&lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="nx"&gt;must&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;capital&lt;/span&gt; &lt;span class="nx"&gt;letter&lt;/span&gt; &lt;span class="nx"&gt;otherwise&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="nx"&gt;won&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;t treated as a component
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ProductCard&lt;/code&gt; component returns a div tag with h1,h2,p, or some other necessary tags written like HTML, but it is &lt;code&gt;Javascript&lt;/code&gt;under the hood. The syntax is called &lt;code&gt;JSX&lt;/code&gt;(javascript XML). No worries I will explain it later.&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;// === Reminder ===&lt;/span&gt;
&lt;span class="nx"&gt;JSX&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;syntax&lt;/span&gt; &lt;span class="nx"&gt;extension&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;javascript&lt;/span&gt; &lt;span class="nx"&gt;that&lt;/span&gt; &lt;span class="nx"&gt;lets&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;write&lt;/span&gt; &lt;span class="nx"&gt;HTML&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;markup&lt;/span&gt; &lt;span class="nx"&gt;inside&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;javascript&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="nx"&gt;which&lt;/span&gt; &lt;span class="nx"&gt;handles&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;logic&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;rendering&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;markup&lt;/span&gt; &lt;span class="nx"&gt;live&lt;/span&gt; &lt;span class="nx"&gt;together&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;same&lt;/span&gt; &lt;span class="nx"&gt;place&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your component is ready to use. For example, you have a Product page and have to render some products.&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;//Products.js or jsx file&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductCard&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;title&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="c1"&gt;// rest of info&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductPage&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;product&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ProductCard&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ProductCard&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ProductCard&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your browser will render 3 times the ProductCard means that 3 product cards have been visible to the browser.&lt;/p&gt;

&lt;h5&gt;
  
  
  The point is How the browser will react after getting this ProductPage code
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;section&amp;gt;&lt;/code&gt; is lowercase, so React knows it will refer to as an HTML tag&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;ProfileCard/&amp;gt;&lt;/code&gt;  starts with a capital &lt;code&gt;P&lt;/code&gt;, so React knows that it will be treated as a Component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;so far so good👏. successfully we have rendered the products card to the product page.&lt;br&gt;
Now times to organize the code:&lt;br&gt;
You may have one or more components like &lt;code&gt;ProductCard&lt;/code&gt;, &lt;code&gt;ProductReviewCard&lt;/code&gt;, &lt;code&gt;SimilarProductCard&lt;/code&gt;, etc. so, it might be cumbersome to declare and manage all the components in one file. so, let's make it more organized to manage using just file structure.&lt;br&gt;
let's create a &lt;code&gt;reusable&lt;/code&gt; folder since the ProductCard may have been used from multiple pages. Inside the &lt;code&gt;reusable&lt;/code&gt; folder create &lt;code&gt;ProductCard.js/jsx&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//reusable/ProductCard.js or jsx file&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductCard&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;title&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="c1"&gt;// rest of info&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now there is a point if you separate the &lt;code&gt;ProductCard&lt;/code&gt;component how will use it from the &lt;code&gt;ProductPage&lt;/code&gt; file. I know you may have already understood yes, we can export the &lt;code&gt;ProductCard&lt;/code&gt;function from the &lt;code&gt;ProductCard&lt;/code&gt;file as named or default which you prefer actually.&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;//reusable/ProductCard.js or jsx file&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ProductCard&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;title&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="c1"&gt;// rest of info&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now it is being usable from the &lt;code&gt;ProductPage&lt;/code&gt; file. just import the &lt;code&gt;ProductCard&lt;/code&gt;from the &lt;code&gt;ProductPage&lt;/code&gt;file then use it as much as you want.&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;//ProductPage.js or jsx file&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ProducrCard&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;./reusable/ProductCard.js&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;ProductPage&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;product&lt;/span&gt; &lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ProductCard&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ProductCard&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ProductCard&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I mentioned earlier why component should use-for &lt;code&gt;reusability&lt;/code&gt;, &lt;code&gt;separation of concern&lt;/code&gt;, and &lt;code&gt;modularity&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our &lt;code&gt;ProductCard&lt;/code&gt; component is fully reusable now. we can use it from anywhere.&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;ProductPage&lt;/code&gt; there may have multiple sections like &lt;code&gt;ProductCard&lt;/code&gt;,&lt;code&gt;ProductReviewCard&lt;/code&gt;,&lt;code&gt;SimilarProductCard&lt;/code&gt;,&lt;code&gt;RecommendedProductCard&lt;/code&gt; etc. Making it manage and maintain the code focusing into a separate section as component by component and section by section.&lt;/li&gt;
&lt;li&gt;After breaking the UI as small chunk of sections makes it easier to &lt;code&gt;test&lt;/code&gt;, &lt;code&gt;debug&lt;/code&gt;, and &lt;code&gt;develop&lt;/code&gt;. we can find &lt;code&gt;bugs/issues&lt;/code&gt; easily from the particular component if bugs arise.
I hope it is much clearer now👍&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After all of this, you have understood that you can be nesting your component also.&lt;/p&gt;

&lt;p&gt;ok let's take a look again-👀&lt;br&gt;
create a &lt;code&gt;Layout&lt;/code&gt; folder&lt;br&gt;
create &lt;code&gt;Header&lt;/code&gt; folder under the &lt;code&gt;Layout&lt;/code&gt; folder then create &lt;code&gt;index.js&lt;/code&gt; file inside &lt;code&gt;Header&lt;/code&gt; folder&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;Header&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="s2"&gt;`Footer`&lt;/span&gt; &lt;span class="nx"&gt;folder&lt;/span&gt; &lt;span class="nx"&gt;under&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="s2"&gt;`Layout`&lt;/span&gt; &lt;span class="nx"&gt;folder&lt;/span&gt; &lt;span class="nx"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;create&lt;/span&gt; &lt;span class="s2"&gt;`index.js`&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="nx"&gt;inside&lt;/span&gt; &lt;span class="s2"&gt;`Footer`&lt;/span&gt; &lt;span class="nx"&gt;folder&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Footer&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;Footer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now create &lt;code&gt;index.js&lt;/code&gt; file under &lt;code&gt;Layout&lt;/code&gt; folder&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;import&lt;/span&gt; &lt;span class="nx"&gt;Header&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;./Header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Footer&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;./Footer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Layout&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Header&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&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;page&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Footer&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you already have discovered the beauty of react component architecture. This is just starting and tried to grow interest inside of your back of mind.&lt;br&gt;
Happy coding🙂🙂&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
  </channel>
</rss>
