So... a block is { }. "Scope" is what values can be seen/accessed. "Block scope" means that a value/variable declared in a block can only be seen/accessed within that block. For example: if (true) { var myVar = 'hello'; let myLet = 'hello'; } console.log(myVar); // hello console.log(myLet); // Uncaught ReferenceError: myLet is not defined ^ var variables are not block scoped and can be accessed outside of the block; let variables are block scoped and cannot be accessed outside of the block... let variables don't exist outside of the block in which they're declared Answer from ForScale on reddit.com
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_scope.asp
JavaScript Scope
Variables declared with let and const inside a code block are "block-scoped," meaning they are only accessible within that block.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ block
Block statement - JavaScript | MDN
July 29, 2025 - Combining statements into blocks is a common practice in JavaScript, especially when used in association with control flow statements like if...else and for. The opposite behavior is possible using an empty statement, where you provide no statement, although one is required. In addition, combined with block-scoped declarations like let, const, and class, blocks can prevent temporary variables from polluting the global namespace, just like IIFEs do.
Discussions

javascript - what is block scope function ECMAScript 6 compare with ECMAScript 5 - Stack Overflow
What is block scope function in ECMAScript 6? Can anyone help me understand the main difference in the block scope function compared to ECMAScript 5? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why was block scope not originally implemented in JavaScript? - Stack Overflow
I have read, and discovered through my own experience, that JavaScript doesn't have a block scope. Assuming that the language was designed this way for a reason, can you explain to me what is that ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is a real-world example of block scope being useful in JavaScript? - Stack Overflow
let and const have introduced block-level scoping to JavaScript, and I now understand the difference. But I'm yet to find a compelling real-world example where let has an advantage over var. What ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What does block scope mean?
So... a block is { }. "Scope" is what values can be seen/accessed. "Block scope" means that a value/variable declared in a block can only be seen/accessed within that block. For example: if (true) { var myVar = 'hello'; let myLet = 'hello'; } console.log(myVar); // hello console.log(myLet); // Uncaught ReferenceError: myLet is not defined ^ var variables are not block scoped and can be accessed outside of the block; let variables are block scoped and cannot be accessed outside of the block... let variables don't exist outside of the block in which they're declared More on reddit.com
๐ŸŒ r/learnjavascript
6
8
July 17, 2020
๐ŸŒ
DEV Community
dev.to โ€บ mingt โ€บ javascript-introduction-to-scope-function-scope-block-scope-d11
JavaScript: Introduction to Scope (function scope, block scope) - DEV Community
November 27, 2018 - Generally speaking, whenever you see {curly brackets}, it is a block. In ES6, const and let keywords allow developers to declare variables in the block scope, which means those variables exist only within the corresponding block.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ scope-in-javascript-global-vs-local-vs-block-scope
Scope in JavaScript โ€“ Global vs Local vs Block Scope Explained
September 4, 2024 - Local scope in JavaScript is like a private room within a building โ€“ it's an enclosed space where variables are only accessible from within that specific room. When you declare a variable in local scope, it is limited in visibility to the ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Glossary โ€บ Scope
Scope - Glossary | MDN
Function scope: The scope created with a function. In addition, identifiers declared with certain syntaxes, including let, const, class, or (in strict mode) function, can belong to an additional scope: Block scope: The scope created with a pair of curly braces (a block).
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-scope
Scope of Variables in JavaScript - GeeksforGeeks
1 week ago - In JavaScript, block scope refers to variables declared with let or const inside a { } block.
Top answer
1 of 1
49

The new let and const in ES2015 (aka "ES6") have four major differences compared with the venerable var:

  1. They have block scope

  2. They aren't hoisted (well, they're sort of hoisted, but in a useful way)

  3. Repeated declarations are errors

  4. When used at global scope, they don't create properties of the global object (despite creating global variables; this is a new concept as of ES2015)

