0% found this document useful (0 votes)
2 views2 pages

Js Output Questions

The document presents a series of JavaScript output-based questions focusing on concepts such as scope, hoisting, and undefined variables. Each question is followed by an answer and a brief explanation of the reasoning behind the output. Key concepts include the differences between 'var' and 'let', the temporal dead zone, and the behavior of variables in strict mode.

Uploaded by

kksharmaz786
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Js Output Questions

The document presents a series of JavaScript output-based questions focusing on concepts such as scope, hoisting, and undefined variables. Each question is followed by an answer and a brief explanation of the reasoning behind the output. Key concepts include the differences between 'var' and 'let', the temporal dead zone, and the behavior of variables in strict mode.

Uploaded by

kksharmaz786
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JavaScript Output-Based Questions

(Scope, Hoisting, Undefined)


1. var a = 10; function test(){ [Link](a); var a = 20;} test();
Answer: undefined

Reason: 'var a' is hoisted inside function, shadowing outer 'a'. It is initialized as undefined.

2. let a = 10; function test(){ [Link](a); let a = 20;} test();


Answer: ReferenceError

Reason: 'let' is in temporal dead zone until initialized.

3. [Link](a); var a = 5;
Answer: undefined

Reason: 'var' is hoisted and initialized with undefined.

4. [Link](a); let a = 5;
Answer: ReferenceError

Reason: Access before initialization (TDZ).

5. function test(){ [Link](a); a = 10;} test();


Answer: ReferenceError

Reason: 'a' is not declared anywhere.

6. var a = 1; function outer(){ var a = 2; function inner(){ [Link](a);}


inner();} outer();
Answer: 2

Reason: Lexical scope → inner uses nearest 'a'.

7. let a = 1; function outer(){ let a = 2; function inner(){ [Link](a);} inner();}


outer();
Answer: 2

Reason: Block scoped 'let' follows lexical scope.

8. var a = 1; function test(){ [Link](a); var a = 2; [Link](a);} test();


Answer: undefined, 2
Reason: Local 'a' is hoisted, first undefined then assigned 2.

9. let a = 1; function test(){ [Link](a); let a = 2;} test();


Answer: ReferenceError

Reason: TDZ for 'let a'.

10. var a = 1; function test(){ a = 2;} test(); [Link](a);


Answer: 2

Reason: Global 'a' is modified.

11. function test(){ a = 10;} test(); [Link](a);


Answer: 10

Reason: Implicit global variable created (non-strict mode).

12. 'use strict'; function test(){ a = 10;} test();


Answer: ReferenceError

Reason: Strict mode prevents implicit globals.

13. [Link](typeof a);


Answer: 'undefined'

Reason: 'typeof' doesn't throw error for undeclared variables.

14. [Link](b);
Answer: ReferenceError

Reason: Direct access to undeclared variable throws error.

15. let a; [Link](a);


Answer: undefined

Reason: Declared but not assigned.

16. let a = undefined; [Link](a);


Answer: undefined

Reason: Explicitly assigned undefined.

You might also like