Skip to content

Commit 26bafff

Browse files
committed
Modifications to temporal deadzone and substring questions
1 parent 563001b commit 26bafff

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

README.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,16 +1291,19 @@
12911291

12921292
22. ### What is the Temporal Dead Zone
12931293

1294-
The Temporal Dead Zone(TDZ) is a specific period or area of a block where a variable is inaccessible until it has been initialized with a value. This behavior in JavaScript that occurs when declaring a variable with the let and const keywords, but not with var. In ECMAScript 6, accessing a `let` or `const` variable before its declaration (within its scope) causes a ReferenceError.
1295-
1296-
Let's see this behavior with an example,
1297-
1294+
The **Temporal Dead Zone (TDZ)** refers to the period between the start of a block and the point where a variable declared with `let` or `const` is initialized. During this time, the variable exists in scope but **cannot be accessed**, and attempting to do so results in a `ReferenceError`.
1295+
1296+
This behavior is part of **JavaScript's ES6 (ECMAScript 2015)** specification and applies **only to variables declared with `let` and `const`**, not `var`. Variables declared with `var` are **hoisted** and initialized with `undefined`, so accessing them before the declaration does not throw an error, though it can lead to unexpected results.
1297+
1298+
### Example
1299+
12981300
```javascript
1299-
function somemethod() {
1300-
console.log(counter1); // undefined
1301-
console.log(counter2); // ReferenceError
1302-
var counter1 = 1;
1303-
let counter2 = 2;
1301+
function someMethod() {
1302+
console.log(counter1); // Output: undefined (due to var hoisting)
1303+
console.log(counter2); // Throws ReferenceError (TDZ for let)
1304+
1305+
var counter1 = 1;
1306+
let counter2 = 2;
13041307
}
13051308
```
13061309

@@ -9291,19 +9294,38 @@ requestAnimationFrame(animate);
92919294
**[⬆ Back to Top](#table-of-contents)**
92929295
92939296
470. ### What is the difference between substring and substr methods?
9294-
There are subtle differences between the substring() and substr() methods, so you should be careful not to get them confused.
92959297
9296-
- The two parameters of substr() are start and length, while for substring(), they are start and end.
9297-
- substr()'s start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0.
9298-
- Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start.
9298+
Both `substring` and `substr` are used to extract parts of a string, but there are subtle differences between the substring() and substr() methods in terms of **syntax** and **behavior**.
92999299
9300-
Furthermore, substr() is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible.
9300+
1. `substring(start, end)`
9301+
- **Parameters:**
9302+
- `start`: The index to start extracting (inclusive).
9303+
- `end`: The index to stop extracting (exclusive).
9304+
- **Behavior:**
9305+
- If `start > end`, it **swaps the arguments**.
9306+
- Negative values are treated as `0`.
9307+
9308+
```javascript
9309+
let str = "Hello World";
9310+
console.log(str.substring(0, 5)); // "Hello"
9311+
console.log(str.substring(5, 0)); // "Hello" (swapped)
9312+
console.log(str.substring(-3, 4)); // "Hell" (negative = 0)
9313+
```
9314+
2. `substr(start, length)` _(Deprecated)_
9315+
9316+
- **Parameters:**
9317+
- `start`: The index to start extracting.
9318+
- `length`: The number of characters to extract.
9319+
- **Behavior:**
9320+
- If `start` is negative, it counts from the **end** of the string.
9321+
- If `length` is omitted, it extracts to the end of the string.
9322+
9323+
```javascript
9324+
let str = "Hello World"; console.log(str.substr(0, 5)); // "Hello"
9325+
console.log(str.substr(-5, 3)); // "Wor" (starts from 'W')`
9326+
```
9327+
**Note:** substr() is considered a legacy feature in ECMAScript, so it is best to avoid using it if possible.
93019328
9302-
```javascript
9303-
const text = "Mozilla";
9304-
console.log(text.substring(2, 5)); // "zil"
9305-
console.log(text.substr(2, 3)); // "zil"
9306-
```
93079329
93089330
**[⬆ Back to Top](#table-of-contents)**
93099331

0 commit comments

Comments
 (0)