From 126ce0dc80c34741aba1688d74712de0177837b1 Mon Sep 17 00:00:00 2001 From: Suvadeep Date: Fri, 23 Aug 2024 18:10:30 +0530 Subject: [PATCH 1/3] Corrected Q36 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 96a2564..7d58cd8 100644 --- a/README.md +++ b/README.md @@ -639,7 +639,7 @@ console.log(name); From 4b327f6af7c9fc4ae4b5eb34d51af954a7e2c488 Mon Sep 17 00:00:00 2001 From: Suvadeep Date: Fri, 23 Aug 2024 18:18:30 +0530 Subject: [PATCH 2/3] typo fixed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d58cd8..4be5828 100644 --- a/README.md +++ b/README.md @@ -639,7 +639,7 @@ console.log(name); From fb9ecbf826ada1f3e5b66c85bef356c5ec37e53a Mon Sep 17 00:00:00 2001 From: Suvadeep Date: Fri, 23 Aug 2024 18:25:44 +0530 Subject: [PATCH 3/3] typos fixed --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4be5828..d1e95b4 100644 --- a/README.md +++ b/README.md @@ -635,12 +635,12 @@ console.log(fullname); console.log(name); ```
- View Answer -
    -
  • Output : Surbhi Dighe, ReferenceError: name is not defined
  • -
  • Reason for console.log(fullname) : The name property from user is assigned to a local variable fullname.
  • -
  • Reason for console.log(name) :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 scopw aka Global scope and still doesnt find it it will throw RefereneError. In browsers such as chrome , name is a deprectaed global scope property .In this example , the code is running inside global scope and there is no user-defined variable for 'name' therefore it searches the predifined variables/properties in the global scope which is in the case of browsers , it searches 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-existant varible will raise a ReferenceError
  • -
+ View Answer +
    +
  • Output: Surbhi Dighe, ReferenceError: name is not defined
  • +
  • Reason for console.log(fullname): 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".
  • +
  • Reason for console.log(name): 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`.
  • +
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**