In JavaScript, scope is the context that determines where variables can be accessed, helping write cleaner and error-free code.
// Declaring a global variable
let x = 10;
function func() {
// Declaring a local variable
let y = 20;
// Accessing Local and Global
// variables
console.log(x,",", y);
}
func();
Output
TechCorp , Alice
There are mainly different types of scopes of a variable. Let us understand these one by one.
1. Global and Local Scope
The image below shows Global and Local Scope in JavaScript to help understand their accessibility.