Skip to content

Corrected Q36 #3

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,12 @@ console.log(fullname);
console.log(name);
```
<details>
<summary><b>View Answer</b></summary>
<ul>
<li><b>Output</b> : Surbhi Dighe, ReferenceError: name is not defined</li>
<li><b>Reason for console.log(fullname)</b> : The name property from user is assigned to a local variable fullname.</li>
<li><b>Reason for console.log(name)</b> : It gives an error because name was assigned to a local variable fullname and therefore name is not directly accessible.</li>
</ul>
<summary><b>View Answer</b></summary>
<ul>
<li><b>Output</b>: Surbhi Dighe, ReferenceError: name is not defined</li>
<li><b>Reason for console.log(fullname)</b>: The `fullname` variable is assigned the value from `user.name`. This value is correctly output as "Surbhi Dighe" because `fullname` is defined and holds the value "Surbhi Dighe".</li>
<li><b>Reason for console.log(name)</b>: In JavaScript, if a variable is not found in the current scope, JavaScript looks up the scope chain. If it reaches the global scope and still doesn’t find the variable, it will throw a `ReferenceError`. In browsers like Chrome, `name` is not a global variable; instead, it might refer to `window.name`, which is a property on the `window` object that holds the name of the window (default is an empty string). Since `name` is not defined locally or globally in this case, a `ReferenceError` is thrown. In Node.js, there is no global `name` property, so attempting to access it would also result in a `ReferenceError`.</li>
</ul>
</details>

**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
Expand Down