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
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Statements โ€บ block
Block statement - JavaScript | MDN - Mozilla
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.
๐ŸŒ
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.
Discussions

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
Block scope, function scope and local scope in Javascript - Stack Overflow
Is block scope sometimes the same as function scope? I know function scope is for everything inside a function, but don't get what exactly a block scope is. For Javascript, is it currently recommen... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - what is block scope function ECMAScript 6 compare with ECMAScript 5 - Stack Overflow
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.) var variables exist throughout the function they're declared in (or globally, if declared globally), they aren't confined to the block ... 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
October 5, 2024
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Glossary โ€บ Scope
Scope - Glossary | MDN - Mozilla
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).
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-function-and-block-scope-in-javascript
What is function and block scope in JavaScript?
When we run the above code, we ... declared in. The block scope of a variable means that the variable is accessible within the block that is between the curly braces....
๐ŸŒ
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 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ gblog โ€บ javascript-es2015-block-scoping
JavaScript ES2015: Block Scoping - GeeksforGeeks
July 23, 2025 - With ES2015, in addition to function-level scoping, JavaScript also supports block-level scoping with the help of the let keyword & const keyword. But before we get into details of ES2015 stuff, letโ€™s discuss what we exactly mean by phrases โ€œfunction-level scopeโ€ and โ€œblock-level scopeโ€.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Medium
josephcardillo.medium.com โ€บ the-difference-between-function-and-block-scope-in-javascript-4296b2322abe
The Difference Between Function and Block Scope in JavaScript | by Joe Cardillo | Medium
January 31, 2022 - It means they are only available inside the function theyโ€™re created in, or if not created inside a function, they are โ€˜globally scoped.โ€™
๐ŸŒ
DEV Community
dev.to โ€บ gaurbprajapati โ€บ block-scope-and-shadowing-in-javascript-28o2
block scope and shadowing in Javascript - DEV Community
July 12, 2023 - In summary, block scope allows variables to be limited to specific blocks of code, improving encapsulation and preventing unintended access. Shadowing occurs when a variable in an inner scope has the same name as a variable in an outer scope, ...
Top answer
1 of 3
7
  1. javascript 5 does not use blocked scope it uses chained scope. the main difference is that you cannot access the variable out side of the scope unless you make it global. ES 6 will have blocked scope when you declare a variable with let.
  2. currently recommended to use var because ES 6 is not fully supported.
2 of 3
5

I'm not sure you really got your questions answered yet:

Is block scope sometimes the same as function scope? I know function scope is for everything inside a function, but don't get what exactly a block scope is.

Yes, a block scope is sometimes the same as a function scope. Block scope is everything inside a set of braces { a block scope here }. So, at the top of a function's code, a block scope will be the same as a function scope:

function test(x) {
   // this is both a block scope and a function scope
   let y = 5;
   if (x) {
       // this is a smaller block scope that is not the same as the function scope
       let z = 1;
   }
}

For Javascript, is it currently recommended to use let / const instead of var for future maintenance? (This was from Airbnb Style Guide)

let and const are part of the newest ES6 specification and are only implemented in the latest Javascript engines and sometimes in the latest engines they are only enabled with special flags. They are coming to all newer JS engines/browsers, but are not widely deployed yet. Thus, if you are writing Javascript for regular browser consumption across the broad internet, you cannot reliably use let and const yet.

There are some cases where you can safely program with let and const now:

  1. If you are targeting only a specific Javascript engine and you know that it has support for those features (such as a specific version of nodejs or a plug-in only for a specific version of a specific browser).

  2. If you are using a transpiler that will convert your code to code that will run in all browsers. When using a transpiler, you can write your code using the latest features and the transpiler will "dumb it down" so that your code will work in older browsers by using simulations of the newer features.

If you are programming for an environment where you know that let and const are supported, then it is advisable to use them as appropriate. If you declare a variable at the top of your function, then let and var will do the same thing.

If you declare a variable in a smaller scope within the function, then let will be contained within the smaller scope, but var will be hoisted to the top of the function and will have function scope, no matter where it is declared.


The AirBnb Style Guide you linked to is specifically written for an ES6 environment (note there is a separate link for an ES5 version of their style guide). So, that means that they are assuming an ES6 capable environment. That's either because they are targeting a server-side JS engine that they know supports ES6 or because they are using a transpiler that will convert ES6 code into something that will run on an ES5 engine.


A note about transpilers. Before using a transpiler and switching all variable declarations to let within block scopes, it is worth understanding what kind of code the transpiler generates and whether the extra code it generates has any effect on the performance of your application. For example, block scope for let is simulated by creating an inline IIFE which can lead to extra run-time overhead for every block that contains a let statement. I'm not saying this is necessarily a bad thing that should keep you from using a transpiler, but when deciding whether to use a transpiler, I'd suggest that you thoroughly familiarize yourself with what the transpiled code looks like for a variety of ES6 features so you know whether it is the right tool for any job you have or only for some jobs.

๐ŸŒ
Medium
medium.com โ€บ @swati.developer17 โ€บ part-10-block-scope-function-scope-and-shadowing-in-js-eb0043b93588
Part 10 : Block Scope, Function Scope and Shadowing in JS. | by Swati Redhu | Medium
June 18, 2024 - Part 10 : Block Scope, Function Scope and Shadowing in JS. What is Block ? Block is defined by curly braces i.e { .. } Block is also know as Compound Statement. Why Block is required in JS? Block is โ€ฆ
๐ŸŒ
Medium
medium.com โ€บ nerd-for-tech โ€บ function-scope-block-scope-in-js-d29c8e7cd216
JavaScript | ES6 | Function Scope & Block Scope | Nerd For Tech
April 10, 2021 - Function Scope: When a variable ... Block Scope: A variable when declared inside the if or switch conditions or inside for or while loops, are accessible within that particular condition or loop....
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() { /* ... */ });
๐ŸŒ
Medium
medium.com โ€บ edge-coders โ€บ function-scopes-and-block-scopes-in-javascript-25bbd7f293d7
Function scopes and block scopes in JavaScript | by Samer Buna | AZ.dev | Medium
February 1, 2022 - Function scopes and block scopes in JavaScript Is the following line a valid line of JavaScript code? {{{}}} Yes it is. These are nested block scopes. This article is based on an interactive lab I โ€ฆ
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 61980865 โ€บ how-does-block-scope-in-javascript-work-under-the-hood
How does block-scope in JavaScript work under the hood? - Stack Overflow
March 10, 2022 - But since a block isn't a function (and I imagine doesn't create a new execution context), how does the block have an awareness as to what's defined inside of it? Is it some sort of IIFE like functionality happening? ... The engine simply opens a new scope for a โ€œblockโ€. When the โ€œblockโ€ ends, so does the scope.
๐ŸŒ
Coolhead
blog.coolhead.in โ€บ difference-between-function-scope-and-block-scope-in-javascript
Difference between function scope and block scope in ...
July 12, 2023 - Function Scope: When a variable ... function. Block Scope: A variable when declared inside the if or switch conditions or inside for or while loops, are accessible within that particular condition ......
๐ŸŒ
daily.dev
daily.dev โ€บ home โ€บ blog โ€บ get into tech โ€บ concepts in javascript: understanding scope
Concepts in JavaScript: Understanding Scope
February 4, 2025 - Try not to use too many closures because they can hang onto memory you don't need anymore. In JavaScript, scope is all about where you can use your variables, functions, and objects.