Interview Question

What is block scope?

let and const can create bindings limited to a block.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

Block scope limits a lexical binding to a block and its nested blocks. • `let`, `const`, class, and some function declarations use lexical block rules. • `var` does not create a block-scoped binding. • The binding cannot be accessed outside its block.

Example

Code
{
  const message = 'inside';
  console.log(message);
}
Output
inside

Quick Revision

let and const can create bindings limited to a block.