Frontend Developer Interview Questions and Answers
Frontend interviews combine language fundamentals (closures, the event loop, async code) with framework knowledge and an understanding of how the browser renders and updates the page.
Intern / Fresher
- What is the difference between var, let and const in JavaScript?
var is function-scoped and can be redeclared; let and const are block-scoped, and const cannot be reassigned after it is set.
- What is the difference between == and === in JavaScript?
== compares values after type coercion, while === compares both value and type with no conversion, so it is the safer default.
- What is hoisting in JavaScript?
Hoisting is JavaScript moving declarations to the top of their scope during compilation, so functions and var variables can be referenced before the line where they are written.
- What is the difference between null and undefined in JavaScript?
undefined means a variable has been declared but no value was assigned; null is an explicit value meaning 'no value' that you assign on purpose.
- What is the difference between map, filter and reduce in JavaScript?
map transforms every item into a new array of the same length, filter keeps only items that pass a test, and reduce folds the whole array into a single value.
- What is the virtual DOM in React and how does it work?
The virtual DOM is a lightweight JavaScript copy of the UI that React compares with the previous version after each update, so it only changes the parts of the real DOM that actually differ.
- What is the difference between props and state in React?
Props are read-only inputs passed from a parent component, while state is data a component owns and can change over time, which triggers a re-render when updated.
- What are semantic HTML elements and why do they matter?
Semantic elements describe their meaning in the name, such as header, nav, main, article and footer, so browsers, assistive technology and search engines can understand the page structure.
- What is the CSS box model?
Every element is a box made of content, padding, border and margin. The default content-box size counts only the content; border-box includes padding and border in the width you set.
- What is the difference between display none and visibility hidden in CSS?
display: none removes the element from the layout so it takes no space; visibility: hidden hides it but leaves the empty space, and the element can still be read in the accessibility tree in some cases.
- What is the difference between inline, block and inline-block in CSS?
Block elements start on a new line and take the full available width; inline elements sit in the text flow and ignore width and height; inline-block sits in the flow but accepts width, height, padding and margin on all sides.
- What is TypeScript and why do we use it?
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It catches type errors in the editor and at build time, and it documents the shape of data for the next person who reads the code.
- What is the difference between interface and type in TypeScript?
Both describe object shapes. interface can be merged and is preferred for public object contracts; type can express unions, intersections and primitives, and cannot be reopened later.
Junior (0-2 years)
- What is a closure in JavaScript and why is it useful?
A closure is a function that keeps access to the variables of the scope where it was created, even after that outer function has finished running.
- How does the this keyword work in JavaScript?
this is decided by how a function is called, not where it is written: as a method it is the object before the dot, with new it is the new object, and arrow functions inherit it from the enclosing scope.
- What is the difference between promises and async/await in JavaScript?
async/await is syntax built on top of promises that lets you write asynchronous code that reads like synchronous code; under the hood every async function returns a promise.
- What is event delegation in JavaScript?
Event delegation means attaching one listener to a parent element and using event bubbling to handle events from its children, instead of adding a listener to each child.
- What are React hooks and what are the rules of hooks?
Hooks are functions such as useState and useEffect that let function components use state, lifecycle behaviour and other React features; they must be called at the top level of a component or custom hook, never inside loops, conditions or nested functions.
- How does useEffect work in React and what is the dependency array?
useEffect runs a function after the component renders; the dependency array tells React when to re-run it: with no array it runs after every render, with an empty array only after the first, and with values only when those values change.
- Why do we need keys in React lists?
Keys give each list item a stable identity so React can match items between renders and correctly add, remove or reorder them without recreating or mixing up their state.
- What is the difference between controlled and uncontrolled components in React?
A controlled component keeps the input's value in React state and updates it through onChange, while an uncontrolled component lets the DOM hold the value and you read it with a ref when needed.
- What is prop drilling and how do you avoid it in React?
Prop drilling is passing props through several layers of components that do not use them just to reach a deeply nested child; you avoid it with composition, the Context API or a state management library.
- What is the difference between relative, absolute, fixed and sticky positioning in CSS?
Relative offsets an element from its normal place; absolute positions it against the nearest positioned ancestor; fixed is against the viewport; sticky sits in normal flow until a scroll threshold, then sticks.
- What is the difference between Flexbox and CSS Grid?
Flexbox is one-dimensional (a row or a column). CSS Grid is two-dimensional (rows and columns at once). Use Flexbox for alignment along a line; use Grid when the layout is a real 2D structure.
- How does CSS specificity work?
Specificity is the score the browser uses to decide which rule wins: inline styles beat IDs, IDs beat classes/attributes/pseudo-classes, and those beat elements. When scores tie, the last rule in the source wins.
- What is the difference between em, rem, %, vh and vw in CSS?
em is relative to the parent font size, rem is relative to the root font size, % is relative to the parent’s relevant size, and vh/vw are 1% of the viewport height or width.
- What is the difference between cookies, localStorage and sessionStorage?
Cookies are sent to the server with every matching request and can expire; localStorage persists until cleared and stays in the browser; sessionStorage lasts for the current tab only.
- What is the difference between any, unknown and never in TypeScript?
any turns off checking. unknown is a type-safe any: you must narrow it before use. never means a value cannot exist, used for exhaustive checks and functions that always throw.
- What are generics in TypeScript?
Generics are type parameters: they let a function, class or type work with many types while keeping the relationship between inputs and outputs, instead of falling back to any.
- What are union and intersection types in TypeScript?
A union (A | B) is a value that is one of the members. An intersection (A & B) is a value that must satisfy both types at once.
- What are utility types in TypeScript like Partial, Pick and Omit?
Utility types are built-in helpers that transform an existing type: Partial makes every field optional, Required the opposite, Pick keeps a set of keys, Omit removes them, and Record builds a key/value map.
Mid-level (2-5 years)
- What is the event loop in JavaScript and how does it work?
The event loop is the mechanism that lets single-threaded JavaScript handle asynchronous work: it runs the call stack to empty, then runs all queued microtasks (promises), then takes the next task (timers, events) from the queue.
- What is prototypal inheritance in JavaScript?
In JavaScript every object has an internal link to another object called its prototype, and property lookups walk up this prototype chain until the property is found or the chain ends.
- What is the difference between debounce and throttle in JavaScript?
Debounce waits until the user stops triggering an event for a set time before running the function; throttle runs the function at most once per time interval no matter how often the event fires.
- What is the difference between useMemo and useCallback in React?
useMemo caches the result of a calculation, while useCallback caches a function definition; both only recompute when their dependencies change.
- What is the difference between useRef and useState in React?
Updating state triggers a re-render, while changing a ref's .current value does not; use state for data shown on screen and refs for DOM access or values that must persist without causing renders.
- What is the difference between SSR, SSG and CSR in React?
CSR renders in the browser after JavaScript loads, SSR renders the HTML on the server for each request, and SSG renders the HTML once at build time; SSR and SSG give crawlers and users content faster, which helps SEO.
- What is the difference between async and defer on script tags?
Both download the script without blocking HTML parsing. defer waits until parsing finishes and runs scripts in document order; async runs a script as soon as it is downloaded, which can be before or after parsing and in any order.
- How does the browser render a web page?
The browser parses HTML into a DOM and CSS into a CSSOM, combines them into a render tree, computes layout (reflow), paints pixels and composites layers onto the screen.
- What is type narrowing in TypeScript?
Narrowing is when TypeScript shrinks a wide type (such as a union) to a more specific type after a runtime check like typeof, instanceof, in, or a custom type guard.
- What is the difference between enum and union types in TypeScript?
A string union such as "admin" | "user" exists only in the type system and erases to plain strings. A TypeScript enum compiles to a JavaScript object (unless it is a const enum), which is extra runtime code and a weaker default.
- How does TypeScript type inference work?
TypeScript fills in types you omit: literals get their literal or widened type, function return types are inferred from return statements, and generic arguments are inferred from the values you pass.
Senior (5+ years)
- How do you find and fix memory leaks in JavaScript?
Take heap snapshots in Chrome DevTools before and after the suspect action, compare what grew, and fix the retaining reference, which is usually a forgotten listener, timer, global variable or unbounded cache.
- Why does a React component re-render and how do you prevent unnecessary re-renders?
A component re-renders when its state changes, its parent re-renders, or a context it consumes changes; you prevent wasted renders by moving state down, using React.memo, and keeping props referentially stable.
- How do you optimize performance in a React application?
Measure first with the Profiler and Lighthouse, then reduce unnecessary renders, split code with lazy loading, virtualise long lists, and optimise images and bundle size.
- What are Core Web Vitals and how do you improve them?
Core Web Vitals are Google’s field metrics for real-user experience: LCP (loading), INP (interactivity) and CLS (visual stability). They influence search ranking and are measured on real devices, not only Lighthouse.
- What are mapped types and conditional types in TypeScript?
Mapped types build a new object type by looping over keys (`{ [K in keyof T]: ... }`). Conditional types pick a result with `T extends U ? X : Y`, which is how many utility types are implemented.