|
1291 | 1291 |
|
1292 | 1292 | 22. ### What is the Temporal Dead Zone
|
1293 | 1293 |
|
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 | + |
1298 | 1300 | ```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; |
1304 | 1307 | }
|
1305 | 1308 | ```
|
1306 | 1309 |
|
@@ -9291,19 +9294,38 @@ requestAnimationFrame(animate);
|
9291 | 9294 | **[⬆ Back to Top](#table-of-contents)**
|
9292 | 9295 |
|
9293 | 9296 | 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. |
9295 | 9297 |
|
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**. |
9299 | 9299 |
|
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. |
9301 | 9328 |
|
9302 |
| -```javascript |
9303 |
| -const text = "Mozilla"; |
9304 |
| -console.log(text.substring(2, 5)); // "zil" |
9305 |
| -console.log(text.substr(2, 3)); // "zil" |
9306 |
| -``` |
9307 | 9329 |
|
9308 | 9330 | **[⬆ Back to Top](#table-of-contents)**
|
9309 | 9331 |
|
|
0 commit comments