1. Mozilla Developer Network (MDN) Web Docs
"Concurrency model and the Event Loop": This document explains the JavaScript runtime model. It states
"The setTimeout function is called
a message is added to the queue... However
the message is not processed until after the main script has finished executing... the console.log from within the setTimeout callback is the last thing to be logged." This confirms that synchronous code
including line 08
executes before the setTimeout callback.
Source: MDN Web Docs
"In depth: Microtasks and the JavaScript runtime environment
" Section: "The event loop."
2. HTML Living Standard
Section 8.7 "Timers": This is the official specification defining setTimeout. It details the algorithm for timers
which involves queuing a task to be run later. Step 12 of the "timer initialization steps" specifies to "queue a task" which will be invoked when the timer expires. This task is processed by the event loop only after the currently running script is complete.
Source: HTML Living Standard
WHATWG
Section 8.7
"Timers."
3. Flanagan
D. (2020). JavaScript: The Definitive Guide (7th ed.). O'Reilly Media. In the chapter on asynchronous JavaScript
the text explains: "When you call setTimeout()
the browser starts a timer. When the timer expires... the code you specified is added to a queue... The JavaScript interpreter will execute that code when it is idle (i.e.
when it is not running any other code)." This supports the conclusion that line 08 runs before the callback.
Source: Chapter 13
"Asynchronous JavaScript
" Section: "Timers."