Filter by the role you are applying for, open a question, and practise explaining the answer out loud. Mark it practised when you can.
Why they ask this: Closures power callbacks, React hooks, module patterns and data privacy, so they show how well you know the language.
Answer
A closure is a function together with the variables from the scope where it was created. The inner function keeps access to those variables even after the outer function has returned.
function makeCounter() {
let count = 0; // private to the closure
return {
increment: () => ++count,
current: () => count,
};
}
const counter = makeCounter();
counter.increment();
counter.increment();
console.log(counter.current()); // 2Practical uses:
count cannot be changed from outside.const add5 = makeAdder(5).Why they ask this: The most common Eloquent performance bug; it shows you think about the queries your code runs.
Answer
The N+1 problem happens when you load a list (1 query) and then run one more query per item to load a relationship (N queries).
// 1 query for posts + 1 query per post for its author = N+1
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name;
}Fix it with eager loading, which loads the relationship for all rows in one extra query:
$posts = Post::with('author')->get(); // 2 queries in total
$posts = Post::with(['author', 'comments'])->get();
$count = Post::withCount('comments')->get(); // comments_count without loading themFind N+1 problems with Laravel Debugbar or Telescope, or make them fail loudly in development with Model::preventLazyLoading(! app()->isProduction());.
Why they ask this: An open-ended classic that shows how broad your understanding of the web is.
Answer
https), host and path.GET /jobs with headers and cookies.Go deeper on whichever part matches the role you are applying for.
Why they ask this: Shows whether you understand how async code actually runs, which matters for debugging ordering and UI freezes.
Answer
JavaScript runs your code on one thread with a call stack. Async work (timers, network, DOM events) is handled by the browser or Node.js, and their callbacks are queued.
The event loop repeats:
.then, await continuations) and queueMicrotask.setTimeout callback or an event handler, run it, and go back to step 2.console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
// Output: 1, 4, 3, 2Microtasks always run before the next timer, even a 0 ms one. A long synchronous loop blocks everything, including clicks and rendering.
Why they ask this: Bugs with lost this in callbacks are common, and the answer shows depth beyond syntax.
Answer
this depends on how a function is called, not where it is written:
| Call style | this |
|---|---|
obj.method() | obj |
plain fn() | undefined in strict mode (the global object otherwise) |
new Fn() | the new object |
fn.call(x) / fn.apply(x) / fn.bind(x) | x |
| arrow function | this of the surrounding scope (arrows have no own this) |
const user = {
name: 'Hari',
hello() { return this.name; },
later() { setTimeout(() => console.log(this.name)); }, // arrow keeps `this`
};
const fn = user.hello;
fn(); // `this` is lost: undefined in strict modeFix lost this with an arrow function or .bind(obj).
Why they ask this: Almost every app calls APIs; interviewers check you can write and handle async code correctly.
Answer
A Promise represents a value that will be available later (pending, then fulfilled or rejected). async/await is syntax on top of promises that makes async code read like synchronous code.
// Promise chain
fetch('/api/jobs')
.then((res) => res.json())
.then((jobs) => console.log(jobs))
.catch((err) => console.error(err));
// async/await
async function loadJobs() {
try {
const res = await fetch('/api/jobs');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return await res.json();
} catch (err) {
console.error(err);
}
}Points worth mentioning:
async function always returns a promise.try/catch for errors with await.await Promise.all([...]) instead of awaiting one after another.Why they ask this: Tests your understanding of DOM event bubbling and performance with dynamic lists.
Answer
Most DOM events bubble up from the target element to its ancestors. Event delegation puts one listener on a parent and uses event.target to work out which child was clicked.
document.querySelector('#todo-list').addEventListener('click', (event) => {
const button = event.target.closest('button.delete');
if (!button) return;
button.closest('li').remove();
});Benefits:
Use event.stopPropagation() carefully, because it breaks delegation for listeners higher up.
Why they ask this: Common in search boxes, scroll and resize handlers; shows you think about performance.
Answer
Both limit how often a function runs:
function debounce(fn, delay = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
const onSearch = debounce((q) => fetch(`/api/search?q=${encodeURIComponent(q)}`));Why they ask this: Most React bugs (infinite loops, stale data, memory leaks) come from misusing useEffect.
Answer
useEffect(effect, deps) runs effect after render:
[]: runs once after the first render (mount).[a, b]: runs after the first render and again whenever a or b changes (compared with Object.is).The function you return is the cleanup. It runs before the effect runs again and when the component unmounts. Use it to remove listeners, clear timers and cancel requests.
useEffect(() => {
const controller = new AbortController();
fetch(`/api/jobs?q=${query}`, { signal: controller.signal })
.then((r) => r.json())
.then(setJobs)
.catch(() => {});
return () => controller.abort(); // cancel the old request when query changes
}, [query]);Include every value from the component that the effect uses in the array. Missing dependencies cause stale values.
Why they ask this: Forms are in every app; this shows how you handle user input and validation.
Answer
value comes from state and every change goes through onChange.ref or from the form data on submit.// Controlled
const [email, setEmail] = useState('');
<input value={email} onChange={(e) => setEmail(e.target.value)} />
// Uncontrolled
const inputRef = useRef(null);
<input ref={inputRef} defaultValue="" />
// later: inputRef.current.valueChoose controlled inputs for instant validation, formatting or conditional UI. Uncontrolled inputs are simpler for basic forms and file inputs (<input type="file"> is always uncontrolled). Form libraries such as React Hook Form mostly use uncontrolled inputs for performance.
Why they ask this: Explains "why is my style not applying?" problems that every frontend developer hits.
Answer
When several rules target the same element, the browser picks the one with the highest specificity. If specificity is equal, the rule that appears later wins.
Specificity is compared as (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements):
| Selector | Specificity |
|---|---|
p | (0, 0, 1) |
.card p | (0, 1, 1) |
#main .card p | (1, 1, 1) |
inline style="" | beats any selector |
!important | overrides normal declarations; avoid it |
:where() adds zero specificity, while :is() and :not() take the specificity of their most specific argument.
Keep specificity low and flat (for example single classes with a BEM-style or utility approach) so styles stay easy to override.
Why they ask this: Accessibility is increasingly expected; it shows care for all users and code quality.
Answer
<button>, <nav>, <main>, headings in order) before reaching for ARIA.alt text (alt="" for decorative images).<label> (for/id, or wrap the input).:focus-visible style.aria-expanded on a menu toggle or aria-live for status messages.Check your work with Lighthouse or axe DevTools and by navigating with only the keyboard.