Hi Muhammad!
As explained above, a block is a portion of code enclosed by curly braces: {}.
An if statement, for example, would look something like this:
let y = 2;
if (y = 2) {
let x = 5;
}
In the above example, x cannot be accessed outside that block.
If we were to run this:
let y = 2;
if (y = 2) {
let x = 5;
}
console.log(x);
The console would return a reference error; because outside that block, x behaves as if it has not been declared at all.
Variables declared within a block using let or const cannot be referenced outside that block, and will essentially not be 'recognized' outside of that block.
Muhammad Saleh Bulah
Web Developer
Yeah I have gotten fully understand of this concept of how to declare tmtge variable, but I want more explanation on scope (Block scope)
Thank you!