Katkat

Explain JS threading and useState vs useRef

OpenAISoftware Engineer · HR Screen

Last updated

This question evaluates understanding of JavaScript's execution model and concurrency mechanisms (including worker-based threading) along with React state-management semantics, specifically the behavioral differences between useState and useRef.

Answer the following conceptual questions about JavaScript and React:

  1. JavaScript and multithreading
  • How does JavaScript's execution model work in the browser or Node.js?
  • Is JavaScript itself multithreaded? If not, how does it achieve concurrency?
  • What mechanisms (if any) exist to use multiple threads (e.g., Web Workers or worker threads), and what are their limitations?
  1. React useState vs useRef
  • What is the purpose of the useState hook, and how does it behave with respect to component re-renders?
  • What is the purpose of the useRef hook? How is its behavior different from useState?
  • In what situations would you choose useState vs useRef? Give a few concrete examples.

Explain in enough detail that a junior engineer could understand the key differences and trade-offs.