7. Popping the Hood
Activity 7.01: Finding out the Number of Stack Frames
Solution
- The function that establishes the call stack's limit is as follows:
var frameCount = 0;
function stackOverflow() { Â Â frameCount++; Â Â stackOverflow(); }
The solution starts out with the
frameCount
variable being initialized with the value0
. ThestackOverflow()
function is declared, which will add 1 to theframeCount
variable and then call itself, thus causing a stack overflow. - Now,
setTimeout()
function is initiated, which will log the value offrameCount
to the console after a minimum of 500 milliseconds. Now, call thestackOverflow()
function.setTimeout(() => console.log(frameCount), 500); stackOverflow();
This takes the
console.log
function out of the main execution thread, allowing it to be called after the stack overflow error is thrown: