Frontend Interview Questions for Freshers: JavaScript, React, HTML and CSS
Frontend interviews are practical. You will usually be asked JavaScript fundamentals, then framework questions (most often React), then to build or debug a small component while sharing your screen.
JavaScript
What is the difference between var, let and const?
var is function-scoped and hoisted with the value undefined. let and const are block-scoped and cannot be used before their declaration. const cannot be reassigned, though an object it holds can still be mutated.
What is a closure?
A function that remembers the variables of the scope it was created in, even after that outer function has returned. Closures are how you create private state, such as a counter function that keeps its own count.
What is the difference between == and ===?
== converts the operands to a common type before comparing, so "5" == 5 is true. === compares value and type without conversion. Use === by default.
Explain the event loop.
JavaScript runs on one thread. Synchronous code runs on the call stack; callbacks from timers, network calls and events wait in queues. When the stack is empty the event loop takes the next task. Promise callbacks (microtasks) run before timer callbacks (macrotasks).
What is a Promise, and what does async/await do?
A Promise represents a value that will be available later; it is pending, then fulfilled or rejected. async/await is syntax over promises: await pauses the async function until the promise settles, so asynchronous code reads top to bottom and errors can be handled with try/catch.
What is event delegation?
Attaching one listener to a parent element and using event.target to see which child was clicked, instead of adding a listener to every child. It works because events bubble up, and it handles elements added later.
What is the difference between null and undefined?
undefined means a variable was declared but not given a value, or a property does not exist. null is a value you assign deliberately to mean "no value".
What is debouncing?
Delaying a function until the user has stopped triggering it for a set time, for example waiting 300 ms after the last keystroke before calling a search API. Throttling instead runs the function at most once per interval.
React
What is the virtual DOM?
An in-memory description of the UI. When state changes, React builds a new tree, compares it with the previous one and applies only the differences to the real DOM, which is the slow part.
What is the difference between props and state?
Props are inputs passed from a parent and are read-only in the child. State is data a component owns and can change; changing it re-renders the component.
What do useState and useEffect do?
useState adds a state variable and a setter to a function component. useEffect runs side effects such as fetching data or subscribing after render; its dependency array controls when it re-runs, and its returned function cleans up.
Why do list items need a key?
Keys let React match items between renders so it can reuse, move or remove the right elements. Use a stable ID; using the array index causes bugs when the list is reordered or items are inserted.
What is a controlled component?
A form input whose value comes from React state and is updated through onChange, so state is the single source of truth. An uncontrolled input keeps its own value in the DOM and is read with a ref.
What is prop drilling and how do you avoid it?
Passing props through several layers of components that do not use them. You avoid it with the Context API or a state management library, or by restructuring so the data lives closer to where it is used.
HTML and CSS
What is semantic HTML?
Using elements that describe their meaning (header, nav, main, article, button) rather than generic divs. It improves accessibility for screen readers and helps search engines understand the page.
Explain the CSS box model.
Every element is a box made of content, padding, border and margin. With box-sizing: border-box, the width you set includes padding and border, which makes layouts easier to reason about.
Flexbox or Grid?
Flexbox lays items out in one direction, a row or a column, and is ideal for navigation bars and aligning items. Grid lays out in two dimensions, rows and columns together, and is ideal for page layouts.
What is CSS specificity?
The rule that decides which style wins when several match: inline styles beat IDs, IDs beat classes, attributes and pseudo-classes, and those beat element selectors. With equal specificity the later rule wins.
How do you make a page responsive?
Use relative units and flexible layouts, set the viewport meta tag, and add media queries to change layout at breakpoints. Design for small screens first, then widen.
Machine-coding tasks to practise
- A to-do list with add, complete, delete and filter.
- A search box that calls an API with debouncing and shows loading and error states.
- A counter or stopwatch with start, stop and reset.
- A paginated or infinitely scrolling list from a public API.
- A form with validation and disabled submit until valid.