Let’s talk about stack overflow in JS

Hi, now we know about the call stack and memory heap, but sometime we will find the definition of stack overflow (not the web site), let me explain what cause the “stack overflow”

So, let see this code that have recursion (function that calls itself)

// Recursion (function that calls itself)
function foo() {  // keep looping over and over...
  foo(); // Run Function again, don’t have end...

foo(); // Execute the function 

And if we run our code in the browser we will have this error related to the stack:

So this cause something like this in the stack, and finally we will have a “stack overflow”

As we know JS goes line through line (synchronous process):

// Array with millions of positions
let data = [5, 9, 2, 3, 2, 5,...100000000000]; //1
console.log(1); // 2
for (i = 0, i < data.length; i++) {... // 3...Massive job here
console.log(2); // 4 This console will take a lot of time to be executed

If we have this in a web site, the user will not be enable to do anything because the website will be freezer until the task number // 3 done and the user just need to wait for and this is not good for the user. 

But asynchronous task will going to pull out the line of code // 3 and in that way the page will not be freezer. 

By Cristina Rojas