Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

Block Scope in JavaScript

Block scope is an important concept in JavaScript. It means that a variable can be accessed only inside the block where it is declared. A block is usually written using curly braces { } . Blocks can be found in if statements, loops, functions, and other parts of JavaScript code. In JavaScript, let and const are block-scoped variables. For example: { let name = " Abishek " ; console . log ( name…

Block scope refers to the limited region of code where a variable can be accessed. This concept plays a crucial role in JavaScript. Within a block, typically enclosed by curly braces {}, a variable exists solely within those boundaries. Blocks appear in various structures such as if statements, loops, functions, and more.

In JavaScript, the keywords let and const introduce block-scoped variables. To illustrate, consider the following example: { let name = 'Abishek'; console.log(name); }. When executed, the output will be 'Abishek'. This demonstrates that name is only available within the block where it was declared. Should we attempt to reference name outside this block, JavaScript will raise an error, as the variable is no longer accessible.

The same principle applies to const. For instance: { const age = 22; console.log(age); }. Here, the output will be '22'. However, age can only be accessed inside the if block. If we try to log age outside the block, JavaScript will once again throw a ReferenceError: age is not defined.

In contrast, var exhibits different behavior. It is function-scoped, not block-scoped. Consider this example: if (true) { var city = 'Chennai'; } console.log(city);. The output will indeed be 'Chennai'. This occurs because var retains its value throughout the entire function, allowing it to be accessed outside its original block.

Block scope is beneficial as it avoids accidental misuse or modification of variables outside their intended area. It enhances code readability and maintainability. Ultimately, remember this key distinction: let and const possess block scope, whereas var has function scope. In contemporary JavaScript, let and const are typically favored over var.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Has your license plate been tracked by Flock? Here's how to check.

The website HaveIBeenFlocked has compiled a database of 4.6 million license plate records that have appeared in roughly 242 million Flock searches.

  • HaveIBeenFlocked.com lets public check if license plate searched in Flock's system.
  • Website uses data from audit logs of Flock's 242 million searches, not storing user info.
  • Flock's cameras track license plates for law enforcement, but system has misuse allegations.

Your Database Is Making 4 Promises. Here's What ACID Means.

Introduction Your program keeps opening transactions. A signup writes a new user row. A checkout debits one account and credits another. A form submission updates three related tables at once.

  • Atomicity ensures all parts of a transaction happen together as one unit
  • If any part fails, the entire transaction is rolled back to maintain consistency
  • ACID guarantees Atomicity, Consistency, Isolation, and Durability for transactions

Understanding chmod Without Memorizing Numbers

How Linux file permissions actually work under the hood, why symbolic mode is your best friend, and how to stop blindly typing chmod 777. Every Linux engineer has been there.

  • Symbolic mode simplifies chmod by using letters instead of numbers.
  • File mode string in ls -l shows read/write/execute permissions.
  • Chmod u+x adds execute permission for user without changing others.

More from Monday 17 August →