(For what it's worth, this is covered in detail in Chapter 2 of my recent book JavaScript: The New Toys, which covers ES2015-ES2020.)

Block scope

var variables exist throughout the function they're declared in (or globally, if declared globally), they aren't confined to the block they're in. So this code is valid:

function foo(flag) {
    a = 10;
    if (flag) {
        var a = 20;
    }
    return a;
}
console.log(foo(false)); // 10
console.log(foo(true));  // 20
Run code snippetEdit code snippet Hide Results Copy to answer Expand

a is defined regardless of whether flag is true and it exists outside the if block; all three as above are the same variable.

That's not true of let (or const):

function foo(flag) {
    if (flag) {
        let a = 10;
    }
    return a;           // ReferenceError: a is not defined
}
console.log(foo(true));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

a only exists inside the block it's declared in. (For for statements, a declaration within the () of the for is handled very specially: a new variable is declared within the block for each loop iteration.) So outside the if, a doesn't exist.

let and const declarations can shadow declarations in an enclosing scope, e.g.:

function foo() {
    let a = "outer";

    for (let a = 0; a < 3; ++a) {
        console.log(a);
    }
    console.log(a);
}
foo();
Run code snippetEdit code snippet Hide Results Copy to answer Expand

That outputs

0
1
2
outer

...because the a outside the for loop isn't the same a as the one inside the for loop.

Hoisting

This is valid code:

function foo() {
    a = 5;
    var a = a * 2;
    return a;
}

Bizarre-looking, but valid (it returns 10), because var is done before anything else is done in the function, so that's really:

function foo() {
    var a;             // <== Hoisted

    a = 5;
    a = a * 2;         // <== Left where it is
    return a;
}

That's not true of let or const:

function foo() {
    a = 5;            // <== ReferenceError: a is not defined
    let a = a * 2;
    return a;
}

You can't use the variable until its declaration. The declaration isn't "hoisted" (well, it's partially hoisted, keep reading).

Earlier I said

  1. They aren't hoisted (well, they're sort of hoisted, but in a useful way)

"Sort of"? Yes. A let or const declaration shadows an identifier throughout the block in which it appears, even though it only actually takes effect where it occurs. Examples help:

function foo() {
    let a = "outer";

    for (let x = 0; x < 3; ++x) {
        console.log(a);            // ReferenceError: a is not defined
        let a = 27;
    }

}

Note that instead of getting "outer" in the console, we get an error. Why? Because the let a in the for block shadows the a outside the block even though we haven't gotten to it yet. The space between the beginning of the block and the let is called the "temporal dead zone" by the spec. Words aren't everybody's thing, so here's a diagram:

Repeated declarations

This is valid code:

function foo() {
    var a;

    // ...many lines later...
    var a;
}

The second var is simply ignored.

That's not true of let (or const):

function foo() {
    let a;

    // ...many lines later...
    let a;                     // <== SyntaxError: Identifier 'a' has already been declared
}

Globals that aren't properties of the global object

JavaScript has the concept of a "global object" which holds various global things as properties. In loose mode, this at global scope refers to the global object, and on browsers there's a global that refers to the global object: window. (Some other environments provide a different global, such as global on NodeJS.)

Until ES2015, all global variables in JavaScript were properties of the global object. As of ES2015, that's still true of ones declared with var, but not ones declared with let or const. So this code using var at global scope, on a browser, displays 42:

"use strict";
var a = 42; // Global variable called "a"
console.log(window.a); // Shows 42, because a is a property of the global object
Run code snippetEdit code snippet Hide Results Copy to answer Expand

But this code shows undefined for the properties, because let and const at global scope don't create properties on the global object:

"use strict";
let a = 42; // Global variable called "a"
console.log(a); // 42 (of course)
console.log(window.a); // undefined, there is no "a" property on the global object
const q = "Life, the Universe, and Everything"; // Global constant
console.log(q); // "Life, the Universe, and Everything" (of course)
console.log(window.q); // undefined, there is no "q" property on the global object
Run code snippetEdit code snippet Hide Results Copy to answer Expand


Final note: Much of the above also holds true if you compare the new ES2015 class (which provides a new, cleaner syntax for creating constructor functions and the prototype objects associated with them) with function declarations (as opposed to function expressions):

  • class declarations have block scope. In contrast, using a function declaration within a flow-control block is invalid. (It should be a syntax error; instead, different JavaScript engines handle it differently. Some relocate it outside the flow-control block, others act as though you'd used a function expression instead.)
  • class declarations aren't hoisted; function declarations are.
  • Using the same name with two class declarations in the same scope is a syntax error; with a function declaration, the second one wins, overwriting the first.
  • class declarations at global scope don't create properties of the global object; function declarations do.

Just as a reminder, this is a function declaration:

function Foo() {
}

These are both function expressions (anonymous ones):

var Foo = function() {
};
doSomething(function() { /* ... */ });

These are both function expressions (named ones):

var Foo = function Foo() {
};
doSomething(function Foo() { /* ... */ });
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ gblog โ€บ javascript-es2015-block-scoping
JavaScript ES2015: Block Scoping - GeeksforGeeks
July 23, 2025 - In this article, we will see what is Block scoping in Javascript, access to the block-scoped variables, how it is distinct from other kinds of variable's scope, through the examples. Prior to ES2015, JavaScript supported only function-level scoping unlike other languages like C++/Java which has block-level scoping.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 50365194 โ€บ what-is-a-real-world-example-of-block-scope-being-useful-in-javascript
What is a real-world example of block scope being useful in JavaScript? - Stack Overflow
let and const have introduced block-level scoping to JavaScript, and I now understand the difference. But I'm yet to find a compelling real-world example where let has an advantage over var. What ...
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ introduction-to-javascript โ€บ modules โ€บ learn-javascript-scope โ€บ cheatsheet
Learn JavaScript: Scope Cheatsheet | Codecademy
const and let are block scoped variables, meaning they are only accessible in their block or nested blocks. In the given code block, trying to print the statusMessage using the console.log() method will result in a ReferenceError.
๐ŸŒ
Mergesociety
mergesociety.com โ€บ javascript โ€บ scope
JavaScript Scope: Understanding Global, Local, and Block Scopes
Learn about JavaScript scopes including global, local, and block scopes. Understand variable accessibility and best practices for writing efficient, bug-free code.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ what-are-block-scoped-variables-and-functions-in-es6
What are Block Scoped variables and functions in ES6 ? - GeeksforGeeks
March 10, 2022 - Block-scoped variables and functions are defined inside a particular block or inside curly { } braces and they can only be accessible inside that particular block or within that block.
๐ŸŒ
GreatFrontEnd
greatfrontend.com โ€บ questions โ€บ quiz โ€บ explain-the-concept-of-scope-in-javascript
Explain the concept of scope in JavaScript | Quiz Interview Questions with Solutions
September 5, 2021 - Function scope means the variable is accessible only within the function it is declared. Block scope, introduced with ES6, means the variable is accessible only within the block (e.g., within curly braces {}) it is declared.
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Closures
Closures - JavaScript | MDN
However, because blocks don't create scopes for var, the var statements here actually create a global variable. There is also a practical example introduced below that illustrates how this can cause actual bugs when combined with closures. In ES6, JavaScript introduced the let and const declarations, which, among other things like temporal dead zones, allow you to create block-scoped variables.