Beginner JavaScript Interview QuestionsBeginnerConcept
JavaScript · Question 6
What is scope in JavaScript, and what are global, function, and block scope?
Direct answer
Scope determines where a variable or function can be accessed; JavaScript commonly uses global scope, function scope, and block scope.
Scope is the set of places in code where an identifier can be resolved. Inner scopes can normally access identifiers from their outer lexical scopes, but outer scopes cannot directly access variables declared only inside an inner scope.
- Global scope: values declared at the top level and available broadly within the current script or module rules.
- Function scope: bindings such as var and function-local declarations that are visible throughout a function.
- Block scope: let, const, and class declarations restricted to blocks such as if statements, loops, and standalone braces.