DEV Community

Cover image for Understanding JavaScript Blocks and Scope
bhavesh jadhav
bhavesh jadhav

Posted on • Originally published at bhaveshjadhav.hashnode.dev

Understanding JavaScript Blocks and Scope

Blocked in JavaScript

A block, or compound statement, in JavaScript is defined by the curly braces {}. It is used to group multiple statements together. For example:

{
    let a = 10;
    console.log(a);
}
Enter fullscreen mode Exit fullscreen mode

Why Do We Need to Group Statements?

We group multiple statements together in a block so that we can use them where JavaScript expects a single statement. For example:

if (true) console.log("Hello");
Enter fullscreen mode Exit fullscreen mode

This is valid JavaScript syntax, as JavaScript expects a single statement after the if condition. But if we need to execute multiple statements when the if condition is true, we use a block:

if (true) {
    var a = 10;
    console.log(a);
}
Enter fullscreen mode Exit fullscreen mode

Block Scope

Block scope refers to the variables and functions that we can access inside the block. For example:

{
    var a = 10;
    let b = 20;
    const c = 30;
}
console.log(a); // Accessible
console.log(b); // Not accessible
console.log(c); // Not accessible
Enter fullscreen mode Exit fullscreen mode

Here, var a is accessible outside the block because var has a global or function scope. However, let and const are not accessible outside the block because they have block scope.

What is Shadowing in JavaScript?

Shadowing occurs when a variable declared within a certain scope (e.g., inside a block) has the same name as a variable declared in an outer scope. The inner variable shadows the outer variable:

var a = 100;
{
    var a = 10; // This shadows the variable outside the block
    let b = 20;
    const c = 30;
    console.log(a); // Prints 10
}
console.log(a); // Prints 10, the value is modified by the inner var
Enter fullscreen mode Exit fullscreen mode

In the case of let and const, shadowing behaves differently because they have block scope:

let b = 100;
{
    var a = 10;
    let b = 20; // This shadows the outer let variable
    const c = 30;
    console.log(b); // Prints 20
}
console.log(b); // Prints 100, because let has block scope
Enter fullscreen mode Exit fullscreen mode

Illegal Shadowing

Illegal shadowing occurs when a let variable is shadowed by a var variable inside the same block:

let a = 20;
{
    var a = 20; // Illegal shadowing, causes an error
}
Enter fullscreen mode Exit fullscreen mode

However, the reverse is allowed:

var a = 20;
{
    let a = 30; // This is allowed
}
Enter fullscreen mode Exit fullscreen mode

Reason for Reverse Allowance

The reason the reverse is allowed (var outside and let inside) is due to the scoping rules of JavaScript. var has function or global scope, meaning it can be accessed throughout the entire function or globally. let and const, however, have block scope, meaning they are only accessible within the block they are defined in. Therefore, defining a let or const inside a block does not interfere with the var outside the block, allowing the code to be valid.

Shadowing in Function Scope

Shadowing also applies to function scopes. However, since var has function scope, it behaves differently:

let a = 20;
function x() {
    var a = 20; // This is allowed
}
Enter fullscreen mode Exit fullscreen mode

In this case, the var declaration inside the function does not affect the let variable outside the function.

Conclusion

Understanding blocks, block scope, and variable shadowing is crucial for writing efficient and bug-free JavaScript code. Properly managing scope and avoiding illegal shadowing can help prevent unexpected behaviors and maintain code clarity.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.