Understanding JavaScript Event Loop with Microtasks and Macros

💡 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐄𝐯𝐞𝐧𝐭 𝐋𝐨𝐨𝐩 𝐓𝐫𝐢𝐜𝐤 (𝐖𝐢𝐭𝐡 𝐚 𝐓𝐫𝐢𝐜𝐤𝐲 𝐄𝐱𝐚𝐦𝐩𝐥𝐞) JS event loop questions look confusing… until you apply 𝒐𝒏𝒆 𝒓𝒖𝒍𝒆 👇 👉 𝐒𝐲𝐧𝐜 → 𝐀𝐥𝐥 𝐌𝐢𝐜𝐫𝐨𝐭𝐚𝐬𝐤𝐬 → 𝐎𝐧𝐞 𝐌𝐚𝐜𝐫𝐨𝐭𝐚𝐬𝐤 → 𝐀𝐥𝐥 𝐌𝐢𝐜𝐫𝐨𝐭𝐚𝐬𝐤𝐬 → 𝐫𝐞𝐩𝐞𝐚𝐭 🔍 𝑾𝒉𝒚 𝒕𝒉𝒊𝒔 𝒘𝒐𝒓𝒌𝒔 (𝒔𝒊𝒎𝒑𝒍𝒆 𝒕𝒉𝒊𝒏𝒌𝒊𝒏𝒈) 𝐴, 𝐹 → sync 𝐷  → microtask 𝐵 → first timer 𝐶 → microtask inside timer 𝐸 → timer inside microtask (runs last) Microtasks 𝐚𝐥𝐰𝐚𝐲𝐬 𝐟𝐢𝐧𝐢𝐬𝐡 𝐟𝐢𝐫𝐬𝐭 before moving to next timer. 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐓𝐢𝐩 Don’t try to “guess” the output. Just ask: - Is it 𝐬𝐲𝐧𝐜? - Is it 𝐦𝐢𝐜𝐫𝐨𝐭𝐚𝐬𝐤? - Is it 𝐦𝐚𝐜𝐫𝐨𝐭𝐚𝐬𝐤? The answer becomes obvious. If this helped you, give it a 👍 Save it for interviews. #JavaScript #EventLoop #Frontend #Interviews #WebDev #React

  • text

To view or add a comment, sign in

Explore content categories