🧠 Day 12 — Prototypal Inheritance in JavaScript (Simplified) JavaScript doesn’t use classical inheritance like other languages — it uses prototypes. --- 🔍 What is Prototypal Inheritance? 👉 Objects in JavaScript can inherit properties and methods from other objects --- 📌 Example: const animal = { eats: true }; const dog = Object.create(animal); dog.barks = true; console.log(dog.eats); // true console.log(dog.barks); // true --- 🧠 What’s happening? dog doesn’t have eats JavaScript looks up the prototype chain Finds eats inside animal 👉 This is called prototype chaining --- 🔗 Prototype Chain 👉 If a property is not found: JS looks in the object Then its prototype Then prototype’s prototype Until null --- 🚀 Why it matters ✔ Memory efficient (shared methods) ✔ Core concept behind JS objects ✔ Used in classes internally --- 💡 One-line takeaway: 👉 “Objects can inherit from other objects using prototypes.” --- Understanding this makes concepts like classes and this much easier. #JavaScript #Prototypes #WebDevelopment #Frontend #100DaysOfCode
Prototypal Inheritance in JavaScript Explained
More Relevant Posts
-
Sharing properties across objects in JavaScript doesn’t have to be complicated 👀 Imagine you’re working on a large codebase. Multiple modules… similar functions… and suddenly you realize you’re repeating the same logic everywhere. Yeah, goodbye DRY principle 😅 This is where Object.prototype (and prototype chain) becomes really useful. In simple terms, every object in JavaScript can access properties from its prototype. So instead of redefining the same function on every object, you can define it once — and all instances can use it. Think of it like a shared layer that every object can access. That’s why when you inspect an object in the console and see __proto__/[prototypes], those are the shared properties coming from the prototype. Why this matters: - More memory efficient (no duplicate functions per object) - Keeps behavior consistent across instances - Cleaner when multiple objects need the same capability But… there are tradeoffs. If the prototype chain gets too deep, JavaScript has to walk through each level to find a property and that can impact performance. Also, modifying global prototypes can be risky if not handled carefully. Still, when used properly, this pattern is very powerful. I also tried a simple example — implementing my own map function using prototype (super basic, but fun to explore). Do you still use prototypes directly, or mostly rely on classes these days? 👀 #javascript #fundamental #prototype
To view or add a comment, sign in
-
-
JavaScript Hoisting 🎭 At first, it feels like variables and functions magically “float” to the top of your script. But in reality, nothing is physically moving. I like to explain it using Reserved Seats. Imagine walking into a cinema. Before the first person enters, some seats are already reserved. But the people (the values) aren’t sitting there yet. That’s exactly how the JavaScript engine works during the Creation Phase: • It scans your code • It allocates memory for declarations • It reserves space before executing a single line Code in Action: console.log(movie); var movie = "Inception"; // Output: undefined Why? Because var movie reserved the seat during creation, but the value "Inception" arrived only when execution reached that line. What about let and const? They are hoisted too but they stay inside the Temporal Dead Zone (TDZ) until their declaration line is reached. Think of it like a reserved seat that stays locked until the owner arrives with the key. Try to use it too early, and JavaScript throws an error. Why This Matters in Real Projects • Debugging unexpected undefined values • Writing predictable code • Avoiding scope confusion • Understanding how JavaScript executes behind the scenes Hoisting isn’t magic — it’s JavaScript preparing the room before the party starts. Once you understand the Creation Phase, many “weird” behaviors finally make sense. #JavaScript #ReactJS #NodeJS #FrontendEngineer #OpentoWork #SoftwareDevelopment #FullStack #JavascriptDeveloper #ReactjsDeveloper #FrontEndDeveloper #FullstackDeveloper
To view or add a comment, sign in
-
🚀Day 30 — Scope Chain & Scope Types in JavaScript (Simplified) Understanding scope is one of the most important fundamentals in JavaScript 🚀 --- 🔍 What is Scope? 👉 Scope decides where variables can be accessed in your code In simple words: 👉 “Who can access what?” --- ⚡ Types of Scope 1. Global Scope 👉 Variables declared outside functions or blocks let name = "John"; function greet() { console.log(name); // accessible } --- 2. Function Scope 👉 Variables declared inside a function function test() { let age = 25; console.log(age); } console.log(age); // ❌ Error --- 3. Block Scope 👉 Variables declared with let and const inside {} if (true) { let city = "Delhi"; } console.log(city); // ❌ Error --- 🔗 What is Scope Chain? 👉 If JS can’t find a variable in current scope, it looks in the outer scope, then outer again… until global scope. This is called the Scope Chain --- 🚀 Why it matters ✔ Prevents variable conflicts ✔ Helps understand closures ✔ Improves debugging skills --- 💡 One-line takeaway: 👉 “JavaScript looks upward to find variables — that’s the scope chain.” --- Mastering scope makes closures, hoisting, and debugging much easier. #JavaScript #Scope #ScopeChain #WebDevelopment #Frontend #100DaysOfCode 🚀
To view or add a comment, sign in
-
Ever written JavaScript that *shouldn’t* work… but somehow does? 🤯 I remember staring at my screen thinking, “Did JS just ignore my logic?” Here’s the problem 👇 JavaScript has this hidden behavior called **hoisting**. It moves declarations (not values) to the top of the scope *before execution*. Sounds helpful… until it silently breaks your expectations. Here’s the insight 💡 * `var` gets hoisted and initialized as `undefined` * `let` & `const` are hoisted but stay in a “temporal dead zone” 🚫 * Functions? Fully hoisted — you can call them before defining! Example: ```js console.log(a); // undefined 😵 var a = 5; ``` Lesson learned 📌 JavaScript isn’t “weird” — it just follows rules we often ignore. Understanding hoisting = fewer bugs + cleaner logic. Now I always think: “What does JS see *before* running this?” 👀 If you’re diving deeper into JS concepts, I share more here 👉 https://webdevlab.org 🚀 Curious… have you ever been surprised by hoisting in your code? 😄 #JavaScript #WebDevelopment #Frontend #CodingTips #DevLife
To view or add a comment, sign in
-
-
🚀 JavaScript Output Prediction Challenge What will be the output? 🤔 💡 Important Behaviour: 👉 JavaScript executes code line by line 👉 The very first line throws the following: ❌ ReferenceError: a is not defined So in reality, the programme stops immediately and nothing else runs. 📌 Now, let’s understand what would happen inside the function (ignoring the first error): 👉 Before execution, JS does Hoisting (memory creation phase) Inside the function: var a → hoisted and initialized as undefined let b → hoisted but kept in Temporal Dead Zone (TDZ) (not accessible yet) 👉 Execution phase: "1:" → undefined (Because 'a' exists but not assigned yet) "2:" → ❌ ReferenceError (because b is in TDZ) After assignment: a = 10 b = 20 "3:" → 10 "4:" → 20 🔥 Core Concepts: JS has 2 phases → Memory Creation + execution. var → hoisted + usable as undefined let/const → hoisted but blocked by TDZ 💬 If you got this right, your JS fundamentals are solid 👇 #JavaScript #Hoisting #Frontend #CodingChallenge #WebDevelopment
To view or add a comment, sign in
-
-
🔍 JavaScript Behavior You Might Have Seen (Prototype Chain) You write this: const arr = [1, 2, 3]; console.log(arr.toString()); // ? 👉 Did you define toString on this array? 👉 Did you even define it anywhere? No. Still it works. Now think step by step 👇 When you do: 👉 arr.toString JavaScript checks: Inside arr → ❌ not found Inside Array.prototype → ❌ not found Inside Object.prototype → ✔ found This step-by-step lookup is called the Prototype Chain 📌 What is Prototype Chain? 👉 It’s the process JavaScript uses to find a property by searching up through prototypes. 📌 How it works? Every object is linked to another object (its prototype) So lookup happens like this: 👉 arr → Array.prototype → Object.prototype → null Same with strings 👇 const str = "hello"; console.log(str.hasOwnProperty("length")); // ? 👉 hasOwnProperty is not in string JS finds it here: 👉 str → String.prototype → Object.prototype 📌 Important point: 👉 JavaScript keeps searching until it finds the property or reaches null 💡 Takeaway: ✔ Prototype Chain = lookup mechanism ✔ JS searches step by step ✔ That’s how all built-in methods work 👉 If you understand this, you’ll understand how JavaScript really works 💬 Comment “chain” if this clicked 🔁 Save this for later ❤️ Like for more JavaScript deep dives #javascript #frontend #codingtips #webdevelopment #js #developer
To view or add a comment, sign in
-
⚡ Day 7 — JavaScript Event Loop (Explained Simply) Ever wondered how JavaScript handles async tasks while being single-threaded? 🤔 That’s where the Event Loop comes in. --- 🧠 What is the Event Loop? 👉 The Event Loop manages execution of code, async tasks, and callbacks. --- 🔄 How it works: 1. Call Stack → Executes synchronous code 2. Web APIs → Handle async tasks (setTimeout, fetch, etc.) 3. Callback Queue / Microtask Queue → Stores callbacks 4. Event Loop → Moves tasks to the stack when it’s empty --- 🔍 Example: console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); Promise.resolve().then(() => { console.log("Promise"); }); console.log("End"); --- 📌 Output: Start End Promise Timeout --- 🧠 Why? 👉 Microtasks (Promises) run before macrotasks (setTimeout) --- 🔥 One-line takeaway: 👉 “Event Loop decides what runs next in async JavaScript.” --- If you're learning async JS, understanding this will change how you debug forever. #JavaScript #EventLoop #WebDevelopment #Frontend #100DaysOfCode 🚀
To view or add a comment, sign in
-
What will happen if you call a variable before initialization? 🤔 That is called Hoisting 👉 "JavaScript moves declarations to the top of their scope before execution" Sounds confusing? Let’s break it down 👇 When you create variables or functions, JavaScript runs your code in 2 phases: 1️⃣ Memory Creation Phase Before execution, JavaScript scans your code and allocates memory Example (mentally): var a → undefined let b → uninitialized (Temporal Dead Zone) 2️⃣ Execution Phase Now JavaScript runs your code line by line 👉 If you access variables before initialization: var → returns undefined let / const → ReferenceError Why does this happen? Because: var is initialized with undefined in memory let and const are hoisted but stay in the Temporal Dead Zone (TDZ) until the line where they are declared Simple way to remember: var => “exist, but don’t have a value yet” let / const => “Don’t touch before declaration” ⚡ Bonus: Function declarations are fully hoisted, so you can call them before defining them Curious how functions behave in hoisting? 🤔 Go Google function vs function expression in JavaScript — it’ll surprise you 👀 That’s hoisting in JavaScript 🚀 #javascript #webdevelopment #coding #frontend #learninpublic #hoisting
To view or add a comment, sign in
-
-
🚀 Understanding the JavaScript Event Loop (Simple Explanation) Ever wondered how JavaScript handles multiple tasks even though it’s single-threaded? 🤔 That’s where the Event Loop comes in! 👉 In simple terms: The Event Loop manages execution of code, handles async operations, and keeps your app running smoothly. 🔹 Key Components: Call Stack → Executes functions (one at a time) Web APIs → Handles async tasks (setTimeout, fetch, etc.) Callback Queue → Stores callbacks from async tasks Microtask Queue → Stores Promises (higher priority) Event Loop → Moves tasks to the Call Stack when it's free 🔹 Example: console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); Promise.resolve().then(() => { console.log("Promise"); }); console.log("End"); 👉 Output: Start End Promise Timeout 🔹 Why this output? "Start" → runs first (Call Stack) "End" → runs next Promise → goes to Microtask Queue (runs before callbacks) setTimeout → goes to Callback Queue (runs last) 💡 Key Insight: 👉 Microtasks (Promises) always execute before Macrotasks (setTimeout) 🔥 Mastering the Event Loop helps you write better async code and avoid unexpected bugs! #JavaScript #Frontend #WebDevelopment #Coding #InterviewPrep
To view or add a comment, sign in
-
Hoisting in JavaScript (and TypeScript) - Explained So You’ll Never Forget Ever saw this weird behavior? console.log(a); // undefined var a = 10; OR even worse: sayHello(); // Works! function sayHello() { console.log("Hello!"); } How is JavaScript using things before they are declared? 👉 That’s called Hoisting 🏠 Real-Life Example: Hotel Reception Imagine you walk into a hotel… "var" — Pre-registered Guest 📝 Your name is already in the system, but your room is not ready yet. 👉 You exist… but not fully ready 👉 So you get "undefined" "let" / "const" — Walk-in Guest 🚶♂️ You are NOT in the system yet. 👉 Try to access before check-in? ❌ “Sir, you are not registered!” (This is called Temporal Dead Zone) "function" — VIP Guest ⭐ Your room is already booked and ready! 👉 You can directly go in 👉 That’s why functions work before declaration Behind the scenes (Simple terms): JavaScript does 2 steps: 1️⃣ Memory Allocation Phase → Variables & functions are stored 2️⃣ Execution Phase → Code runs line by line Key Takeaways: ✔ "var" is hoisted (initialized as "undefined") ✔ "let" & "const" are hoisted but NOT initialized ✔ Functions are fully hoisted Pro Tip: Avoid confusion → Always declare variables at the top (or just use "let" / "const" properly) 💬 Have you ever faced a bug because of hoisting? #JavaScript #TypeScript #WebDevelopment #Frontend #Coding #LearnToCode
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development