Skip to content

Commit 0c8e98e

Browse files
authored
Merge pull request lydiahallie#698 from SeyyedKhandon/master
fix(q76): 📝 the answer and its explanation (Fully detailed💯)
2 parents 2c91d1a + c1df5c4 commit 0c8e98e

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,9 +2390,9 @@ Since `shape` is frozen, and since the value of `x` is not an object, we cannot
23902390
###### 76. What's the output?
23912391

23922392
```javascript
2393-
const { name: myName } = { name: 'Lydia' };
2393+
const { firstName: myName } = { firstName: 'Lydia' };
23942394

2395-
console.log(name);
2395+
console.log(firstName);
23962396
```
23972397

23982398
- A: `"Lydia"`
@@ -2403,13 +2403,47 @@ console.log(name);
24032403
<details><summary><b>Answer</b></summary>
24042404
<p>
24052405

2406-
#### Answer: C
2406+
#### Answer: D
2407+
2408+
By using [destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) syntax we can unpack values from arrays, or properties from objects, into distinct variables:
2409+
2410+
```javascript
2411+
const { firstName } = { firstName: 'Lydia' };
2412+
// ES5 version:
2413+
// var firstName = { firstName: 'Lydia' }.firstName;
2414+
2415+
console.log(firstName); // "Lydia"
2416+
```
2417+
2418+
Also, a property can be unpacked from an object and assigned to a variable with a different name than the object property:
2419+
2420+
```javascript
2421+
const { firstName: myName } = { firstName: 'Lydia' };
2422+
// ES5 version:
2423+
// var myName = { firstName: 'Lydia' }.firstName;
2424+
2425+
console.log(myName); // "Lydia"
2426+
console.log(firstName); // Uncaught ReferenceError: firstName is not defined
2427+
```
2428+
2429+
Therefore, `firstName` does not exist as a variable, thus attempting to access its value will raise a `ReferenceError`.
2430+
2431+
**Note:** Be aware of the `global scope` properties:
2432+
2433+
```javascript
2434+
const { name: myName } = { name: 'Lydia' };
2435+
2436+
console.log(myName); // "lydia"
2437+
console.log(name); // "" ----- Browser e.g. Chrome
2438+
console.log(name); // ReferenceError: name is not defined ----- NodeJS
2439+
2440+
```
24072441

2408-
When we unpack the property `name` from the object on the right-hand side, we assign its value `"Lydia"` to a variable with the name `myName`.
2442+
Whenever Javascript is unable to find a variable within the _current scope_, it climbs up the [Scope chain](https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch3.md) and searches for it and if it reaches the top-level scope, aka **Global scope**, and still doesn't find it, it will throw a `ReferenceError`.
24092443

2410-
With `{ name: myName }`, we tell JavaScript that we want to create a new variable called `myName` with the value of the `name` property on the right-hand side.
2444+
- In **Browsers** such as _Chrome_, `name` is a _deprecated global scope property_. In this example, the code is running inside _global scope_ and there is no user defined local variable for `name`, therefore it searches the predefined _variables/properties_ in the global scope which is in case of browsers, it searches through `window` object and it will extract the [window.name](https://developer.mozilla.org/en-US/docs/Web/API/Window/name) value which is equal to an **empty string**.
24112445

2412-
Since we try to log `name`, a variable that is not defined, `undefined` is returned on the left side assignment. Later, the value of `Lydia` is stored through the destructuring assignment.
2446+
- In **NodeJS**, there is no such property on the `global` object, thus attempting to access a non-existent variable will raise a [ReferenceError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined).
24132447

24142448
</p>
24152449
</details>

0 commit comments

Comments
 (0)