// program showing block-scoped concept
// global variable
let a = 'Hello';
function greet() {
// local variable
let b = 'World';
console.log(a + ' ' + b);
if (b == 'World') {
// block-scoped variable
let c = 'hello';
console.log(a + ' ' + b + ' ' + c);
}
// variable c cannot be accessed here
console.log(a + ' ' + b + ' ' + c);
}
greet();
//The let and const keywords provide block-scoping
//Global scoped
let global = "I'm Global Scoped";
{
//Block Scoped
let a = "I'm Block Scoped";
const b = "I'm Block Scoped";
//Var is not block scoped(don't use var)
var c = "I'm not Block Scoped";
}
//Variable a and b cannot be accessed as they are block scoped
console.log(a,b,c, global); //c and global can be accessed.