logologo
  • AI Interviewer
  • Features
  • AI Tools
  • FAQs
  • Jobs
logologo

Transform your hiring process with AI-powered interviews. Screen candidates faster and make better hiring decisions.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Certifications
  • Topics
  • Collections
  • Articles
  • Services

AI Tools

  • AI Interviewer
  • Xperto AI
  • AI Pre-Screening

Procodebase © 2025. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

An In-depth Look at Event Loop in JavaScript

author
Generated by
Abhishek Goyan

21/07/2024

JavaScript

Sign in to read full article

JavaScript is a single-threaded, non-blocking asynchronous language, which means that code is executed in a sequential manner. The event loop is a mechanism that allows JavaScript to efficiently handle asynchronous operations. When an asynchronous task is encountered, it is placed in the event queue and executed when the current call stack is empty.

There are two types of tasks in the event loop: macro tasks and micro tasks. Macro tasks are tasks that are added to the end of the event queue and have higher priority than micro tasks. Examples of macro tasks include setTimeout, setInterval, and I/O operations. Micro tasks, on the other hand, have higher priority and are executed before macro tasks. Examples of micro tasks include Promise callbacks, MutationObserver callbacks, and process.nextTick.

Let's take a look at an example to better understand the event loop and tasks in JavaScript:

console.log('Script start');

setTimeout(() => {
  console.log('setTimeout');
}, 0);

Promise.resolve().then(() => {
  console.log('Promise');
});

console.log('Script end');

In this example, the output will be:

Script start
Script end
Promise
setTimeout

This is because the micro task (Promise callback) is executed before the macro task (setTimeout callback). Understanding the event loop and the concept of macro and micro tasks is crucial for writing efficient and scalable JavaScript code.

Popular Tags

JavaScriptEvent LoopAsynchronous Programming

Share now!

Like & Bookmark!

Related Collections

  • JavaScript Mastery: From Basics to Advanced Techniques

    15/10/2024 | VanillaJS

  • JavaScript Interview Mastery: 20 Essential Concepts

    22/10/2024 | VanillaJS

  • JavaScript Coding Challenges for Interviews

    14/09/2024 | VanillaJS

Related Articles

  • Flatten a Nested Array Without Using the Flat Function

    14/09/2024 | VanillaJS

  • Implementing an LRU Cache in JavaScript

    12/09/2024 | VanillaJS

  • Understanding JavaScript Garbage Collection

    22/10/2024 | VanillaJS

  • An In-depth Look at Event Loop in JavaScript

    21/07/2024 | VanillaJS

  • Implementing a Debounce Function

    14/09/2024 | VanillaJS

  • Creating a Promise-Based API Request Function

    14/09/2024 | VanillaJS

  • JavaScript Performance Optimization Techniques

    22/10/2024 | VanillaJS

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design