maximum call stack size exceeded
This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you The JS Call Stack Explained In 9 Minutes. Following along are instructions in the video below:
which functions have been invoked but arent done yet so thats what the call stack is its a structure that stores it so its like a to-do list and the reason its called a stack by the way thats a common data structure and the way those stack works is that its a last in first out structure so I like to think of it as like a pile of books I have a pile of books over here you might be wondering what I was doing with that if I want to add a book to a stack I add it to the top right Im not gonna try and add it to the bottom and like shift everything over Im gonna add it to the top just like this and then if I want to remove something from a stack I take it off the top so the first thing that was in our sorry the last thing that was in this red book is the first thing out I dont remove from the bottom that would cause a book avalanche alright thats enough of the books push those out of the way so in a similar way heres a little visualization of some function invocations dont worry about what the text says but we have a function called main and our call stack takes note of that but it actually calls multiply and our function multiply for some reason calls another function called something so these are all added to the stack so this was the last thing it was added to the top so its the first thing to come off here we go its popped off then multiply and then finally main is finished last in first out so to summarize that when you invoke a function the details of the function call the invocation are saved to the top of the stack they are pushed to the top of the stack and then when that function returns or whenever any function returns the information about the invocation is taken off the top of the stack its popped off taking the book off so heres a simple example we have a
called second thing and Im calling second thing first and that returns first thing plus the string second thing so if I call it if I just run it on its own Im going to use its a snippet so I can do command to return and I get first thing second thing but now lets see how that actually happens so Im going to add a breakpoint right here and now Im going to run the code command enter or I could click that button okay so we have our main function in this case the way they display it is anonymous and the very first thing that needs to happen Im going to step through second thing is called and you can see that its added to the stack at the beginning and you can see the the line numbers by the way so the first anonymous main function was line 9 on line 6 we had second thing now second thing doesnt return right away it needs to call first thing first first thing first and that happens if I step through it you can see its added first thing is right here now first thing is at the top and it returns the string first thing so what that means is its going to be popped off because when we return something the top value or the top frame of the stack is removed so watch it right here oh were done area now its removed so now were back at second thing and this arrow indicates whats actually being executed so we have the value of first thing which we called it was returned to us so now we just add that with second thing and weve returned that that string and youll see this gets popped off and then finally one more time were done and you can see first thing second thing appears as our return value so I have another little bit of code I just changed it a tiny bit first thing now calls second thing second thing now calls first thing so you can probably imagine whats gonna happen if I execute this code we get
but what is that error exactly there we go maximum call stack size exceeded so what this tells us is that we exceeded the number of frames that chrome allows us to have in the call stack because nothing was ever being returned were just constantly adding books and books on top of the stack over and over and over or adding frames on top of the stack and nothing was being popped off and in chrome I think the maximum call stack size is 16,000 frames so this happened 16,000 times and then it gave up and through this error maximum call stack size exceeded so this is some time this is common error especially when we talked about recursion which I have a video on that as well you will see what causes this otherwise its probably not that common to have you know a function calling one function and having the same one calling the first one but with recursion you have a function calling itself and you can run into this very very easily so to summarize everything to wrap up a stack the call stack is just an ordered set of stack frames just a bunch of its a to-do list for function calls or invocations so the bottom of the stack is the first function invoked so lets say you go back to our books this is the first function that was invoked then the next one thats invoked gets added on top and add it on top until finally something returns a value all right we returned this value so its the last in first out this was the last thing in now its the first thing out and we keep going until the last frame the last function invocation returns some value so its processed from top to bottom and on that note I think thats pretty much it thats all I have to say so goodbye thanks for watching Ill pop out from under the mic like subscribe comment whatever what am I supposed to say just do whatever you want but theres buttons you can click if you want to see more all right good bye now
Thank you for watching all the articles on the topic The JS Call Stack Explained In 9 Minutes. All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.