JavaScript Runtime Quiz: Node.js, ASI and Event Loop Practice
Test your JavaScript fundamentals with 12 questions and three programs on ECMAScript, V8, Node.js, reading stdin, semicolon insertion and event loop order.
- Course: JavaScript study plan
- Module: JavaScript and its runtime
- Kind: Checkpoint — cleared at 70%
- Reading time: 24 min
- Runtime: Node 16
Checkpoint — JavaScript and its runtime is the checkpoint that closes the JavaScript and its runtime module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers what JavaScript is and how ECMAScript is specified, engines and the JIT pipeline, Node as a runtime with its globals, reading stdin and writing exact output, statements versus expressions and automatic semicolon insertion, strict mode, and the event loop's ordering of synchronous code, microtasks and tasks.
How it works. Twelve questions and three programs; 70% on the questions and every program accepted clears the module.
Before you start, make sure you can answer:
- The difference between JavaScript, ECMAScript and V8.
- The engine pipeline from source to machine code, and why predictable object shapes are fast.
- What
readFileSync(0, "utf8")does and why"".split(/\s+/)needs a check. - Why
console.logof an array is wrong for a judge, and how to print many lines fast. - The three places ASI bites, and what strict mode changes.
- The order of synchronous logs, promise callbacks and
setTimeout(0)callbacks.
The programs are a token-cursor input parser that survives ragged lines, a report formatter that builds exact output, and an event-loop simulator that prints the order a script's logs would appear in.
Common questions
What is the difference between JavaScript, ECMAScript and V8?
JavaScript is the language, ECMAScript is its specification, published every year by TC39, and V8 is one engine that implements that specification — the one inside Chrome, Edge, Node.js and Deno.
Why does splitting an empty string return one element in JavaScript?
Splitting an empty string on whitespace returns [""], an array holding one empty string, not an empty array. Parsing empty input that way yields one bogus token, so check for empty input before you split it.
In what order do console.log, Promise.then and setTimeout 0 run?
Synchronous code runs first, then every promise callback, because microtasks drain completely after the current task, and then the setTimeout(0) callback, which is a task that waits for the next turn of the event loop.
Where does automatic semicolon insertion cause bugs?
In three places: return with its value on the next line, which returns undefined; a line beginning with (, [ or a backtick, which continues the previous line; and ++ or -- at the start of a line, which binds to that line rather than the one before.
Exercises
Ragged input census
Read all of standard input. Print lines=<number of non-blank lines>, tokens=<number of whitespace-separated tokens>, numbers=<how many tokens are finite numbers by Number()> and sum=<sum of those numbers> (print integers without a decimal point, otherwise up to 2 decimals via Number(x.toFixed(2))). Empty input prints all zeros.
Example input
3 apples 4.5
and 2 more
→
lines=2
tokens=6
numbers=3
sum=9.5Report card
Read n and n lines <name> <s1> <s2> <s3>. For each print the name left-aligned in 8, the three scores each right-aligned in 4, and the average with one decimal right-aligned in 6 — e.g. ada 90 80 70 80.0. Then a line best=<name with the highest average; first on ties>. Build all lines first and print once.
Example: 2 then ada 90 80 70, bob 100 100 95 →
ada 90 80 70 80.0
bob 100 100 95 98.3
best=bobEvent loop simulator, nested
Simulate the ordering rules with nested scheduling. Read n tokens; each is <kind>:<label> or <kind>:<label>><kind>:<label> — the part after > is what the callback schedules when it runs (one level deep). Kinds: sync, micro, timeout. Rules: run the script (sync labels print immediately; scheduled items join their queues); drain the microtask queue fully (a microtask that schedules a microtask adds it to the end of the same drain; one that schedules a timeout adds to the task queue); then run tasks one at a time, draining microtasks after each. Print the labels in order, then order=<joined by ,>.
Example: 3 then timeout:t1>micro:m2 micro:m1>timeout:t2 sync:s →
s
m1
t1
m2
t2
order=s,m1,t1,m2,t2In this module: JavaScript and its runtime
← The event loop — a first look · The seven primitives, objects and typeof →