Skip to content

Commit 4ee3e81

Browse files
committedAug 30, 2024
add call stack
1 parent 312bbe4 commit 4ee3e81

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
 
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Call Stack
2+
3+
What
4+
5+
- A callback is a function that is passed as an argument to another function. It's a way to defer the execution of a function until a certain event occurs or a condition is met.
6+
7+
Why
8+
9+
- Function Calls: Keeps track of the order of function calls and their return addresses.
10+
- Execution Context: Stores information about each function call, including local variables, arguments, and the return address.
11+
12+
How
13+
14+
- Function Call: When a function is called, its execution context is pushed onto the call stack.
15+
- Execution: The function's code is executed.
16+
- Return: When the function returns, its execution context is popped off the stack, and the returned value is passed back to the calling function.
17+
18+
Example
19+
20+
```javascript
21+
function greet(name) {
22+
console.log("Hello, " + name + "!");
23+
}
24+
25+
function main() {
26+
greet("Alice");
27+
console.log("Main function finished.");
28+
}
29+
30+
main();
31+
```
32+
33+
Call Stack:
34+
35+
- main function is called.
36+
- main's execution context is pushed onto the stack.
37+
- greet is called within main.
38+
- greet's execution context is pushed onto the stack.
39+
- greet executes and returns.
40+
- greet's execution context is popped off the stack.
41+
- main continues execution.
42+
- main's execution context is popped off the stack.
43+
44+
Note
45+
46+
- The call stack plays a crucial role in managing the flow of execution in JavaScript. Understanding the call stack is essential for debugging and understanding how functions are called and return values.
47+
48+
Analogy
49+
50+
- Think of the call stack as a stack of plates. The most recently added plate is the top plate, and it's the first one to be removed when you need a plate. Similarly, in JavaScript, the most recent function call is at the top of the stack, and it's the first one to be removed when the function returns.

‎javascript_under_the_hood/javascript_engine.md

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ Why
1010
- Optimizes performance: Employs techniques like Just-In-Time (JIT) compilation to improve execution speed.
1111
- Manages memory: Handles memory allocation and garbage collection.
1212

13+
How
14+
15+
- Parsing: Breaks down JavaScript code into smaller parts (tokens).
16+
- Abstract Syntax Tree (AST): Creates a tree-like structure representing the code's structure.
17+
- Interpretation or Compilation: Executes code directly or compiles it into machine code.
18+
- Execution: Runs the compiled code or interpreted instructions.
19+
1320
Example
1421

1522
- V8: Used in Google Chrome and Node.js.

0 commit comments

Comments
 (0)
Please sign in to comment.