Posts

Showing posts with the label javascript

⚡ What Is Hoisting in JavaScript?

⚡ What Is Hoisting in JavaScript? Hoisting means that JavaScript moves variable and function declarations to the top of their scope (before code execution) — but not the initialization . So JavaScript “knows” about your variables and functions before it runs your code, but values are not yet assigned. ๐Ÿง  Example 1 — Variable Hoisting (with var ) console.log(a); // ๐Ÿ‘‰ undefined var a = 10; ๐Ÿ” Behind the Scenes JavaScript internally does this: var a; // Declaration is hoisted console.log(a); a = 10; // Initialization stays in place ✅ So it doesn’t throw an error — it just logs undefined . ๐Ÿšซ let and const Are Not Fully Hoisted console.log(b); // ❌ ReferenceError: Cannot access 'b' before initialization let b = 20; ✅ Explanation: let and const are hoisted , but they are not initialized until the execution reaches that line. The time between hoisting and initialization is called the Temporal Dead Zone (TDZ) . ๐Ÿงฉ Example 2 — Function Hoi...

๐Ÿงต JavaScript Interview Prep: Single-Threaded or Multi-Threaded? Event Loop? Async Tasks?

If you’re preparing for a JavaScript interview , chances are you’ll come across questions like: Is JavaScript single-threaded or multi-threaded? How does JavaScript handle asynchronous tasks? What is the event loop, and how does it work? These are fundamental concepts that every JavaScript developer should understand — not only for interviews but also for writing efficient, non-blocking code. Let’s break them down clearly ๐Ÿ‘‡ ๐Ÿง  Question 1: Is JavaScript Single-Threaded or Multi-Threaded? The short answer is: ๐Ÿ‘‰ JavaScript is single-threaded. That means it can execute only one task at a time on a single main thread . This single thread handles: Code execution Function calls Event handling DOM updates (in browsers) So, when you run JavaScript, it processes your code line by line , synchronously . If one piece of code takes too long to finish, the rest of the program must wait — this is called blocking . ⚙️ Question 2: How Does JavaScript Handle Asynchronous Task...