Block scope in JavaScript refers to the region of code enclosed by curly braces {}. Variables declared using let and const keywords within a block are limited to that block scope.
function blockFunction() {
if (true) {
let letVariable = "letVaule";
const constVariable = "constVaule";
console.log(letVariable); // Accessing block-scoped variable
console.log(constVariable); // Accessing constant block-scoped variable
}
console.log(letVariable); // not accessible
console.log(constVariable); // not accessible
}
blockFunction();
In this example, letVariable and constVariable are only accessible in if block not outside of if block.