JavaScriptQuestion 01 of 36
How does JavaScript work?
Answer out loud or in your head first, then compare.
Answer
JavaScript is run by an engine, e.g. V8 in Chrome and Node.js. Synchronous code runs on a single main thread and uses the call stack.
If you call a function:
JavaScript
function first() {
second();
}
function second() {
console.log("Hello");
}
first();the stack looks roughly like this:
first()
↓
second()
↓
console.log()When they finish, functions are popped off the stack.
JavaScript can also handle asynchronous operations, e.g.:
JavaScript
setTimeout(...)
fetch(...)They don't block the whole program. The browser or Node.js environment handles them, and when they finish, their callback can come back for execution through the event loop.
To be precise:
JavaScript is single-threaded as far as running JS code goes, but the whole environment doesn't have to run on one physical thread.
How did it go? Be honest — “Not yet” puts the question on your review list.