Lädt...


🔧 Understanding JavaScript Blocks and Scope


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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);
}

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");

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);
}

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

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

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

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
}

However, the reverse is allowed:

var a = 20;
{
    let a = 30; // This is allowed
}

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
}

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.

...

🔧 Understanding Scope and Scope Chain in JavaScript.


📈 47.48 Punkte
🔧 Programmierung

🔧 Understanding the Scope Chain, Scope, and Lexical Environment in JavaScript


📈 47.48 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Blocks and Scope


📈 43.24 Punkte
🔧 Programmierung

🔧 Scope in JavaScript – Global vs Local vs Block Scope Explained


📈 37.68 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Scope: Global and Local


📈 31.92 Punkte
🔧 Programmierung

🔧 Understanding Lexical Scope and Closure in JavaScript


📈 31.92 Punkte
🔧 Programmierung

🔧 [JS] The top-level scope is NOT always the global scope


📈 31.12 Punkte
🔧 Programmierung

🔧 Understanding the Scope Chain in JavaScript🚀


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding Lexical Scope in JavaScript ✅


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding Closures in JavaScript: A Powerful Mechanism for Variable Scope


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding Lexical Scope in JavaScript ✅


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding Variable Scope in JavaScript 🌐


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding Scope Chain in JavaScript


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding Nested Scope in Javascript: The Onion Concept as a Metaphor


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding scope in JavaScript


📈 30.26 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Objects: The Building Blocks of Your Code


📈 26.01 Punkte
🔧 Programmierung

🪟 WhatsApp tests new text formats: Code blocks, quote blocks and lists


📈 24.29 Punkte
🪟 Windows Tipps

🔧 JavaScript Scope and Hoisting


📈 23.78 Punkte
🔧 Programmierung

🔧 Scope, Hoisting and Closures in Javascript


📈 23.78 Punkte
🔧 Programmierung

🔧 JavaScript variables and scope


📈 23.78 Punkte
🔧 Programmierung

🔧 Scopes And Scope Chain in JavaScript


📈 23.78 Punkte
🔧 Programmierung

🔧 Scope, Closures, and Hoisting in JavaScript – Explained with Code Examples


📈 23.78 Punkte
🔧 Programmierung

🔧 Hoisting, Lexical Scope, and Temporal Dead Zone in JavaScript


📈 23.78 Punkte
🔧 Programmierung

🔧 Day 13: Deep Dive into Object Properties, Getters & Setters, and Lexical Scope in JavaScript 🎉


📈 23.78 Punkte
🔧 Programmierung

🔧 Mastering Variables and Scope in JavaScript: A Developer's Guide


📈 23.78 Punkte
🔧 Programmierung

📰 Facebook Exec Admits 'No Real Understanding' for the Scope of Fake News


📈 23.7 Punkte
📰 IT Security Nachrichten

🔧 Understanding Kotlin's Scope Functions


📈 23.7 Punkte
🔧 Programmierung

🔧 Node.js vs. Browser: Understanding the Global Scope Battle


📈 23.7 Punkte
🔧 Programmierung

🔧 Understanding the Scope of Pattern Variables in Java


📈 23.7 Punkte
🔧 Programmierung

🔧 From Toy Blocks to Code Blocks: My Journey into Tech


📈 22.62 Punkte
🔧 Programmierung

matomo