Example output
### What is the **Call Stack** in JavaScript?
The **call stack** is a data structure in JavaScript that manages the execution of functions. It works on a **"Last In, First Out" (LIFO)** principle, which means the last function that gets called is the first to complete. Think of the call stack as a **stack of plates**. You add (push) plates on top, and you remove (pop) plates from the top when theyβre done being used. Similarly, JavaScript adds function calls to the call stack and processes them one at a time, from top to bottom.
### How the Call Stack Works (Real-life example):
Imagine you're a chef in a kitchen, preparing multiple dishes:
1. **Function 1: Boiling pasta** β You start boiling the pasta, and while waiting, you need to prepare the sauce.
2. **Function 2: Making sauce** β You start making the sauce. Now, the pasta is still boiling, but you're working on the sauce.
3. **Function 3: Chopping vegetables** β While the sauce simmers, you start chopping vegetables. The pasta is still boiling, and the sauce is cooking.
Each task represents a function. The call stack helps JavaScript manage these tasks. Once the chopping (Function 3) is done, the chef returns to finish the sauce (Function 2), and finally, completes the pasta (Function 1).
In JavaScript:
- When a function is called, it's added to the top of the stack.
- Once the function finishes, it's removed from the stack, and JavaScript moves to the next function.
### Call Stack in Code:
Hereβs a simple example:
```javascript
function firstFunction() {
console.log('First function called');
secondFunction();
}
function secondFunction() {
console.log('Second function called');
thirdFunction();
}
function thirdFunction() {
console.log('Third function called');
}
firstFunction();
```
### What happens in the call stack:
1. The `firstFunction` is called and pushed onto the stack.
2. Inside `firstFunction`, `secondFunction` is called, and it gets pushed onto the stack.
3. Inside `secondFunction`, `thirdFunction` is called, and it gets pushed onto the stack.
4. `thirdFunction` finishes first and is removed (popped) from the stack.
5. After that, `secondFunction` finishes and is removed from the stack.
6. Finally, `firstFunction` finishes and is removed from the stack.
**Output**:
```
First function called
Second function called
Third function called
```
### Key Points to Remember:
1. The call stack follows a **Last In, First Out (LIFO)** order.
2. It tracks which function is currently being executed.
3. Functions are added (pushed) to the stack when called and removed (popped) when they finish.
4. When the stack is empty, JavaScript can run other tasks (like handling user input or timers).
### Coding Challenge:
Hereβs a small challenge to practice your understanding of the call stack:
```javascript
function greet() {
console.log('Hello');
askHowAreYou();
}
function askHowAreYou() {
console.log('How are you?');
reply();
}
function reply() {
console.log('I am fine, thank you.');
}
greet();
```
- Can you predict the output?
- Write down the order in which functions will be pushed and popped from the call stack.
### Study Tips:
1. **Visualize the call stack**: Try to think of function calls as plates stacking and unstacking.
2. **Start small**: Practice with simple function calls before moving to more complex code.
3. **Practice debugging**: Use browser developer tools to step through your code and see the call stack in action.
4. **Read documentation**: Mozilla Developer Network (MDN) is an excellent resource for understanding concepts like the call stack.
### Recommended Resources:
- **MDN Web Docs**: [JavaScript Call Stack](https://developer.mozilla.org/en-US/docs/Glossary/Call_stack)
- **JavaScript.info**: [Execution Context and Call Stack](https://javascript.info/execution-context)
- **YouTube**: Search for "JavaScript call stack" to watch visual explanations.
### Clarifying Questions:
- Do you feel confident about how the call stack processes function calls?
- Do you want to try another example with more complex function calls or recursion?
### Quiz:
1. What data structure does the call stack follow?
2. What happens when a function finishes executing?
3. What is the order in which functions are removed from the call stack?
### Quiz Answers:
1. **LIFO (Last In, First Out)**.
2. It is **removed (popped) from the call stack**.
3. The most recently called function is removed **first**.
Let me know if you'd like to dive deeper into another topic!