Watch it run
Pick a snippet, then play it or step through. Every step says what just happened.
function square(n) { return n * n;}function sumOfSquares(a, b) { const total = square(a) + square(b); return total;}console.log(sumOfSquares(2, 3));console.log("done");Call stack
The top frame is running
Each frame's variables
Blue: sumOfSquares. Amber: square.
Console0 lines
Nothing has run yet. Declaring the two functions doesn't call them, so the stack is empty.
Try it yourself
Change the code, predict the order, then run it for real in your browser.
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.
Reading a stack trace
When an error is created it records the frames that are on the stack at that moment, newest first. The top line is where it happened; every line under it is the caller of the line above. Run this and read the output from the top down: c, called by b, called by a, called by the script itself.
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
Run to completion
While a function is on the stack, nothing else runs: not a click handler, not a timer that is already due. A loop that keeps the thread busy for half a second delays everything else by half a second, which is why one slow function makes a page feel frozen.
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
Too deep
The stack has a fixed size. A function that calls itself without a way to stop fills it up, and the engine throws instead of crashing.
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
If you need to process something very deep (a long linked list, a huge tree), use a loop with your own array as the stack instead of recursion.