Skip to content

Commit 563001b

Browse files
committed
Improve block scoped variables usage
1 parent a22b516 commit 563001b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,43 +1247,46 @@
12471247

12481248
20. ### What is the reason to choose the name let as a keyword
12491249

1250-
`let` is a mathematical statement that was adopted by early programming languages like **Scheme** and **Basic**. It has been borrowed from dozens of other languages that use `let` already as a traditional keyword as close to `var` as possible.
1250+
The keyword `let` was chosen because it originates from mathematical notation, where "let" is used to introduce new variables (for example, "let x = 5"). This term was adopted by several early programming languages such as Scheme and BASIC, establishing a tradition in computer science. JavaScript follows this convention by using `let` to declare variables with block scope, providing a modern alternative to `var`. The choice helps make the language more familiar to programmers coming from other languages and aligns with the mathematical practice of variable assignment.
12511251

12521252
**[⬆ Back to Top](#table-of-contents)**
12531253

12541254
21. ### How do you redeclare variables in a switch block without an error
12551255

1256-
If you try to redeclare variables in a `switch block` then it will cause errors because there is only one block. For example, the below code block throws a syntax error as below,
1257-
1256+
When you try to redeclare variables using `let` or `const` in multiple `case` clauses of a `switch` statement, you will get a SyntaxError. This happens because, in JavaScript, all `case` clauses within a `switch` statement share the same block scope. For example:
1257+
12581258
```javascript
12591259
let counter = 1;
12601260
switch (x) {
12611261
case 0:
12621262
let name;
12631263
break;
1264-
12651264
case 1:
1266-
let name; // SyntaxError for redeclaration.
1265+
let name; // SyntaxError: Identifier 'name' has already been declared
12671266
break;
12681267
}
12691268
```
1270-
1271-
To avoid this error, you can create a nested block inside a case clause and create a new block scoped lexical environment.
1272-
1269+
1270+
To avoid this error, you can create a new block scope within each `case` clause by wrapping the code in curly braces `{}`. This way, each `let` or `const` declaration is scoped only to that block, and redeclaration errors are avoided:
1271+
12731272
```javascript
12741273
let counter = 1;
12751274
switch (x) {
12761275
case 0: {
12771276
let name;
1277+
// code for case 0
12781278
break;
12791279
}
12801280
case 1: {
1281-
let name; // No SyntaxError for redeclaration.
1281+
let name; // No SyntaxError
1282+
// code for case 1
12821283
break;
12831284
}
12841285
}
12851286
```
1286-
1287+
1288+
That means, to safely redeclare variables in different cases of a switch statement, wrap each case’s code in its own block using curly braces. This ensures each variable declaration is scoped to its specific case block.
1289+
12871290
**[⬆ Back to Top](#table-of-contents)**
12881291

12891292
22. ### What is the Temporal Dead Zone

0 commit comments

Comments
 (0)