. Modules are deferred by default, use strict mode, and have their own top-level scope: declarations do not become properties on window. The browser also caches a module within the page, and modules can use static or dynamic imports. Scopes: Scope determines where a variable can be accessed. let and const are block-scoped; var is function-scoped. Functions create their own scope, and closures allow an inner function to retain access to variables from its outer scope. Keeping component state inside a function or module reduces accidental global variables and naming collisions. IIFEs: An Immediately Invoked Function Expression is a function defined and called immediately, such as (function () { const state = {}; })();. Before native modules, IIFEs were a common way to create a private scope and avoid leaking variables globally. They remain useful in some legacy scripts, but modules usually provide clearer dependency management and encapsulation for modern applications." } }, { "@type": "Question", "name": "Why use React, and how does React work under the hood?", "acceptedAnswer": { "@type": "Answer", "text": "I use React when I want to build a UI from reusable components and describe what the interface should look like for a given state, rather than manually keeping DOM updates in sync. That model is useful for complex interfaces such as a payments or rewards flow, where many UI states depend on user and server data. Under the hood, the broad process is: A component renders React elements from its props and state. These elements are lightweight descriptions of the UI, not DOM nodes. React reconciles the new element tree with the previous one. Its Fiber architecture represents work and supports prioritizing or yielding during rendering. React determines what needs to change. Stable keys help it match items correctly in lists. In the commit phase, React applies the necessary mutations to the host environment, typically the browser DOM. State updates can be batched. With concurrent features, render work may be interrupted or restarted, but the commit phase applies a consistent result. React’s main benefit is the component and state model, not a guarantee that it is always faster than hand-written DOM updates. It makes UI changes more predictable and maintainable; performance still depends on sensible rendering, data flow, and profiling." } }, { "@type": "Question", "name": "How would you attach click event listeners to suggestions, and what are event bubbling and event delegation?", "acceptedAnswer": { "@type": "Answer", "text": "Attach listeners directly: For a small, fixed set of suggestion elements, add a click listener to each one. If suggestions are recreated after every search, you must attach listeners again whenever you render them. Use event delegation for dynamic suggestions: Attach one listener to the stable parent (for example, the suggestions