These javascript interview questions and answers cover everything Pakistani and international employers ask in 2026 — from fundamentals to the tricky follow-ups that separate hired candidates from rejected ones. JavaScript remains the most in-demand programming language for web development jobs, and interviewers test both your theoretical understanding and your ability to write clean code on the spot. Work through this guide, practise the code examples, and walk into your interview ready.
1. Basic JavaScript Interview Questions
Q: What is the difference between var, let, and const?
A: var is function-scoped and hoisted with an undefined initial value; let and const are block-scoped and live in the temporal dead zone until declaration. const additionally prevents reassignment of the binding (though object properties can still change). Modern code uses const by default and let when reassignment is needed — var is legacy.
Q: Explain == vs ===.
A: == performs type coercion before comparing (so 0 == '0' is true), while === compares both value and type with no coercion. Always prefer === to avoid subtle bugs.
Q: What are primitive data types in JavaScript?
A: String, number, bigint, boolean, undefined, symbol, and null. Everything else — objects, arrays, functions — is a reference type.
2. Closures and Scope
Q: What is a closure? Give an example.
A: A closure is a function that remembers variables from its outer scope even after that scope has finished executing. Example:
function counter() {
let count = 0;
return function() { return ++count; };
}
const c = counter();
c(); // 1 — 'count' is preserved via closure
Interviewers love closures because they reveal whether you truly understand scope. Be ready to explain practical uses: data privacy, function factories, and event handlers.
3. ES6+ Features Interviewers Always Ask
Q: What are arrow functions, and how does 'this' behave in them?
A: Arrow functions (=>) provide shorter syntax and do not bind their own this — they inherit it from the surrounding scope. This makes them ideal for callbacks but unsuitable as object methods or constructors.
Q: Explain destructuring, spread, and rest.
A: Destructuring unpacks values: const {name} = user. Spread expands: [...arr1, ...arr2]. Rest collects remaining items: function sum(...nums). Know all three with examples.
Q: What are promises, and how does async/await relate?
A: A promise represents a future value with three states: pending, fulfilled, rejected. async/await is syntactic sugar over promises that lets asynchronous code read like synchronous code. Always mention error handling with try/catch or .catch().
4. The Event Loop and Asynchronous JavaScript
Q: Explain the event loop.
A: JavaScript is single-threaded. The event loop continuously checks the call stack and the task/microtask queues: when the stack is empty, it moves queued callbacks onto the stack. Microtasks (promises) run before macrotasks (setTimeout). A strong answer walks through a short code example predicting output order — practise this, as it is the single most common "gotcha" question.
Q: What is the difference between setTimeout, setImmediate, and process.nextTick?
A: process.nextTick runs before the next event-loop phase, promise microtasks run after the current operation, setImmediate runs on the next loop iteration, and setTimeout(fn, 0) runs after a minimum delay. (Node.js context — mention it only for backend roles.)
5. DOM and Browser Questions
Q: What is event delegation?
A: Attaching one listener to a parent element instead of many listeners to children, using event.target to identify which child triggered the event. It improves performance and handles dynamically added elements.
Q: Explain event bubbling and capturing.
A: Events propagate in two phases: capturing (window down to target) then bubbling (target up to window). addEventListener defaults to bubbling; pass true as the third argument for capturing. stopPropagation() halts the flow.
6. Tricky Favourites
Q: What is hoisting?
A: Declarations are moved to the top of their scope during compilation: function declarations are fully hoisted, var is hoisted as undefined, and let/const are hoisted but uninitialised (temporal dead zone).
Q: What is the difference between null and undefined?
A: undefined means a variable was declared but never assigned; null is an intentional assignment meaning "no value". typeof null returns "object" — a famous language quirk worth mentioning.
Q: Explain prototypal inheritance in one minute.
A: Objects inherit directly from other objects via the prototype chain. When you access a property, JavaScript walks up the chain until it finds it. ES6 classes are syntactic sugar over this system.
7. Coding Questions to Practise
Most javascript interview questions include live coding. Practise these until they feel automatic:
- Reverse a string and check for palindromes.
- Implement debounce and throttle from scratch.
- Flatten a nested array without
Array.flat(). - Write a deep-clone function handling nested objects.
- Fetch data from an API with error handling and loading states.
How to Prepare in the Final Week
Do not just memorise answers — interviewers spot recitation instantly. For each topic above, write the code yourself, break it, and fix it. Explain concepts aloud as if teaching a friend; if you stumble, that is the gap to close. Build one small project (a to-do app with localStorage, or a weather app using fetch) so you have real code to discuss. Finally, research the company: a software house in Lahore hiring React developers will weight component questions heavily, while a backend Node.js role will dig into the event loop. Tailor your preparation, revise these javascript interview questions the night before, and walk in confident.
Comments (0)
Leave a Comment