🔄 The Event Loop: How JavaScript Really Works 80% of candidates fail this. Don't be one of them. The Classic Test: ```js console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4'); ``` Output: 1, 4, 3, 2 --- ⚙️ How It Works Step 1: Call Stack runs sync code → 1 and 4 log. Step 2: Web APIs handle async operations while stack clears. Step 3: Event Loop checks queues in priority: ``` 1. Run ALL sync code 2. Clear ENTIRE Microtask Queue (Promises) 3. Run ONE Macrotask (setTimeout) 4. Repeat ``` Queue Examples Priority Microtasks Promises, queueMicrotask 🔥 Highest Macrotasks setTimeout, events, I/O ⬇️ Lower Why output is 1, 4, 3, 2: · Sync: 1, 4 log immediately · Microtask queue: 3 logs (all microtasks run) · Macrotask queue: 2 logs (one runs next) --- 🚨 Critical Rules ✅ Microtasks ALWAYS run before next macrotask — even with 0ms delay. ✅ Microtasks queuing microtasks? They ALL run in same cycle. ✅ Too many microtasks = UI freeze (macrotasks never run). --- 🔥 Common Questions Q: Will this log "DONE"? ```js while (true) { Promise.resolve().then(() => {}); } console.log('DONE'); ``` A: No — microtasks starve the loop forever. Q: Async/await order? ```js async function test() { console.log('A'); await Promise.resolve(); console.log('B'); } test(); console.log('C'); ``` A: A, C, B — await schedules rest as microtask. Q: 10,000 microtasks + 1 macrotask? A: All microtasks run first → page freezes. Fix: Use Web Workers or chunk work. --- 💡 Senior Takeaway Event loop knowledge = better performance: · Batch DOM updates in microtasks · Break heavy loops with setTimeout · Understand React effect scheduling --- 👇 Your turn: Write code with sync + microtask + macrotask. Post output below. #JavaScript #EventLoop #AsyncJS #FrontendInterview #WebPerformance
JavaScript Event Loop: Sync, Microtasks, Macrotasks Explained
More Relevant Posts
-
❓ What actually happens when you call fetch('/api')? So I sat down and figured it out. Here's what blew my mind 👇 💡 The JS engine itself is TINY. Just two things inside it: 📦 Memory Heap — where your objects live 📚 Call Stack — tracks what function is running That's it. It can't do timers. It can't make network requests. It can't even listen for a click. 🤯 🎭 So who does all the async work? The BROWSER does. Not JavaScript. ⚙️ Web APIs (written in C++) handle the heavy lifting on separate threads: 🌐 fetch — network requests ⏱️ setTimeout — timers 🖥️ DOM — page manipulation 🖱️ Events — clicks, scrolls, keypresses 💾 LocalStorage, Geolocation, WebSockets… When they finish, they drop callbacks into two queues: 🟢 Microtask Queue (HIGH priority) → Promises, await, queueMicrotask 🔴 Callback Queue (LOW priority) → setTimeout, click, fetch response 🔄 Then the Event Loop steps in: 1️⃣ Is the Call Stack empty? 2️⃣ Drain ALL microtasks first 3️⃣ Run ONE macrotask 4️⃣ Let the browser paint 5️⃣ Repeat forever 🎯 This explains SO much: ✅ Why a heavy loop freezes your page (stack never empties) ✅ Why Promise.then() ALWAYS beats setTimeout(fn, 0) ✅ Why async/await isn't magic — it's just microtask syntax ✅ Why single-threaded doesn't mean single-tasking 👨🍳 My favorite mental model: The JS engine is a single chef. Web APIs are robot assistants running errands in the background. The Microtask Queue is the VIP line. The Callback Queue is the regular line. The Event Loop is the maître d' — but only seats people when the chef is free. 💥 The biggest realization: "JavaScript" the language and "JavaScript" the thing running in your browser are two VERY different things. ✨ The language is small. 🌊 The runtime around it is massive. I mapped the whole thing out with diagrams — call stack traces, V8's Ignition/TurboFan pipeline, the full click-to-fetch-to-DOM lifecycle. Dropping it in the comments 👇 👋 What's something you use every day but never really looked under the hood of? #JavaScript #WebDevelopment #Frontend #V8 #EventLoop #CodeNewbie
To view or add a comment, sign in
-
🧸 Closure — Story First Imagine: 👉 A child goes outside to play 👉 But he still remembers what’s inside his house That’s a closure. 👉 A function goes outside its original scope 👉 But still remembers variables from where it was created 🧠 Real Definition:- A closure is a javascript feature where a function remembers and can access variables from its outer (lexical) scope even after the outer function has finished executing. ⚙️ Behind the Scenes (JS Engine) 💡 Example:- function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const counter = outer(); counter(); counter(); 📦 Step 1: Global Execution Context Created 1- Stored in Call Stack 2- Memory allocated: outer → function counter → undefined ⚙️ Step 2: outer() is called 👉 New Execution Context created for outer :- Inside its memory: count = 0 inner = function() {...} 👉 Normally, when outer() finishes → its memory should be deleted ❌ BUT… 👉 JavaScript sees: “inner function is still using count” So it does something special: 🔥 Closure is Created 👉 JS keeps count alive in memory 👉 Even after outer() is finished This saved memory is called: 👉 Lexical Environment ⚡ Step 3: Execution counter(); → 1 counter(); → 2 👉 Because count is remembered (not destroyed) 🧠 Where is this stored? 👉 The closure data is stored in: Execution Context memory Referenced via Scope Chain Internally kept in Heap (because it persists) (Heap is a region of memory where JavaScript stores reference types like objects, arrays, and functions.) 🔥 Why Closures Matter (Real World) Data privacy (private variables) React hooks Callbacks Event handlers #JavaScript #FrontendDeveloper #WebDevelopment #CodingJourney
To view or add a comment, sign in
-
𝐈𝐬 𝐲𝐨𝐮𝐫 `𝐮𝐬𝐞𝐄𝐟𝐟𝐞𝐜𝐭` 𝐟𝐢𝐫𝐢𝐧𝐠 𝐰𝐚𝐲 𝐭𝐨𝐨 𝐨𝐟𝐭𝐞𝐧? 𝐘𝐨𝐮 𝐦𝐢𝐠𝐡𝐭 𝐛𝐞 𝐟𝐚𝐥𝐥𝐢𝐧𝐠 𝐟𝐨𝐫 𝐚 𝐜𝐨𝐦𝐦𝐨𝐧 𝐝𝐞𝐩𝐞𝐧𝐝𝐞𝐧𝐜𝐲 𝐚𝐫𝐫𝐚𝐲 𝐭𝐫𝐚𝐩. We've all been there: you put an object or array into your `useEffect`'s dependency array, thinking it's stable. But because JavaScript compares objects and arrays by reference, even if their content is identical, `useEffect` sees a new object on every render and re-runs your effect. This can lead to performance hits, stale data, or even infinite loops. 🚫 The Problem: ```javascript function MyComponent({ data }) { const options = { id: data.id, filter: 'active' }; useEffect(() => { // This runs every render if 'options' object is new fetchData(options); }, [options]); // 'options' is a new object reference each time // ... } ``` ✅ The Fix: Stabilize with `useMemo` If your object or array doesn't logically change across renders (or only changes based on specific primitives), wrap it in `useMemo`. This memoizes the object itself, ensuring its reference remains the same unless its dependencies actually change. ```javascript function MyComponent({ data }) { const stableOptions = useMemo(() => ({ id: data.id, filter: 'active' }), [data.id]); // Only re-create if data.id changes useEffect(() => { fetchData(stableOptions); }, [stableOptions]); // Now only re-runs if stableOptions' reference changes // ... } ``` This trick is a lifesaver for optimizing `useEffect` calls, especially when dealing with complex configurations or filtering logic passed down as props. It keeps your effects clean and your app performant. What's been your biggest `useEffect` headache, and how did you solve it? #React #FrontendDevelopment #JavaScript #Performance #WebDev
To view or add a comment, sign in
-
What are the differences between var, let, and const in JavaScript? 1. var: Traditional and Globalized Scope: Function-level scope. Variables declared with var are scoped to the function they are declared in, or globally if declared outside of a function. Hoisting: var declarations are hoisted, meaning they are moved to the top of their scope, but their values are not initialized until the code reaches the declaration line. Re-declaration: You can redeclare variables declared with var within the same scope. Example: var x = 5; if (true) { var x = 10; // same variable, overwrites the previous value } console.log(x); // 10 2. let: Modern and Block-scoped Scope: Block-level scope. let is limited to the block (inside loops, conditionals, etc.) where it is declared, offering more predictable behavior. Hoisting: Variables declared with let are also hoisted, but they cannot be accessed until the execution reaches the line where they are declared, leading to a "temporal dead zone." Re-declaration: You cannot redeclare a variable declared with let in the same scope. Example: let y = 5; if (true) { let y = 10; // different variable, does not affect the outer one } console.log(y); // 5 3. const: Immutable and Block-scoped Scope: Block-level scope, just like let. Immutability: Variables declared with const cannot be reassigned after their initial assignment. However, this does not make the value immutable if it's an object or array. You can still change properties or elements within them. Re-declaration: You cannot redeclare a const variable in the same scope, just like let. Example: const z = 5; // z = 10; // Error: Assignment to constant variable. const obj = { name: "John" }; obj.name = "Jane"; // Allowed console.log(obj); // { name: "Jane" } Key Differences at a Glance: var: Function-scoped, hoisted, can be redeclared. let: Block-scoped, hoisted with TDZ (Temporal Dead Zone), cannot be redeclared in the same scope. const: Block-scoped, cannot be reassigned, cannot be redeclared, but objects/arrays declared with const can have mutable properties/elements. When to Use: Use var if you're working with legacy code or need to support older JavaScript versions (not recommended for modern JS). Use let for mutable variables that need to change their values within a block or loop. Use const for immutable values or when you want to prevent accidental reassignment, especially for constants and objects/arrays that you want to maintain structure but modify their contents. 🔧 Mastering var, let, and const will make you a more efficient JavaScript developer and help you avoid tricky bugs! 💻 #JavaScript #WebDevelopment #CodingTips #JavaScriptBestPractices #TechTips #JavaScriptForDevelopers #WebDev #Programming
To view or add a comment, sign in
-
-
Day 15/30 — JavaScript Journey Scope = Visibility 👀 Know exactly what your code can access—and where. Why Scope Matters Scope controls data flow. It decides which variables are accessible at any given point. Poor scope management leads to bugs, conflicts, and unpredictable behavior. Strong scope awareness makes your code safer, cleaner, and easier to reason about. Core Idea Think of scope as boundaries of access. Every variable lives inside a specific “zone,” and only code inside that zone (or its inner zones) can use it. Types of Scope 1. Global Scope 🌍 Accessible everywhere. Powerful—but dangerous if overused (can cause naming conflicts and hard-to-track bugs). 2. Function Scope 🔒 Variables exist only inside a function. Encapsulation starts here—protects data from leaking outside. 3. Block Scope 📦 Defined by {} (like loops or conditions). Gives precise control—limits variable exposure to only where it’s needed. Lexical Scope (The Real Engine) JavaScript determines scope based on where code is written, not where it runs. This means inner functions can access outer variables—but not the other way around. 👉 Structure defines access. Not execution. Scope Chain 🔗 When accessing a variable, JavaScript looks: Inside current scope Then moves outward Until it finds it—or throws an error This lookup process is the scope chain. Why Developers Fail Here 🚫 Overusing global variables Misunderstanding block vs function scope Assuming runtime affects scope (it doesn’t) Ignoring how deeply nested scopes interact Pro Insight 💡 Good developers write code that works. Great developers control where their data lives. Bottom Line Scope is not syntax—it’s architecture. Master it, and your JavaScript becomes predictable, scalable, and bug-resistant.
To view or add a comment, sign in
-
-
🚨 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗧𝗿𝗮𝗽 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 – 𝗣𝗮𝗿𝘁 𝟮 If you think you understand JavaScript… Try predicting these outputs 𝘄𝗶𝘁𝗵𝗼𝘂𝘁 𝗿𝘂𝗻𝗻𝗶𝗻𝗴 𝘁𝗵𝗲 𝗰𝗼𝗱𝗲 😏 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟭: 𝗛𝗼𝗶𝘀𝘁𝗶𝗻𝗴 𝗠𝘆𝘀𝘁𝗲𝗿𝘆 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑎); 𝑣𝑎𝑟 𝑎 = 10; 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑏); 𝑙𝑒𝑡 𝑏 = 20; 𝗢𝘂𝘁𝗽𝘂𝘁: undefined ReferenceError 💡 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻 • var is hoisted and initialized with undefined • let is hoisted but stays in 𝗧𝗲𝗺𝗽𝗼𝗿𝗮𝗹 𝗗𝗲𝗮𝗱 𝗭𝗼𝗻𝗲 (𝗧𝗗𝗭) 👉 Accessing b before initialization throws an error. 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟮: 𝗖𝗹𝗼𝘀𝘂𝗿𝗲𝘀 𝗶𝗻 𝗔𝗰𝘁𝗶𝗼𝗻 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑜𝑢𝑡𝑒𝑟() { 𝑙𝑒𝑡 𝑐𝑜𝑢𝑛𝑡 = 0; 𝑟𝑒𝑡𝑢𝑟𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑖𝑛𝑛𝑒𝑟() { 𝑐𝑜𝑢𝑛𝑡++; 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑐𝑜𝑢𝑛𝑡); }; } 𝑐𝑜𝑛𝑠𝑡 𝑓𝑛 = 𝑜𝑢𝑡𝑒𝑟(); 𝑓𝑛(); 𝑓𝑛(); 𝑓𝑛(); 𝗢𝘂𝘁𝗽𝘂𝘁: 1 2 3 💡 𝗪𝗵𝘆? The inner function forms a 𝗰𝗹𝗼𝘀𝘂𝗿𝗲, remembering count even after outer() has finished execution. 👉 This is heavily used in 𝗥𝗲𝗮𝗰𝘁 𝗵𝗼𝗼𝗸𝘀 & 𝗿𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝗮𝗽𝗽𝘀. 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟯: 𝗣𝗿𝗼𝗺𝗶𝘀𝗲 𝘃𝘀 𝘀𝗲𝘁𝗧𝗶𝗺𝗲𝗼𝘂𝘁 (𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽) 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔("𝑆𝑡𝑎𝑟𝑡"); 𝑠𝑒𝑡𝑇𝑖𝑚𝑒𝑜𝑢𝑡(() => { 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔("𝑇𝑖𝑚𝑒𝑜𝑢𝑡"); }, 0); 𝑃𝑟𝑜𝑚𝑖𝑠𝑒.𝑟𝑒𝑠𝑜𝑙𝑣𝑒().𝑡ℎ𝑒𝑛(() => { 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔("𝑃𝑟𝑜𝑚𝑖𝑠𝑒"); }); 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔("𝐸𝑛𝑑"); 𝗢𝘂𝘁𝗽𝘂𝘁: Start End Promise Timeout 💡 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻 • Promise callbacks go to Microtask Queue • setTimeout goes to Macrotask Queue 👉 𝗠𝗶𝗰𝗿𝗼𝘁𝗮𝘀𝗸𝘀 are executed before 𝗺𝗮𝗰𝗿𝗼𝘁𝗮𝘀𝗸𝘀. 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟰: 𝗧𝗿𝗶𝗰𝗸𝘆 𝗘𝗾𝘂𝗮𝗹𝗶𝘁𝘆 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔([] == 𝑓𝑎𝑙𝑠𝑒); 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔([] === 𝑓𝑎𝑙𝑠𝑒); 𝗢𝘂𝘁𝗽𝘂𝘁: true false 💡 𝗪𝗵𝘆? • == allows 𝘁𝘆𝗽𝗲 𝗰𝗼𝗲𝗿𝗰𝗶𝗼𝗻 • [] → "" → 0 and false → 0 → ✅ true • === checks strict equality (no coercion) → ❌ false 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝟱: 𝗢𝗯𝗷𝗲𝗰𝘁 𝗥𝗲𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗧𝗿𝗮𝗽 𝑐𝑜𝑛𝑠𝑡 𝑎 = { 𝑛𝑎𝑚𝑒: "𝐽𝑆" }; 𝑐𝑜𝑛𝑠𝑡 𝑏 = 𝑎; 𝑏.𝑛𝑎𝑚𝑒 = "𝑅𝑒𝑎𝑐𝑡"; 𝑐𝑜𝑛𝑠𝑜𝑙𝑒.𝑙𝑜𝑔(𝑎.𝑛𝑎𝑚𝑒); 𝗢𝘂𝘁𝗽𝘂𝘁: React 💡 𝗘𝘅𝗽𝗹𝗮𝗻𝗮𝘁𝗶𝗼𝗻 Objects are stored by reference, not value. Both a and b point to the 𝘀𝗮𝗺𝗲 𝗺𝗲𝗺𝗼𝗿𝘆 𝗹𝗼𝗰𝗮𝘁𝗶𝗼𝗻. 💬 𝗜𝗻𝘁𝗲𝗿𝘃𝗶𝗲𝘄 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 If you can confidently answer these, you're ahead of 𝟴𝟬% 𝗼𝗳 𝗰𝗮𝗻𝗱𝗶𝗱𝗮𝘁𝗲𝘀. Most developers fail not because they don’t know JavaScript… But because they don’t understand its 𝗰𝗼𝗿𝗲 𝗯𝗲𝗵𝗮𝘃𝗶𝗼𝗿 𝗱𝗲𝗲𝗽𝗹𝘆. 🔥 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝗳𝗼𝗿 𝗬𝗼𝘂 console.log(typeof null); console.log(typeof NaN); Drop your answers in the comments 👇 ♻️ Follow Shubham Kumar Raj for more such high-value interview content #javascript #frontenddeveloper #codinginterview #webdevelopment #programming #learnjavascript #100daysofcode #hiring
To view or add a comment, sign in
-
🧠 𝗗𝗼 𝗬𝗼𝘂 𝗥𝗲𝗮𝗹𝗹𝘆 𝗞𝗻𝗼𝘄 𝗪𝗵𝗮𝘁 `new` 𝗗𝗼𝗲𝘀 𝗜𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁? ⤵️ The new Keyword in JavaScript: What Actually Happens ⚡ 🔗 𝗥𝗲𝗮𝗱 𝗵𝗲𝗿𝗲: https://lnkd.in/dyAXzDHD 𝗧𝗼𝗽𝗶𝗰𝘀 𝗰𝗼𝘃𝗲𝗿𝗲𝗱 ✍🏻: ⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ ⇢ What actually happens internally when you use `new` ⇢ The 4-step process: create → link → run → return ⇢ Constructor functions & how they really work ⇢ Prototype linking & why it matters ⇢ How instances share methods but keep separate data ⇢ Recreating `new` manually (deep understanding) ⇢ What goes wrong when you forget `new` ⇢ Debugging real-world bugs related to constructors ⇢ new vs ES6 classes — what's really different ⇢ Key tradeoffs & hidden pitfalls Thanks Hitesh Choudhary Sir & Piyush Garg Sir, and the amazing Chai Aur Code community 🙌 #ChaiAurCode #JavaScript #WebDevelopment #Programming #SystemDesign #Frontend #Hashnode
To view or add a comment, sign in
-
Complete JavaScript Road Map A-Z JavaScript🎯 1.Variables ↳ var ↳ let ↳ const 2. Data Types ↳ number ↳ string ↳ boolean ↳ null ↳ undefined ↳ symbol 3.Declaring variables ↳ var ↳ let ↳ const 4.Expressions Primary expressions ↳ this ↳ Literals ↳ [] ↳ {} ↳ function ↳ class ↳ function* ↳ async function ↳ async function* ↳ /ab+c/i ↳ string ↳ ( ) Left-hand-side expressions ↳ Property accessors ↳ ?. ↳ new ↳ new .target ↳ import.meta ↳ super ↳ import() 5.operators ↳ Arithmetic Operators: +, -, *, /, % ↳ Comparison Operators: ==, ===, !=, !==, <, >, <=, >= ↳ Logical Operators: &&, ||, ! 6.Control Structures ↳ if ↳ else if ↳ else ↳ switch ↳ case ↳ default 7.Iterations/Loop ↳ do...while ↳ for ↳ for...in ↳ for...of ↳ for await...of ↳ while 8.Functions ↳ Arrow Functions ↳ Default parameters ↳ Rest parameters ↳ arguments ↳ Method definitions ↳ getter ↳ setter 9.Objects and Arrays ↳ Object Literal: { key: value } ↳ Array Literal: [element1, element2, ...] ↳ Object Methods and Properties ↳ Array Methods: push(), pop(), shift(), unshift(), splice(), slice(), forEach(), map(), filter() 10.Classes and Prototypes ↳ Class Declaration ↳ Constructor Functions ↳ Prototypal Inheritance ↳ extends keyword ↳ super keyword ↳ Private class features ↳ Public class fields ↳ static ↳ Static initialization blocks 11.Error Handling ↳ try, ↳ catch, ↳ finally (exception handling) ADVANCED CONCEPTS: 12.Closures ↳ Lexical Scope ↳ Function Scope ↳ Closure Use Cases 13.Asynchronous JavaScript ↳ Callback Functions ↳ Promises ↳ async/await Syntax ↳ Fetch API ↳ XMLHttpRequest 14.Modules ↳ import and export Statements (ES6 Modules) ↳ CommonJS Modules (require, module.exports) 15.Event Handling ↳ Event Listeners ↳ Event Object ↳ Bubbling and Capturing 16.DOM Manipulation ↳ Selecting DOM Elements ↳ Modifying Element Properties ↳ Creating and Appending Elements 17.Regular Expressions ↳ Pattern Matching ↳ RegExp Methods: test(), exec(), match(), replace() 18.Browser APIs ↳ localStorage and sessionStorage ↳ navigator Object ↳ Geolocation API ↳ Canvas API 19.Web APIs ↳ setTimeout(), setInterval() ↳ XMLHttpRequest ↳ Fetch API ↳ WebSockets 20.Functional Programming ↳ Higher-Order Functions ↳ map(), reduce(), filter() ↳ Pure Functions and Immutability 21.Promises and Asynchronous Patterns ↳ Promise Chaining ↳ Error Handling with Promises ↳ Async/Await 22.ES6+ Features ↳ Template Literals ↳ Destructuring Assignment ↳ Rest and Spread Operators ↳ Arrow Functions ↳ Classes and Inheritance ↳ Default Parameters ↳ let, const Block Scoping 23.Browser Object Model (BOM) ↳ window Object ↳ history Object ↳ location Object ↳ navigator Object 24.Node.js Specific Concepts ↳ require() ↳ Node.js Modules (module.exports) ↳ File System Module (fs) ↳ npm (Node Package Manager) 25.Testing Frameworks ↳ Jasmine ↳ Mocha ↳ Jest #LinkedIn #CareerGrowth #SuccessMindset #Networking #Leadership
To view or add a comment, sign in
-
🧠 Memory Management in JavaScript — What Every Developer Should Know Memory management is something many JavaScript developers ignore… until performance issues start appearing 🚨 Let’s break it down 👇 🔹 How JavaScript Stores Data JavaScript uses two types of memory: 👉 Stack (Primitive Data Types) Stored directly in memory (fast & simple) Examples: • number • string • boolean • null • undefined • bigint • symbol Example: let a = 10; let b = a; // copy of value 👉 Each variable gets its own copy ✅ 👉 Heap (Reference Data Types) Stored as references (complex structures) Examples: • objects • arrays • functions Example: let obj1 = { name: "Kiran" }; let obj2 = obj1; obj2.name = "JS"; console.log(obj1.name); // "JS" 👉 Both variables point to the same memory location ❗ 🔹 Garbage Collection (GC) JavaScript uses “Mark and Sweep”: Marks reachable data Removes unreachable data 💡 If something is still referenced, it won’t be cleaned 🔹 Common Memory Leak Scenarios ⚠️ Even with GC, leaks can happen: • Global variables • Closures holding large data • Unstopped setInterval / setTimeout • Detached DOM elements 🔹 How to Avoid Memory Issues ✅ Use let/const ✅ Clear timers (clearInterval / clearTimeout) ✅ Remove unused event listeners ✅ Avoid unnecessary references ✅ Use Chrome DevTools → Memory tab 🔹 Pro Tip 💡 Performance issues are often not slow code — they’re memory that never gets released 🚀 Final Thought Understanding Stack vs Heap gives you a huge edge in debugging and building scalable apps 💬 Have you ever faced a memory leak? What caused it? #JavaScript #WebDevelopment #Frontend #Performance #CodingTips #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 4: Why doesn't JavaScript get confused? 🧩 Variable Environments & The Call Stack(How Functions works in JS) Today I explored how JavaScript handles multiple variables with the same name across different functions. Using the code below, I took a deep dive into the Variable Environment and the Call Stack. 💻 The Code Challenge: javascript var x = 1; a(); b(); console.log(x); function a() { var x = 10; console.log(x); } function b() { var x = 100; console.log(x); } 🧠 The "Behind the Scenes" Logic: 1️⃣ Global Execution Context (GEC): Memory Phase: x is set to undefined. Functions a and b are stored entirely. Execution Phase: x becomes 1. Then, a() is invoked. 2️⃣ The Function a() Context: A brand new Execution Context is created and pushed onto the Call Stack. This context has its own Variable Environment. The x inside here is local to a(). It logs 10, then the context is popped off the stack and destroyed. 3️⃣ The Function b() Context: Same process! A new context is pushed. Its local x is set to 100. It logs 100, then it's popped off the stack. 4️⃣ Back to Global: Finally, the last console.log(x) runs in the Global context. It looks at the GEC’s Variable Environment where x is still 1. 📚 Key Learnings: Variable Environment: Each execution context has its own "private room" for variables. The x in a() is completely different from the x in the Global scope. Call Stack: It acts as the "Manager," ensuring the browser knows exactly which execution context is currently running. Independence: Functions in JS are like mini-programs with their own memory space. This is the foundation for understanding Lexical Scope and Closures! Watching this happen live in the browser's "Sources" tab makes you realize that JS isn't "magic"—it's just very well-organized! 📂 #JavaScript #WebDevelopment #CallStack #ExecutionContext #ProgrammingTips #FrontendEngineer #CodingLogic
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