Most developers get confused by the JavaScript this keyword. Sometimes it refers to the object. Sometimes it becomes undefined. And with arrow functions, it behaves completely differently. In this short video, you'll understand: • this inside object methods • this in standalone functions • this in arrow functions • Why this becomes undefined in strict mode If JavaScript’s this keyword has ever confused you, this quick explanation will make it clear. Perfect for developers learning JavaScript fundamentals or preparing for frontend interviews. JavaScript this keyword this keyword in javascript javascript this explained javascript this undefined method vs arrow function javascript javascript interview question javascript fundamentals js this keyword example #JavaScript #WebDevelopment #FrontendDev #thiskeyword #JSTips #ProgrammingBugs #softwareengineering
More Relevant Posts
-
One of the most common JavaScript interview questions: "Why does setTimeout with 0ms delay not run immediately?" Most developers cannot answer this correctly. Here is the full explanation: JavaScript is single-threaded. It can only do one thing at a time. The Event Loop is how it manages everything else. The execution order is always the same: 1 — Synchronous code runs first All regular code on the Call Stack executes immediately. 2 — Microtasks run second Promises, async/await — these run before anything else once the Call Stack is empty. 3 — Macrotasks run last setTimeout, setInterval, DOM events — these wait until ALL microtasks are done. This is why setTimeout with 0ms still runs after a Promise. The Promise is a microtask. setTimeout is a macrotask. Microtasks always win. Understanding this prevents real bugs in production — async state updates, race conditions, unexpected render order. Save this post for your next async debugging session. Have you ever been confused by JavaScript async order? Drop a comment below. #JavaScript #WebDevelopment #Frontend #SoftwareEngineering #AsyncJavaScript
To view or add a comment, sign in
-
-
Recently in an interview, I was asked about the JavaScript Event Loop… I thought I knew it — but explaining it clearly was harder than expected. So I created this diagram to understand it properly 👇 • Understanding JavaScript Event Loop (Simple Explanation) ° JavaScript Engine JavaScript runs on a single thread. It uses: • Memory Heap → to store data • Call Stack → to execute code 📞 Call Stack All synchronous code runs here. Functions are pushed and popped one by one. 🌐 Web APIs (Browser / Node.js) Async operations are handled outside the JS engine: • setTimeout / setInterval • fetch API • DOM events These APIs run in the background. 📥 Queues Once async work is done, callbacks go into queues: 👉 Microtask Queue (High Priority) • Promise.then() • async/await 👉 Task Queue (Low Priority) • setTimeout • setInterval • DOM events 🔁 Event Loop (Most Important Part) The event loop keeps checking: Is Call Stack empty? Execute ALL Microtasks Then execute ONE Task from Task Queue That’s why Promises run before setTimeout! One Line Summary: JavaScript uses Call Stack + Web APIs + Queues + Event Loop to handle async code without blocking execution. This is one of the most common interview questions — but also one of the most misunderstood. If you can explain this clearly, you’re already ahead of many developers. #JavaScript #EventLoop #AsyncJavaScript #WebDevelopment #NodeJS #Frontend #Programming #Interview
To view or add a comment, sign in
-
-
𝗤𝘂𝗶𝗰𝗸 𝗙𝗶𝗿𝗲 𝗝𝗦 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗦𝗲𝗿𝗶𝗲𝘀 (𝟯/𝟴) Now we get into one of the most misunderstood parts of JavaScript. 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟯 What does this mean in JavaScript? This is where candidates often memorize rules… but fail to reason about execution. A strong answer should cover: • What this actually represents (execution context) • How this is determined at call-time, not definition-time • The difference between regular functions and arrow functions • How call-site affects this binding 𝗔𝗻𝘀𝘄𝗲𝗿 • this refers to the object that is executing the current function • Its value is determined by how the function is called, not where it’s defined • In regular functions, this depends on the call-site (e.g. obj.method() → this === obj) • In arrow functions, this is lexically inherited from the surrounding scope • Methods like call, apply, and bind can explicitly control this 𝗘𝘅𝘁𝗿𝗮 — 𝗢𝗻𝗲 𝗹𝗶𝗻𝗲𝗿 this is not about where the function is written — it’s about how it’s called. If you don’t think in terms of call-site, this will always feel unpredictable. And interviews will catch that instantly.
To view or add a comment, sign in
-
-
JavaScript Interview Question: How do you cancel API requests in JavaScript? Answer: Using AbortController. Example: 𝘤𝘰𝘯𝘴𝘵 𝘤𝘰𝘯𝘵𝘳𝘰𝘭𝘭𝘦𝘳 = 𝘯𝘦𝘸 𝘈𝘣𝘰𝘳𝘵𝘊𝘰𝘯𝘵𝘳𝘰𝘭𝘭𝘦𝘳() 𝘧𝘦𝘵𝘤𝘩("/𝘢𝘱𝘪/𝘥𝘢𝘵𝘢", { 𝘴𝘪𝘨𝘯𝘢𝘭: 𝘤𝘰𝘯𝘵𝘳𝘰𝘭𝘭𝘦𝘳.𝘴𝘪𝘨𝘯𝘢𝘭 }) // 𝘤𝘢𝘯𝘤𝘦𝘭 𝘳𝘦𝘲𝘶𝘦𝘴𝘵 𝘤𝘰𝘯𝘵𝘳𝘰𝘭𝘭𝘦𝘳.𝘢𝘣𝘰𝘳𝘵() Explanation: AbortController allows you to cancel ongoing requests. Useful for: 1. search inputs (cancel previous queries) 2. component unmount 3. preventing race conditions Without it, outdated responses may overwrite newer data. Follow-up: Have you ever faced race condition bugs in API calls? #javascript #WebDevelopment #AsyncProgramming #FrontendDevelopment
To view or add a comment, sign in
-
🚀 JavaScript Interview Questions Every Developer Should Know Here are some useful JS questions with simple answers 👇 🔹 1. What is the output? console.log(typeof null); 👉 Answer: "object" 💡 This is a well-known JavaScript bug. 🔹 2. What is closure? 👉 A closure is a function that remembers variables from its outer scope even after the outer function has finished execution. function outer() { let count = 0; return function inner() { count++; return count; }; } 🔹 3. Difference between == and ===? 👉 == → compares value (loose equality) 👉 === → compares value + type (strict equality) 🔹 4. What is hoisting? 👉 JavaScript moves variable and function declarations to the top of their scope before execution. 🔹 5. What will be the output? let a = 10; (function() { console.log(a); let a = 20; })(); 👉 Answer: ❌ ReferenceError 💡 Due to Temporal Dead Zone (TDZ) 🔹 6. What is event loop? 👉 It handles async operations by managing the call stack and callback queue. 🔹 7. What is this keyword? 👉 Refers to the object that is calling the function (depends on context). 🔹 8. What is a promise? 👉 A promise represents a value that may be available now, later, or never. 🔹 9. What is async/await? 👉 Syntactic sugar over promises to write async code like synchronous code. 🔹 10. What is debounce? 👉 Limits how often a function runs. Useful for search inputs. 🔥 Save this for your next interview 💬 Comment your favorite question 🔁 Share with your developer friends #JavaScript #WebDevelopment #Frontend #InterviewPrep #Coding
To view or add a comment, sign in
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗘𝘃𝗲𝗻𝘁𝗟𝗼𝗼𝗽 𝗘𝘅𝗽𝗹𝗮𝗶𝗻𝗲𝗱 (𝗔 𝗠𝘂𝘀𝘁‐𝗞𝗻𝗼𝘄 𝗖𝗼𝗻𝗰𝗲𝗽𝘁) Understanding the JavaScript Event Loop is a game changer for writing efficient and predictable asynchronous code. Many developers use setTimeout and Promises every day — but far fewer truly understand how JavaScript executes async tasks behind the scenes. Let’s break it down 👇 𝗛𝗼𝘄 𝘁𝗵𝗲 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 𝗪𝗼𝗿𝗸𝘀 • JavaScript runs on a single thread • Synchronous code executes first via the Call Stack • Then Microtasks run (like Promises) • Next, one Macrotask executes (timers, events) • This cycle continues repeatedly 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗣𝗿𝗶𝗼𝗿𝗶𝘁𝘆 ➡️ Synchronous ➡️ Microtasks ➡️ Macrotasks 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗰𝗼𝗻𝗰𝗲𝗽𝘁 𝗺𝗮𝘁𝘁𝗲𝗿𝘀 ✅ Debug async issues with confidence ✅ Avoid unexpected execution order ✅ Build more predictable React applications ✅ Frequently tested in frontend interviews Credit: owner Follow Rensith Udara Gonalagoda for more related content! 🤔 Having Doubts in technical journey? 🚀 Book 1:1 session with me : https://lnkd.in/gQfXYuQm 🚀 Subscribe and stay up to date: https://lnkd.in/dGE5gxTy 🚀 Get Complete React JS Interview Q&A Here: https://lnkd.in/d5Y2ku23 🚀 Get Complete JavaScript Interview Q&A Here: https://lnkd.in/d8umA-53 #JavaScript #EventLoop #FrontendDevelopment #ReactJS #WebDevelopment #InterviewPrep #AsyncJavaScript #SoftwareEngineering
To view or add a comment, sign in
-
-
𝐈𝐟 𝐘𝐨𝐮 𝐊𝐧𝐨𝐰 𝐓𝐡𝐞𝐬𝐞 𝐑𝐮𝐥𝐞𝐬, 𝐘𝐨𝐮’𝐥𝐥 𝐍𝐞𝐯𝐞𝐫 𝐁𝐫𝐞𝐚𝐤 𝐂𝐨𝐝𝐞 𝐰𝐢𝐭𝐡 “𝐭𝐡𝐢𝐬” 𝐢𝐧 𝐣𝐚𝐯𝐚𝐬𝐜𝐫𝐢𝐩𝐭! 1. Global Context Outside any function, this refers to the global object window in browsers, global in js. 2. Regular Function (non-strict mode) this refers to the global object (window), even though you're inside a function. 3. Regular Function (strict mode) this is undefined. JavaScript stops the accidental global binding. 4. Object Method When a function is called as a property of an object, this refers to that object. 5. Arrow Function Doesn't have its own this. It inherits this from the surrounding lexical scope wherever the arrow function was written, not called. 6. Constructor Function / new keyword this refers to the newly created object being built. 7. Class Method Same as object method this refers to the instance of the class. 8. call(), apply(), bind() You manually set what this should be. These are the escape hatches. 9. Event Listeners (DOM) this refers to the HTML element that triggered the event but only with regular functions, not arrow functions. 10. Callback Functions this is often lost here. Passing a method as a callback detaches it from its original object. For more insightful content checkout below: 🟦 𝑳𝒊𝒏𝒌𝒆𝒅𝑰𝒏 - https://lnkd.in/dwi3tV83 ⬛ 𝑮𝒊𝒕𝑯𝒖𝒃 - https://lnkd.in/dkW958Tj 🟥 𝒀𝒐𝒖𝑻𝒖𝒃𝒆 - https://lnkd.in/dDig2j75 or Priya Frontend Vlogz 🔷 𝐓𝐰𝐢𝐭𝐭𝐞𝐫 - https://lnkd.in/dyfEuJNt #frontend #javascript #react #interview #interviewpreparation #SDE w3schools.com JavaScript Mastery JavaScript Developer freeCodeCamp
To view or add a comment, sign in
-
-
I was recently asked a classic JavaScript "gotcha" in an interview: "How do you return an object in an arrow function, and why are parentheses required?" It sounds simple But the "Why" is where most developers get tripped up. The Problem: In JavaScript, {} is ambiguous. It can mean an Object Literal OR a Function Block. By default, the JS engine sees a brace after an arrow => and assumes it's a function block. The Result: const getUser = () => { name: 'Chandhan' }; The engine thinks name: is a label and returns undefined. The Fix: Wrap it in parentheses! ({ ... }) The () forces the engine to treat the contents as an expression, not a statement. ✅ const getUser = () => ({ name: 'Chandhan' }); Small syntax, big difference in how the engine parses your code. #JavaScript #WebDevelopment #CodingTips #Angular #Frontend #InterviewPrep
To view or add a comment, sign in
-
🔥 Top 10 JavaScript Interview Questions You Must Know 🔥 (These decide your JS fundamentals) 1️⃣ var vs let vs const var → function scoped let / const → block scoped 👉 const is preferred by default. 2️⃣ What is Hoisting? Variables and functions are moved to the top during execution. 👉 let and const are hoisted but not initialized. 3️⃣ What is Closure? A function remembers variables from its outer scope. 👉 Very common and very important. 4️⃣ == vs === == → compares value (type conversion) === → compares value + type 👉 Always prefer ===. 5️⃣ What is the Event Loop? It handles async operations like callbacks and promises. 👉 Explains how JS is non-blocking. 6️⃣ Promise vs Callback Promise → cleaner, chainable, better error handling Callback → can cause callback hell 👉 Promises improved async code. 7️⃣ What is this keyword? this depends on how a function is called. 👉 Context matters, not where it’s written. 8️⃣ What is Debouncing and Throttling? Debouncing → delays execution Throttling → limits execution rate 👉 Used for performance optimization. 9️⃣ What is Spread vs Rest operator? Spread → expands values Rest → collects values 👉 Same syntax, different use. 🔟 What is Prototype in JavaScript? Objects inherit properties via prototype chain. 👉 Core concept behind JS inheritance. 💡 JavaScript interviews test concepts, not syntax. 💪 One goal – SELECTION #javascript #Interview #questions #mostasking #important #save
To view or add a comment, sign in
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗘𝘃𝗲𝗻𝘁𝗟𝗼𝗼𝗽 𝗘𝘅𝗽𝗹𝗮𝗶𝗻𝗲𝗱 (𝗔 𝗠𝘂𝘀𝘁‐𝗞𝗻𝗼𝘄 𝗖𝗼𝗻𝗰𝗲𝗽𝘁) Understanding the JavaScript Event Loop is a game changer for writing efficient and predictable asynchronous code. Many developers use setTimeout and Promises every day — but far fewer truly understand how JavaScript executes async tasks behind the scenes. Let’s break it down 👇 𝗛𝗼𝘄 𝘁𝗵𝗲 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 𝗪𝗼𝗿𝗸𝘀 • JavaScript runs on a single thread • Synchronous code executes first via the Call Stack • Then Microtasks run (like Promises) • Next, one Macrotask executes (timers, events) • This cycle continues repeatedly 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗣𝗿𝗶𝗼𝗿𝗶𝘁𝘆 ➡️ Synchronous ➡️ Microtasks ➡️ Macrotasks 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗰𝗼𝗻𝗰𝗲𝗽𝘁 𝗺𝗮𝘁𝘁𝗲𝗿𝘀 ✅ Debug async issues with confidence ✅ Avoid unexpected execution order ✅ Build more predictable React applications ✅ Frequently tested in frontend interviews Credit: owner Follow Alpna P. for more related content! 🤔 Having Doubts in technical journey? 🚀 Book 1:1 session with me : https://lnkd.in/gQfXYuQm 🚀 Subscribe and stay up to date: https://lnkd.in/dGE5gxTy 🚀 Get Complete React JS Interview Q&A Here: https://lnkd.in/d5Y2ku23 🚀 Get Complete JavaScript Interview Q&A Here: https://lnkd.in/d8umA-53 #JavaScript #EventLoop #FrontendDevelopment #ReactJS #WebDevelopment #InterviewPrep #AsyncJavaScript #SoftwareEngineering
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