Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    JSJavaScript

    Getting started

    • What is JavaScript?
    • Variables: let, const and var
    • Data types
    • Type conversion

    Operators and strings

    • Arithmetic and assignment
    • Comparison and logic
    • Strings and template literals
    • String methods
    • Numbers and Math

    Control flow

    • Conditions: if and else
    • switch and the ternary operator
    • Loops: for and while
    • Challenge: FizzBuzz

    Functions

    • Functions
    • Arrow functions
    • Parameters: default, rest and spread
    • Scope and closures
    • Callbacks and higher-order functions

    Arrays

    • Array basics
    • map, filter and find
    • reduce and sorting
    • Destructuring

    Objects

    • Object basics
    • Copies and references
    • Methods and this
    • JSON and optional chaining

    Classes and OOP

    • Classes
    • Inheritance

    JavaScript in the page

    • Controlling the page (DOM)
    • Creating elements
    • Events
    • Forms and validation
    • Saving in the browser (localStorage)

    Errors and async

    • Handling errors: try and catch
    • Timers and the event loop
    • Promises
    • async and await
    • Fetching data from an API

    Practical tools and techniques

    • Searching with regular expressions
    • Cancellation and stale results
    • Testing and debugging JavaScript

    Modern JavaScript and a project

    • Map and Set
    • Dates and localised formatting
    • Modules: import and export
    • Project: a complete to-do app
    JavaScript

    Errors and async · Lesson 35 of 45

    Timers and the event loop

    setTimeout, and why code doesn't always run in the order you expect.

    9 minutes2 games

    JavaScript does one thing at a time (single-threaded). Slow things (a timer, a server request) don't block the code: they're registered, the code carries on, and when they finish their callback waits in a queue. The event loop runs queued work once the current code has finished.

    Step by step1 / 6
    1. ▶console.log("1: start")
    2. 2setTimeout(() => {
    3. 3 console.log("3: timeout")
    4. 4}, 0)
    5. 5Promise.resolve().then(() => {
    6. 6 console.log("2: promise")
    7. 7})
    8. 8console.log("1b: end")

    Variables

    queue[]

    Output

    1: start

    Follow the order: normal code, then promises, then timers.

    Call stack: code running now

    Microtasks: promises and await

    Macrotasks: setTimeout and events

    The event loop empties the stack, then all microtasks, then one macrotask, and repeats.

    JavaScript
    let n = 0
    const id = setInterval(() => {
      n++
      console.log("tick", n)
      if (n === 3) clearInterval(id)
    }, 200)
    setTimeout(() => console.log("after 1 second"), 1000)

    Important

    setTimeout(fn, 1000) means “after at least one second”, not exactly. If the code is busy, it waits longer.

    Interview question

    Explain the event loop. What order does the code above print in?

    What's the output?

    setTimeout(() => console.log("B"), 0)
    console.log("A")

    What will this code print?

    What's the output?

    setTimeout(() => console.log("timer"), 0)
    Promise.resolve().then(() => console.log("promise"))
    console.log("sync")

    What will this code print?

    Learn more from

    • javascript.info: Event loop: microtasks and macrotasksjavascript.info
    • MDN: The event loopdeveloper.mozilla.org
    • javascript.info: Scheduling: setTimeout and setIntervaljavascript.info

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousHandling errors: try and catchNextPromises
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.