ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

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 Coding Challenges for Interviews

    14/09/2024 · VanillaJS

  • JavaScript Interview Mastery: 20 Essential Concepts

    22/10/2024 · VanillaJS

Related articles

  • Implementing a Debounce Function

    14/09/2024 · VanillaJS

  • Error Handling in JavaScript

    22/10/2024 · VanillaJS

  • Understanding JavaScript Scope Chain

    22/10/2024 · VanillaJS

  • Understanding JavaScript Garbage Collection

    22/10/2024 · VanillaJS

  • An In-depth Look at Event Loop in JavaScript

    21/07/2024 · VanillaJS

  • An In-Depth Guide to Function Currying in Vanilla JavaScript

    22/10/2024 · VanillaJS

  • Understanding JavaScript Polyfills

    22/10/2024 · VanillaJS

Popular category

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