Every call = new world
One invocation, one Execution Context.
- A call creates a brand new Execution Context with its own Variable Environment.
- Parameters, locals, and nested function declarations live inside it.
- Return value is sent back; the entire context is destroyed after return.
- Calling the same function N times = N independent contexts, no shared state.
Same name, different memory
Why local variables never collide.
var x = 10ina()andvar x = 100inb()live in different memory slots.- Neither touches the global
x. - When
a()returns, itsxis gone —b()can’t read it. - This isolation is what a “scope” is, underneath the word.
Missing var is dangerous
Implicit globals and strict mode.
x = 10(novar/let/const) → writes to the global variable.- In non-strict mode, it silently creates one if none exists.
- In strict mode →
ReferenceError: x is not defined. - Prefer strict mode everywhere to catch this at the source.
Lexical scoping
Scope is decided where you write, not where you call.
- Function
adefined at the top level → its scope chain goes through the global scope. - Even if
ais called from insideb, it cannot seeb’s locals. - JavaScript uses static (lexical) scoping, not dynamic.
- This is why closures work — inner functions remember the scope they were born into.
Primitives vs objects
By value, by reference.
- Primitives (number, string, boolean, null, undefined, symbol, bigint) → passed by value.
- Objects (arrays, objects, functions) → passed by reference (technically, a reference copy).
- Mutating
o.valinside the function changes the caller’s object. - Reassigning the parameter (
o = {}) only changes the local variable.
The arguments object
Every regular function gets one.
- Array-like, not an array — has
lengthand indexed access, but nomap/forEach. - Available in every non-arrow function, regardless of declared parameters.
- Arrow functions do not have their own
arguments— they borrow the enclosing one. - Modern code prefers rest params (
...args) — cleaner and array-native.
Comments
Comments are disabled in this environment. Set
PUBLIC_GISCUS_REPO,PUBLIC_GISCUS_REPO_ID, andPUBLIC_GISCUS_CATEGORY_IDto enable.