js_flashcards_hoisting

(23 cards)

1
Q

What is hoisting in JavaScript?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are var variables hoisted?

A

Hoisted to the top and initialized with undefined. Accessible before declaration but value is undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are let and const variables hoisted?

A

They are hoisted but not initialized. They exist in the Temporal Dead Zone (TDZ) until declaration. Access before declaration throws ReferenceError.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Temporal Dead Zone (TDZ)?

A

The period between entering a scope and the declaration of let/const. Accessing variables in TDZ causes ReferenceError.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are function expressions hoisted?

A

Depends on assignment. If var: variable is hoisted with undefined (calling before assignment causes TypeError). If let/const: subject to TDZ (ReferenceError).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a common pitfall with var and hoisting?

A

Access before assignment results in undefined, leading to unexpected behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Benefits of let and const over var in relation to hoisting?

A

TDZ enforces declaring before using, promoting cleaner code and reducing bugs. They also support block scoping.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does hoisting affect code readability?

A

Makes code harder to understand. Best practice: declare variables at the top for clarity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Best practice for function declarations and hoisting?

A

Even though hoisted, define functions before calling for clarity and organization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does hoisting interact with block scope?

A

var is function-scoped, ignoring block scope. let and const are block-scoped, important for hoisting and TDZ.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does Strict Mode prevent accidental globals?

A

Assigning to undeclared variables throws ReferenceError instead of creating globals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Common errors in Strict Mode?

A

ReferenceError (undeclared vars), TypeError (illegal assignments), SyntaxError (duplicate parameters).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Examples of good and bad variable names?

A

Good: getUserById, totalPrice, isValid.
Bad: getU, tp, isV.
Use camelCase, PascalCase for classes, UPPER_SNAKE_CASE for constants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do const and let improve code over var?

A

const: prevents reassignment.
let: block scoped.
var: function scoped, can cause unexpected behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Two restrictions in Strict Mode?

A

Disallows with statement, deleting undeletable properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a ReferenceError?

A

Occurs when accessing undeclared/unavailable variable at runtime.

17
Q

What is an Execution Context?

A

Environment for executing JS code: contains variables, functions, this.

18
Q

What is the Function Execution Context?

A

Created each function call with its variables, arguments, this.

19
Q

Heap vs Stack in JS memory?

A

Heap: dynamic data (objects, arrays).
Stack: static data (function calls, vars).

20
Q

Explain mutability in JS.

A

Primitives immutable. Reference types mutable. const prevents reassignment but not mutation.

21
Q

How to prevent accidental globals?

A

Always declare with var/let/const. Use ‘use strict’.

22
Q

Where are objects/arrays stored?

A

In the heap, with variables holding references.

23
Q

How to prevent accidental globals?

A

Declare with var/let/const. Use Strict Mode.