JavaScript objects have a powerful feature called Prototype that enables inheritance. In JavaScript, if an object doesn’t have a specific property or method, the engine automatically looks for it in its prototype. This process continues up the prototype chain until the property is found or the chain ends. Example: function Person(name) { this.name = name; } Person.prototype.greet = function () { console.log("Hello, my name is " + this.name); }; const john = new Person("John"); john.greet(); Even though john doesn’t directly have the greet() method, it can still access it through Person.prototype. This mechanism allows JavaScript objects to share methods and inherit behavior efficiently. Understanding prototypes helps you better understand how inheritance and object behavior work behind the scenes in JavaScript. Follow for more JavaScript concepts explained visually. #javascript #webdevelopment #frontenddeveloper #coding #learninginpublic #100DaysOfCode
JavaScript Prototype Inheritance Explained
More Relevant Posts
-
🚨 JavaScript Hoisting – Something Most Developers Still Misunderstand Most developers say: 👉 “JavaScript moves variables to the top of the scope.” But that’s not actually what happens. Let’s test this 👇 console.log(a); var a = 10; Output: undefined Now try this: console.log(b); let b = 20; Output: ReferenceError: Cannot access 'b' before initialization 💡 Why the difference? Both var and let are hoisted. But the real difference is initialization timing. ✔ var is hoisted and initialized with undefined during the creation phase. ✔ let and const are hoisted but stay inside the Temporal Dead Zone (TDZ) until the line where they are declared. That’s why accessing them before declaration throws an error. 👉 So technically: JavaScript doesn’t “move variables to the top”. Instead, the JavaScript engine allocates memory for declarations during the creation phase of the execution context. Small detail. But it explains a lot of confusing bugs. 🔥 Understanding this deeply helps when debugging closures, scope issues, and async code. #javascript #frontend #webdevelopment #reactjs #coding #softwareengineering
To view or add a comment, sign in
-
Day 1 of 30 days of javascript challenge. problem-2667 Problem - Write a function createHelloWorld that returns another function, that returns "Hello World" As this is my first code, I revised my notes on javascript scope. ☑️ Function scope - Any variables declared inside a function body cannot be accessed outside the function body, but global variables can be used inside function body ☑️ Block scope - Any variable declared inside { } cannot be used outside the { } block, although it supports only let and const keyword, var can be used ☑️ Lexical scope - A variable declared outside a function can be accessed inside another function defined after the variable declaration. (The opposite is not true ) This problem uses the concept of closures and higher order functions. Please feel free to discuss where can I improve the code or if you have a different perspective, comment below your views. #javascript #coding #development #motivation #goals #leetcode #webdevelopment
To view or add a comment, sign in
-
-
One of my favorite JavaScript one-liners: filter(Boolean). Filtering out falsy values from an array meant chaining conditions and hoping I hadn't missed an edge case. When you pass Boolean as a callback to .filter(), JavaScript calls Boolean(item) on every element. Anything falsy - null, undefined, 0, ", false, NaN - gets removed. What you're left with is a clean array of only meaningful values. It's not just about writing less code. It's about communicating intent clearly. This pattern shines especially in real-world scenarios: cleaning up APl responses, processing user input, or combining .map) and.filter) to transform and sanitize data in a single chain. #JavaScript #WebDevelopment #SoftwareEngineering #CleanCode #Frontend #Programming #JS #CodeQuality #TechTips #Developer
To view or add a comment, sign in
-
-
JavaScript can be surprisingly logical… and surprisingly weird at the same time. 😄 Take a look at these three lines: console.log(true + true); console.log(null + 1); console.log(undefined + 1); Before running them, try guessing the outputs. Here’s what JavaScript actually returns: true + true → 2 null + 1 → 1 undefined + 1 → NaN At first, this felt a little strange to me. But it starts to make sense once you remember how type coercion works in JavaScript. true is treated as 1, so 1 + 1 = 2 null becomes 0, so 0 + 1 = 1 undefined turns into NaN, which leads to NaN Small examples like this are a good reminder that JavaScript quietly converts values behind the scenes. And if you’re not aware of it, the results can feel pretty surprising. The deeper I go into JavaScript, the more I realize that understanding these tiny behaviors makes a huge difference in writing reliable code. Which one caught you off guard the most? #javascript #webdevelopment #frontend #coding #learninginpublic
To view or add a comment, sign in
-
🚀 Understanding Hoisting in JavaScript Many developers hear that JavaScript moves variables and functions to the top, but what actually happens behind the scenes? In JavaScript, hoisting occurs during the compilation phase, before the code executes. The JavaScript engine first scans the entire code and allocates memory for variables and functions. This means: • var variables are hoisted and initialized with undefined • let and const are also hoisted but remain in the Temporal Dead Zone (TDZ) until their declaration line is reached • Function declarations are fully hoisted, allowing them to be called before they appear in the code Example: console.log(a); var a = 10; Output: undefined Internally JavaScript treats it like this: var a; console.log(a); a = 10; ⚠️ Important: JavaScript does not physically move code to the top. During compilation the engine simply registers declarations in memory before execution begins. Understanding hoisting helps developers better grasp execution context, scope, and the JavaScript engine's behavior. #JavaScript #WebDevelopment #Frontend #Programming #Coding
To view or add a comment, sign in
-
🔍 A small JavaScript detail that can cause unexpected bugs: Object key ordering Many developers assume object keys are always returned in insertion order, but JavaScript actually follows a specific ordering rule when you iterate over object properties (Object.keys, Object.entries, for...in). The order is: • Integer index keys → sorted in ascending order • String keys → insertion order • Symbol keys → insertion order (not included in Object.keys) This is one of the reasons why using Object as a map can sometimes lead to unexpected iteration behavior when numeric keys are involved. If key order matters, Map is usually the more predictable choice since it preserves insertion order for all key types. Small language details like this are easy to overlook, but they often explain those subtle bugs you run into during debugging. #JavaScript #SoftwareEngineering #Frontend
To view or add a comment, sign in
-
-
💡 JavaScript Surprise: await works on NON-promises?! const obj = { then: () => console.log("I'm evil 😈") }; await obj; // I'm evil 😈 At first glance, this looks strange… Why is await calling then() on a plain object? 👉 The answer lies in a powerful (and sometimes dangerous) concept: Thenables 🚀 What’s happening here? In JavaScript, await doesn’t just wait for Promises. It follows the "thenable protocol": If an object has a .then() method… JavaScript treats it like a Promise! So internally, this: await obj; Becomes something like: Promise.resolve(obj).then(...) And since your object has a then() method, it gets executed automatically. ⚠️ Why this matters This behavior can: Lead to unexpected side effects Introduce hard-to-debug issues Even be abused in malicious patterns 😈 🧠 Key takeaway “In JavaScript, anything with a .then() can act like a Promise.” So always be careful when: Working with external data Designing APIs Overloading object behavior 🔥 Pro tip Avoid adding a .then() method to objects unless you intentionally want them to behave like Promises. JavaScript is flexible… sometimes too flexible 😅 #JavaScript #AsyncAwait #WebDevelopment #Programming #100DaysOfCode #JSDeepDive #reactjs #nodejs
To view or add a comment, sign in
-
-
One of my favorite JavaScript one-liners: .filter(Boolean) Filtering out falsy values from an array meant chaining conditions and hoping I hadn't missed an edge case. When you pass Boolean as a callback to .filter(), JavaScript calls Boolean(item) on every element. Anything falsy - null, undefined, 0, "", false, NaN - gets removed. What you're left with is a clean array of only meaningful values. It's not just about writing less code. It's about communicating intent clearly. This pattern shines especially in real-world scenarios: cleaning up API responses, processing user input, or combining .map() and .filter() to transform and sanitize data in a single chain. #JavaScript #WebDevelopment #SoftwareEngineering #CleanCode #Frontend #Programming #JS #CodeQuality #TechTips #Developer --- I post about web engineering, front-end and soft skills in development. Follow me here: Irene Tomaini
To view or add a comment, sign in
-
-
🚀 Just solved the "Best Time to Buy and Sell Stock" problem in JavaScript! Today I worked on improving my problem-solving skills by implementing a solution to calculate the maximum profit from stock prices. 🔍 Problem: Given an array of prices, determine the maximum profit you can achieve by buying and selling once. 💡 Approach: I started with a brute-force solution using nested loops to compare every possible buy/sell pair. While not the most optimal (O(n²)), it helped me deeply understand the problem before optimizing. 📌 Example: Input: [10, 1, 5, 6, 7, 1] Output: 6 🧠 Key takeaway: Sometimes starting simple is the best way to build strong intuition before moving to more efficient solutions. 👉 Check out my code and more JavaScript patterns here: https://lnkd.in/ej4fNeZs #JavaScript #Coding #ProblemSolving #100DaysOfCode #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 86 of My #100DaysOfCode Challenge Today I discovered a lesser-known feature in JavaScript — Symbols. Most developers work with object keys using strings, but JavaScript also provides another unique type called Symbol. A Symbol creates a unique and hidden property key that cannot accidentally conflict with other keys. Example const id = Symbol("id"); const user = { name: "Tejal", [id]: 12345 }; console.log(user.name); // Tejal console.log(user[id]); // 12345 Why Symbols are interesting • Every Symbol is unique • Helps create hidden object properties • Prevents accidental property overwriting • Often used internally in libraries and frameworks Even if two symbols have the same description, they are still different. const a = Symbol("key"); const b = Symbol("key"); console.log(a === b); // false Learning about features like Symbols helps me understand how JavaScript works behind the scenes and how large applications manage object data safely. Exploring deeper concepts every day. 💻✨ #Day86 #100DaysOfCode #JavaScript #WebDevelopment #LearningInPublic #CodingJourney
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