What is scope? What does lexical mean?
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Scope & lexical environments
Definition:
Scope is the part of a program where a variable can be accessed. JavaScript uses lexical scope, which means the available outer variables depend on where a function was defined, not where it is later called. The engine searches outward through surrounding scopes when a name is not found locally.
Key points:
-
Scope decides where a variable is available.
-
Lexical scope depends on where code is written.
-
An inner function can use variables from its outer scope.
-
Block scope applies to
letandconstinside braces;varnormally belongs to the enclosing function. -
A local variable can shadow an outer variable with the same name, which can make debugging confusing.
Interview point:
- A function carries its access to outer scopes even when passed to another function.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What is scope? What does lexical mean?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.