JavaScript Internals & Advanced Concepts Flashcards

(52 cards)

1
Q

What is an execution context in JavaScript?

A

It’s an abstract concept describing the environment where code is evaluated and executed, including scope, variables, and the value of ‘this’.

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

What is the call stack?

A

The call stack is a data structure that tracks function invocation, executing the most recent function call first (LIFO).

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

What is a closure in JavaScript?

A

A closure is a function that retains access to its lexical scope even after the outer function has finished executing.

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

What is memory management in JavaScript?

A

It involves allocating and releasing memory for variables, objects, and functions used in the program.

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

What is garbage collection in JavaScript?

A

It is the process of automatically reclaiming memory occupied by objects no longer reachable in the code.

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

What is the difference between deep and shallow copy?

A

A shallow copy copies only top-level properties, while a deep copy recursively copies all nested properties.

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

What is currying in JavaScript?

A

Currying is the process of breaking a function with multiple arguments into a series of functions that each take one argument.

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

What is debouncing in JavaScript?

A

Debouncing delays a function’s execution until a certain time has passed since the last call.

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

What is throttling in JavaScript?

A

Throttling limits a function to execute at most once in a specified period.

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

What is an advantage of the call stack model?

A

It simplifies function invocation tracking and local execution context.

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

What is a disadvantage of the call stack model?

A

Too many recursive calls can lead to stack overflow errors.

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

What is an advantage of closures?

A

They enable private variables and maintain state across function calls.

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

What is a disadvantage of closures?

A

They can cause memory leaks if references to outer scopes are not properly cleaned up.

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

What is an advantage of automatic garbage collection?

A

It simplifies memory management for developers.

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

What is a disadvantage of automatic garbage collection?

A

It can lead to non-deterministic memory cleanup, causing performance hiccups.

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

What is a best practice with closures?

A

Avoid overusing closures in long-lived contexts to prevent memory leaks.

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

What is a best practice with memory management?

A

Avoid global variables and detach event listeners when no longer needed.

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

What is a best practice with deep vs shallow copies?

A

Use deep copies when working with nested objects that need to be independently modified.

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

What is a best practice with debouncing and throttling?

A

Use debouncing for user input, and throttling for scroll or resize events.

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

What is a use case for execution context and call stack?

A

Debugging function call chains and understanding async stack traces.

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

What is a use case for closures?

A

Implementing data encapsulation and factory functions.

22
Q

What is a use case for garbage collection?

A

Freeing memory in long-running servers or applications to maintain performance.

23
Q

What is a use case for deep copy?

A

Cloning application state in Redux or similar state management libraries.

24
Q

What is a use case for currying?

A

Creating reusable, configurable functions in functional programming.

25
What is a use case for throttling?
Limiting the frequency of API calls or event listeners in real-time apps.
26
How does the call stack affect system design?
Impacts recursion depth, error tracing, and async execution flow.
27
How do closures affect system design?
They can support modular architecture but may complicate debugging and memory profiling.
28
What are architectural implications of memory management?
Efficient memory use is critical in high-load or long-lived Node.js applications.
29
What is the architectural impact of debounce/throttle?
They optimize frontend responsiveness and reduce server load.
30
Give an example of a closure.
function outer() { let count = 0; return function() { count++; return count; }; }
31
Give an example of a shallow vs deep copy.
Shallow: let b = {...a}; Deep: let b = JSON.parse(JSON.stringify(a));
32
Give an example of currying.
const add = a => b => c => a + b + c; add(1)(2)(3);
33
Give an example of debounce.
function debounce(fn, delay) { let timeout; return (...args) => { clearTimeout(timeout); timeout = setTimeout(() => fn(...args), delay); }; }
34
Give an example of throttle.
function throttle(fn, limit) { let inThrottle; return (...args) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => inThrottle = false, limit); } }; }
35
How does call stack depth impact performance?
Deep stacks increase memory usage and risk stack overflows.
36
How do closures affect memory usage?
They hold references to outer variables, which can cause memory leaks if not handled carefully.
37
How does garbage collection affect performance?
Pauses for GC cycles can cause slight performance dips during cleanup.
38
How do you monitor call stack usage?
Use DevTools or Node.js inspector to analyze stack traces and memory snapshots.
39
How to debug closure-related bugs?
Use named functions, avoid unnecessary outer references, and inspect scope chains in DevTools.
40
How to monitor memory usage in Node.js?
Use process.memoryUsage(), Chrome DevTools, or heap profiling tools.
41
What is a real-world tradeoff with closures?
State persistence vs. potential for retained memory and leaks.
42
What is a real-world tradeoff with garbage collection?
Ease of development vs. lack of precise control over memory cleanup.
43
What is a real-world tradeoff with debounce/throttle?
Better performance vs. delayed or skipped execution events.
44
Interview Question: What is the difference between stack and heap memory?
Stack memory stores primitives and call contexts, while heap stores objects and dynamic data.
45
Interview Question: What are closures and how are they used?
Closures are inner functions that access outer function variables even after the outer function exits. Used in encapsulation, callbacks, and async handlers.
46
Interview Question: What tools are used to analyze memory leaks in Node.js?
Chrome DevTools, Node.js built-in profiler, `heapdump`, and `memwatch-next`.
47
Interview Question: When should you use deep copy instead of shallow copy?
When modifying nested structures that should not affect the original object.
48
Interview Question: What is currying and why is it useful?
Currying transforms a function so it can be partially applied; useful for function reuse and composition.
49
What is a gotcha with the call stack?
Infinite recursion or long sync loops can cause stack overflow errors.
50
What is a gotcha with closures?
Retaining unintended variables in memory if closures are misused or not released.
51
What is a gotcha with deep copy?
Using JSON.parse/stringify fails with functions, dates, or circular references.
52
What is a gotcha with debounce/throttle?
Incorrect delay settings can make UI feel laggy or unresponsive.