fix(q76): 馃摑 the answer and its explanation (Fully detailed馃挴) #698
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixed the question/answer and also covered the old question.
76. What's the output?
"Lydia"
"myName"
undefined
ReferenceError
Answer
Answer: D
By using destructuring assignment syntax we can unpack values from arrays, or properties from objects, into distinct variables:
Also, a property can be unpacked from an object and assigned to a variable with a different name than the object property:
Therefore,
firstName
does not exist as a variable, thus attempting to access its value will raise aReferenceError
.Note: Be aware of the
global scope
properties: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 forname
, therefore it searches the predefined variables/properties in the global scope which is in case of browsers, it searches throughwindow
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.