What is hoisting in JavaScript?
Hoisting is a mechanism where variable and function declarations are conceptually moved to the top of their scope before execution. Only declarations are hoisted, not initializations.
How are var variables hoisted?
Hoisted to the top and initialized with undefined. Accessible before declaration but value is undefined.
How are let and const variables hoisted?
They are hoisted but not initialized. They exist in the Temporal Dead Zone (TDZ) until declaration. Access before declaration throws ReferenceError.
What is the Temporal Dead Zone (TDZ)?
The period between entering a scope and the declaration of let/const. Accessing variables in TDZ causes ReferenceError.
How are function expressions hoisted?
Depends on assignment. If var: variable is hoisted with undefined (calling before assignment causes TypeError). If let/const: subject to TDZ (ReferenceError).
What is a common pitfall with var and hoisting?
Access before assignment results in undefined, leading to unexpected behavior.
Benefits of let and const over var in relation to hoisting?
TDZ enforces declaring before using, promoting cleaner code and reducing bugs. They also support block scoping.
How does hoisting affect code readability?
Makes code harder to understand. Best practice: declare variables at the top for clarity.
Best practice for function declarations and hoisting?
Even though hoisted, define functions before calling for clarity and organization.
How does hoisting interact with block scope?
var is function-scoped, ignoring block scope. let and const are block-scoped, important for hoisting and TDZ.
How does Strict Mode prevent accidental globals?
Assigning to undeclared variables throws ReferenceError instead of creating globals.
Common errors in Strict Mode?
ReferenceError (undeclared vars), TypeError (illegal assignments), SyntaxError (duplicate parameters).
Examples of good and bad variable names?
Good: getUserById, totalPrice, isValid.
Bad: getU, tp, isV.
Use camelCase, PascalCase for classes, UPPER_SNAKE_CASE for constants.
How do const and let improve code over var?
const: prevents reassignment.
let: block scoped.
var: function scoped, can cause unexpected behavior.
Two restrictions in Strict Mode?
Disallows with statement, deleting undeletable properties.
What is a ReferenceError?
Occurs when accessing undeclared/unavailable variable at runtime.
What is an Execution Context?
Environment for executing JS code: contains variables, functions, this.
What is the Function Execution Context?
Created each function call with its variables, arguments, this.
Heap vs Stack in JS memory?
Heap: dynamic data (objects, arrays).
Stack: static data (function calls, vars).
Explain mutability in JS.
Primitives immutable. Reference types mutable. const prevents reassignment but not mutation.
How to prevent accidental globals?
Always declare with var/let/const. Use ‘use strict’.
Where are objects/arrays stored?
In the heap, with variables holding references.
How to prevent accidental globals?
Declare with var/let/const. Use Strict Mode.