frontend-interview-questions

1. How does the JavaScript event loop work, and how does it impact React’s rendering and state updates?

The event loop allows JavaScript (single-threaded) to handle asynchronous operations using a call stack , task queue (macrotasks) , and microtask queue .

In React:


2. What’s your mental model for understanding JavaScript closures, and how do you use them effectively in a React application?

A closure is a function that retains access to its lexical scope , even when invoked outside that scope.

In React:

Closures must be handled carefully in effects to avoid stale state issues.


3. How do you explain prototypal inheritance in JavaScript, and how does it differ from classical inheritance?

Prototypal inheritance links objects to other objects via the [[Prototype]] chain , enabling property sharing.

Unlike classical inheritance:


4. What’s the significance of the this keyword in JavaScript, and how do you manage its binding in React components?

this refers to the execution context . In React class components, this often refers to the component instance .

In class components:

this.handleClick = this.handleClick.bind(this);

In functional components:


5. How does JavaScript’s single-threaded nature influence your approach to handling computationally expensive tasks in a React app?

Since JS runs on a single thread , heavy tasks block the UI .

Strategies:


6. How do you design a robust async/await workflow in a React application for handling multiple API calls?

Use async/await inside useEffect, encapsulated in try/catch, and optionally use Promise.all


7. What’s the difference between promises and async/await under the hood, and how do you optimize their usage in React?

Optimizations:


8. How do you implement a custom Promise-based utility (e.g., a retry mechanism) for a React app?

const retry = (fn, retries = 3, delay = 1000) =>
  fn().catch((err) => {
    if (retries === 0) throw err;
    return new Promise((res) => setTimeout(res, delay)).then(() =>
      retry(fn, retries - 1, delay)
    );
  });


9. What’s your approach to managing microtasks and macrotasks in a React application with heavy async operations?

Use microtasks for chaining logic and macrotasks for deferring execution.


10. How do you use the async iterator protocol (e.g., for await...of) in a React app for streaming data?

Used for streaming APIs (e.g., SSE, file chunks):

async function streamData(url) {
  const response = await fetch(url);
  for await (const chunk of response.body) {
    processChunk(chunk);
  }
}

In React:


11. How do you leverage ES6 modules in a large React codebase to ensure maintainability and tree-shaking?


12. What’s your understanding of JavaScript scope and hoisting, and how do you prevent common pitfalls in React?

Answer:

In React:


13. How do you design a JavaScript utility library for a React team, balancing encapsulation and reusability?

Answer:


14. How do you apply functional programming principles (e.g., immutability, pure functions) in a React application?


15. What’s your approach to memoizing expensive computations in JavaScript for a React app?

Use useMemo:

const filtered = useMemo(() => heavyFilter(data), [data]);

Avoid premature optimization; profile first. Ensure dependencies are correct to avoid stale or recomputed values.


16. How do you optimize JavaScript performance in a React app by minimizing object mutations?


17. How do you use JavaScript Proxies or Reflect in a React application for dynamic state management or debugging?

Use Proxy to track mutations or build reactive-like wrappers:

const state = new Proxy(
  {},
  {
    set(target, key, value) {
      console.log(`${key} changed to ${value}`);
      target[key] = value;
      return true;
    },
  }
);

Good for debugging , form libraries , or custom observables .


18. What’s your understanding of JavaScript’s garbage collection, and how do you prevent memory leaks in a React app?

JS uses mark-and-sweep GC . Memory leaks happen when objects remain referenced unintentionally.

In React:


19. How do you handle JavaScript’s floating-point precision issues in a React app (e.g., financial calculations)?

Avoid native floats for money. Use:


20. What’s your approach to polyfilling or shimming modern JavaScript features for older browsers in a React app?