Variable Scope JavaScript
-
What is a scope?
- A scope is a certain region of a program where a defined variable exists & can be recognized & beyond that they cannot be recognized.
-
What are the types of Scopes?
- Global Scope
- Block Scope
- Functional Scope
-
varis functional scoped
{
var a = 5;
}
console.log(a); // 5
let&constare block scoped
{
let a = 5;
}
console.log(a); // ReferenceError: a is not defined
{
let a = 5;
console.log(a); // 5
}
{
const a = 5;
}
console.log(a); // ReferenceError: a is not defined
{
const a = 5;
console.log(a); // 5
}