JavaScript can feel like magic. But magic is just tricks you don’t see. Let’s pull back the curtain on two big ideas: Execution Context and this
. By the end, you’ll see why they matter and why you should care.
What Is an Execution Context?
Think of every bit of code as a kitchen.
- Global Kitchen is the main studio.
- Function Kitchen is a new room a chef builds each time they cook.
- Eval Kitchen is a secret room you rarely visit.
Each kitchen has its own tools (functions) and ingredients (variables). When you run code, JS picks a kitchen and works there.
The Three Steps
-
Creation
- JS sets all
var
toundefined
. -
let
/const
are known but not ready.
- JS sets all
-
Execution
- JS runs your lines one by one.
- Values fill the blanks.
-
Exit
- Kitchen closes.
- Its tools and ingredients go away.
I firmly believe that knowing these steps stops 80% of your bugs.
Understanding this
In JS, this
is the chef’s badge it tells you who’s cooking right now.
Where you are |
this is… |
---|---|
In the global scope | The global object |
Inside a plain function | Depends on how it’s called |
Inside an object’s method | That object |
Inside an arrow function | Same this as outer scope |
function show() {
console.log(this); // window in browser
}
const user = {
name: "mimi",
greet() {
console.log(this.name); // "mimi"
}
};
show();
user.greet();
Lexical Environment: A Simple Tree
“Lexical” means “where it’s written.” Imagine your code as a tree:
Global
└ main()
└ outer()
└ inner()
Each node holds its own vars and sees its parent’s vars, but not its child’s. That rule is non-negotiable.
let x = "earth";
function main() {
let y = "home";
function outer() {
let z = "door";
function inner() {
let w = "mind";
console.log(x, y, z, w); // earth home door mind
}
inner();
console.log(w); // Error: w is not here
}
outer();
}
main();
Why This Matters
- Fewer Surprises. You know where your vars live.
- Clear Bugs. You see why you got
undefined
or the wrongthis
. - Better Code. You write functions that do just what you expect.
Top comments (0)