JavaScript fundamentals are the first technical filter in nearly every frontend and Node.js interview in Pakistan. Frameworks change; these questions haven't in years. Interviewers use them to check whether you understand the language or just the library you learned it through.
1Explain closures with a practical example.
A closure is a function that retains access to variables from the scope where it was created, even after that scope has finished executing. Practical example: a counter factory — function makeCounter() { let n = 0; return () => ++n; } — each returned function keeps its own private n. Real-world uses: debounce/throttle implementations, module patterns, and React hooks, which are closures over component state.
2What is the event loop? Why does setTimeout(fn, 0) not run immediately?
JavaScript is single-threaded; the event loop coordinates the call stack with task queues. Synchronous code runs first, then queued callbacks. setTimeout(fn, 0) places fn in the macrotask queue, which only drains after the current stack and all pending microtasks (promises) complete. The classic follow-up — output order of sync logs, a promise .then, and a setTimeout — is worth practising until automatic.
3Difference between var, let, and const?
var is function-scoped, hoisted with undefined initialisation, and attaches to the global object at top level. let and const are block-scoped and hoisted into a temporal dead zone (accessing them before declaration throws). const prevents reassignment of the binding, not mutation of the object it points to. Modern default: const everywhere, let when reassignment is needed, var never.
4Explain promises and async/await. How do you handle errors in each?
A promise represents a future value in pending, fulfilled, or rejected state. .then chains transformations and .catch handles rejections anywhere upstream in the chain. async/await is syntax over promises: await pauses the async function until settlement, and errors surface as throws handled by try/catch. Interviewers often follow with Promise.all (fails fast) versus Promise.allSettled (never rejects) — know when each fits.
5What does 'this' refer to in different contexts?
It depends on the call site: for obj.method(), this is obj; for a plain function call, undefined in strict mode (or the global object otherwise); with call/apply/bind, whatever you pass; in arrow functions, this is inherited lexically from the enclosing scope — they have no own this. That last point is why arrow functions are used for callbacks inside class methods and why they can't be object methods that rely on this.
6Shallow copy vs deep copy — how do you actually deep-clone an object?
Spread ({...obj}) and Object.assign copy one level; nested objects remain shared references. For deep cloning, structuredClone(obj) is the modern built-in answer (handles dates, maps, cycles). JSON.parse(JSON.stringify(obj)) is the legacy trick, but drops functions, undefined, and dates become strings — mentioning those limitations is what earns the point.
7What are debounce and throttle, and when do you use each?
Both limit how often a function runs. Debounce waits until events stop for a delay period — right for search-as-you-type API calls. Throttle guarantees at most one execution per interval — right for scroll and resize handlers. Being able to sketch a debounce implementation with closures and setTimeout is a very common Pakistani interview whiteboard task, and it ties together closures, timers, and the event loop.
Preparation tips
Practise predicting console output for snippets mixing hoisting, promises, and setTimeout — this exact format appears in most local written tests.
Write debounce, deep-clone-lite, and array flatten by hand once; they cover 80% of implementation questions.
Say "strict mode" and "microtask vs macrotask" where relevant — precision vocabulary signals depth quickly.
First, get the interview
A strong resume is what gets you into the room. Build one free — ATS-friendly templates, unlimited edits, no account needed, first download free.
Free to build No account required ATS-friendly templates First download free