Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(q76): 馃摑 the answer and its explanation (Fully detailed馃挴) #698

Merged
merged 3 commits into from Jan 22, 2023

Conversation

SeyyedKhandon
Copy link
Contributor

@SeyyedKhandon SeyyedKhandon commented Dec 7, 2022

Fixed the question/answer and also covered the old question.

76. What's the output?
const { firstName: myName } = { firstName: 'Lydia' };

console.log(firstName);
  • A: "Lydia"
  • B: "myName"
  • C: undefined
  • D: ReferenceError
Answer

Answer: D

By using destructuring assignment syntax we can unpack values from arrays, or properties from objects, into distinct variables:

const { firstName } = { firstName: 'Lydia' };
// ES5 version:
// var firstName = { firstName: 'Lydia' }.firstName;

console.log(firstName); // "Lydia"

Also, a property can be unpacked from an object and assigned to a variable with a different name than the object property:

const { firstName: myName } = { firstName: 'Lydia' };
// ES5 version:
// var myName = { firstName: 'Lydia' }.firstName;

console.log(myName); // "Lydia"
console.log(firstName); // Uncaught ReferenceError: firstName is not defined

Therefore, firstName does not exist as a variable, thus attempting to access its value will raise a ReferenceError.

Note: Be aware of the global scope properties:

const { name: myName } = { name: 'Lydia' };

console.log(myName); // "lydia"
console.log(name); // "" ---- Browsers e.g. Chrome
console.log(name); // ReferenceError: name is not defined  ----- NodeJS

Whenever Javascript is unable to find a variable within the current scope, it climbs up the Scope chain 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.

  • 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 value which is equal to an empty string.

  • In NodeJS, there is no such property on the global object, thus attempting to access a non-existent variable will raise a ReferenceError.

Also covered the old question.
Update the explanation for Nodejs
Add some references and update what is actually happening in the browsers and nodejs enviroment.
@wodin wodin mentioned this pull request Dec 13, 2022
@SeyyedKhandon SeyyedKhandon changed the title fix(q76): 馃摑 the answer and its explanation fix(q76): 馃摑 the answer and its explanation (Full detailed) Jan 3, 2023
@SeyyedKhandon SeyyedKhandon changed the title fix(q76): 馃摑 the answer and its explanation (Full detailed) fix(q76): 馃摑 the answer and its explanation (Fully detailed) Jan 3, 2023
@SeyyedKhandon SeyyedKhandon changed the title fix(q76): 馃摑 the answer and its explanation (Fully detailed) fix(q76): 馃摑 the answer and its explanation (Fully detailed馃挴) Jan 3, 2023
Copy link

@ZahraMirzaei ZahraMirzaei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more clear with a great explanation.

@jakeherp jakeherp merged commit 0c8e98e into lydiahallie:master Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants