You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
+
```
2407
2441
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`.
2409
2443
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**.
2411
2445
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).
0 commit comments