I happen to be familiar with pancake stacks… short stacks, high stacks, all stacks. So when you hear the word “stack,” the first thing that comes to mind is naturally pancakes. Luckily, this just happens to be a good visual metaphor for the software stack.
What is a stack
A software stack is an array-like data type that behaves as a collection of elements. In this visual example, each pancake represents an element added to the top of the stack. You can remove each element only from the top. Because of this linear structure, the stack is also called LIFO or last-in-first-out.
Stacks have two main operations: push and pop.If you are familiar with arrangement, it’s nice to know that these methods are exactly what you have in mind: push adds an element to the top of the stack, pop removes the most recently added element from the top. increase. Unlike an array (or stack of pancakes), you can’t access elements from the bottom or middle without first removing the top element.
What is the JS call stack
A JavaScript call stack is an example of a stack data structure. In the JS call stack, elements are function calls. Each time you call a function it is added to the stack. If there are nested functions, the nested functions are also added to the top of the stack. You can think of the call stack as a kind of to-do list in JavaScript.
Stack and JS call stack
A JavaScript call stack is an example of a stack data structure. In the JS call stack, elements are function calls. Each time you call a function it is added to the stack. If there are nested functions, the nested functions are also added to the top of the stack. You can think of the call stack as a kind of to-do list in JavaScript.
It’s important to remember that JavaScript is largely a single-threaded process. So JavaScript processes her one command at a time.Asynchronous actions are handled differently and are highly recommended this video If you want to know more about it.
If you push all the functions that a particular script needs to run to the top of the stack in call order, JavaScript will start resolving them in order from top to bottom and pop them off the stack.
In this video the function is printSquare
Called at the bottom of the screen.of printSquare
A function is added to the call stack but cannot be resolved until it receives the return value of the nested function it called square
The .square function has been added to the call stack, but cannot be resolved until it receives the following return value: this is nested function called multiply
. of multiply
The function is pushed to the top of the stack, has a return statement that can return immediately, so it goes ahead and completes, and is popped off the top of the stack. The call stack continues like this, popping functions in the opposite order they were called.
Researching this article was like managing your own call stack. Starting a series of investigations inevitably resulted in nested questions. I had to add it to the top of my “to do” list to figure it out before continuing. While researching that her second question, I came across another topic, added it to the top of the list, then moved on to the previous subject, and so on and so forth. By solving the latest question I added, I was able to remove it from the top of the list and move on to the next question below it until I was ready to go back to level 1 and write this blog.
how to practice stack
Chrome browser is very robust. DevTools panelOne of the things you can do with the panel is step through the code Line by line, see it pushed onto the stack.To open it, right-click (or type Cmd + option + j
) on any web page in Chrome,[検査]Select the option, at the top[ソース]Select a tab. I made a small video with a demo of stack calls with breakpoints and a step through tool.
add a breakpoint in aDayInTheLife
Pause the code so you can step through each cascading function. As you can see, functions are added to the stack from the bottom up, and when a function receives a return value and is resolved, it is resolved from the top down. Finally, the string “Made the bus in seconds flat” is passed all the way back to the original bus. aDayInTheLife
It works and finally returns to the console once the stack is cleared.
What is Stack Overflow
The basic assumption is that a computer’s memory is finite, and if more elements are pushed onto the stack than there is space, the stack will overflow. The most common cause of stack overflow situations is infinite or very deep recursion.a recursive function that calls itself. Or imagine her two functions calling each other. The first function adds her second function to the call stack, and the second function adds the first function to the call stack. Nothing is returned or resolved, so the stack overflows.
Calling two functions to each other will cause a stack overflow.
As you can see, the Chrome dev tools have error handling for this and after 16,000 frames (call stack elements) the error occurs.
The Beatles say hello and goodbye forever — no error handling needed.
Here are some fun facts. The website, Stack Overflow, is named 2008 coding horror poll Deployed by a creator who was looking for help naming their website. Some of the other options were humbledeveloper.com and writeoncereadmany.com. 25% of the nearly 7,000 votes went to stackoverflow.com to be the winner.
Now… who’s hungry?