LearnFrenzy
Sign in Take a free test
JS

JavaScript Programming · Topic 8 of 25

Scope and Hoisting Questions and Concepts

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Scope and Hoisting · Study material

Scope and Hoisting – Interview Questions

1. What is scope in JavaScript?

The region of code where a variable is accessible. JavaScript has global, module, function and block scope.

2. What is the scope chain?

The nested list of scopes searched from the current one outwards when a name is used. The first match is used; if none is found, a ReferenceError is thrown.

3. What is lexical scope?

Scope determined by where functions and blocks are written in the source code, not by where functions are called.

4. What is hoisting?

The effect of the creation phase: declarations are registered before code runs. Function declarations are usable early, var is undefined, and let/const/class are in the temporal dead zone.

5. Are classes hoisted?

Yes, but like let they stay uninitialised until the declaration runs, so using a class before its definition throws a ReferenceError.

6. What is an execution context?

The environment created for running global code or a function call: its variables (scope), its outer scope reference and its this value. It is created in a creation phase and then executed.

7. What wins when a function declaration and a var share a name?

During hoisting the function declaration wins; the var declaration is ignored, but a later assignment to the var replaces the function.

8. What is a closure?

A function together with the lexical scope in which it was created; it can use that scope's variables even after the outer function has returned.

9. What is the difference between scope and this?

Scope is lexical and fixed when the code is written. this is decided at call time (except in arrow functions, which take it from their lexical scope).

10. What is the temporal dead zone?

The time between entering a scope and evaluating a let/const/class declaration, during which the variable cannot be accessed.

11. Do ES modules create global variables?

No. Each module has its own top-level scope; only explicit assignments to globalThis create globals.

12. Can an outer function access variables of an inner function?

No. Lookup only goes outwards, from inner scopes to outer ones.

13. What is variable shadowing?

Declaring a variable in an inner scope with the same name as one in an outer scope; inside the inner scope, the outer one is hidden.

14. What is globalThis?

A standard reference to the global object in any environment: window in browsers, self in workers, global in Node.js.

15. Why can a function call a variable declared after the function, using let?

The function body runs only when it is called. If the call happens after the let line has run, the variable is initialised; if the call happens before, it throws.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About JavaScript Programming

Why it matters

  • JavaScript runs every web page and, with Node.js, servers, tools and APIs – it is the most used language in the world
  • Front-end, full-stack and MERN roles test JavaScript first, and many campus coding rounds allow it
  • "Predict the output" questions on hoisting, closures, this, coercion and the event loop are interview favourites
  • Modern ES6+ – let/const, arrow functions, destructuring, promises and async/await – is what companies expect from freshers

Who it is for

  • College students (B.Tech, BCA, MCA, B.Sc CS) learning web development and preparing for semester exams and viva
  • Freshers preparing for campus placements, online assessments and front-end interviews
  • Developers moving to React, Angular, Vue or Node.js who need strong core JavaScript
  • Anyone revising ES6 and asynchronous JavaScript before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs on concepts and ES6 features
  • "Predict the output" puzzles – hoisting, coercion, closures, promises and timers
  • "Find the error" debugging: SyntaxError, ReferenceError, TypeError and silent logic bugs
  • Coding problems with solutions and real output, every one run on Node.js – run any study example in your browser

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture, Cognizant tests
  • Front-end developer interviews
  • MERN / full-stack interviews
  • Online coding assessments
  • University practicals

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, debugging, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links