Ace Your Interviews 🎯

Browse our collection of interview questions across various technologies.

JavaScriptBeginner

What is JavaScript and what is it primarily used for?

Answer

JavaScript is a high-level, interpreted programming language originally designed for browser interactivity. Today it runs in browsers (frontend), on servers (Node.js), in mobile apps (React Native), and desktop apps (Electron). It is the most used programming language in the world.

JavaScriptBeginner

What is the difference between let, const, and var?

Answer

var is function-scoped and hoisted — avoid it. let is block-scoped and reassignable. const is block-scoped and cannot be reassigned (though object properties can still be mutated). Use const by default; let when you need reassignment; never use var in new code.

JavaScriptBeginner

What is the difference between == and ===?

Answer

== is loose equality — performs type coercion before comparing (1 == '1' is true). === is strict equality — no coercion, compares both value and type (1 === '1' is false). Always use ===.

JavaScriptBeginner

What are JavaScript data types?

Answer

7 primitives: string, number, bigint, boolean, null, undefined, symbol. And one complex type: object (which includes arrays, functions, and plain objects). typeof null returns 'object' — this is a known bug.

JavaScriptBeginner

What is the difference between null and undefined?

Answer

undefined means a variable was declared but not assigned a value. null is an intentional absence of value — you explicitly assign it. typeof undefined is 'undefined'. typeof null is 'object' (JavaScript bug).

JavaScriptBeginner

What is hoisting in JavaScript?

Answer

JavaScript moves function declarations and var declarations to the top of their scope during compilation. Function declarations are fully hoisted (callable before the definition). var is hoisted but initialized to undefined. let and const are hoisted but not initialized — accessing them before declaration throws a ReferenceError (Temporal Dead Zone).

JavaScriptBeginner

What is an arrow function? How does it differ from a regular function?

Answer

Arrow functions have shorter syntax and don't create their own 'this' binding — they inherit 'this' from the surrounding scope. They also can't be used as constructors and don't have the 'arguments' object. Use arrow functions for callbacks and non-method functions; regular functions when you need 'this' to refer to the calling object.

JavaScriptBeginner

What is the difference between Array.map(), Array.filter(), and Array.reduce()?

Answer

map() transforms every element and returns a new array of the same length. filter() returns a new array containing only elements that pass a condition. reduce() accumulates elements into a single value (sum, count, object). All three return new arrays/values without mutating the original.

JavaScriptBeginner

What is a callback function?

Answer

A function passed as an argument to another function, to be called later. The foundation of asynchronous JavaScript. Array methods like map and filter take callback functions. Event listeners receive callback functions. The term 'callback hell' refers to deeply nested callbacks.

JavaScriptBeginner

What is the DOM?

Answer

Document Object Model — the browser's structured, programmable representation of an HTML page as a tree of nodes. JavaScript uses the DOM API (document.getElementById, querySelector, createElement, etc.) to read and modify page content dynamically.

JavaScriptIntermediate

What is a closure and give a real-world example?

Answer

A closure is a function that has access to variables from its outer scope even after that scope has returned. Real example: a counter function returns increment/decrement functions that share and remember a private count variable. React's useState hook is implemented using closures.

JavaScriptIntermediate

Explain the JavaScript Event Loop.

Answer

JavaScript is single-threaded. Synchronous code runs on the Call Stack. Async operations (fetch, setTimeout) are handled by Web APIs/libuv. Their callbacks are placed in the Callback Queue (or Microtask Queue for Promises). The Event Loop moves callbacks to the Call Stack when it's empty. Microtasks (Promises) drain before macrotasks (setTimeout).

JavaScriptIntermediate

What is the difference between Promise.all() and Promise.allSettled()?

Answer

Promise.all() rejects immediately if any promise rejects — fast-fail. Promise.allSettled() waits for all promises to finish regardless of outcome, returning an array of { status: 'fulfilled'|'rejected', value|reason } objects. Use all() when you need all or nothing; allSettled() when you need results from all even if some fail.

JavaScriptIntermediate

What is the prototype chain?

Answer

Every JavaScript object has an internal [[Prototype]] link to another object. When you access a property, JS looks in the object, then its prototype, then the prototype's prototype — up the chain until it reaches null. This is how methods like .toString() on your objects come from Object.prototype.

JavaScriptIntermediate

What is the 'this' keyword and how is its value determined?

Answer

The value of 'this' depends on how a function is called:

1

Regular function called as a method — 'this' is the object.

2

Regular function called alone — 'this' is global (or undefined in strict mode).

3

Arrow function — 'this' is from the outer scope.

4

Constructor with new — 'this' is the new object.

5

.call()/.apply()/.bind() — 'this' is explicitly set.

JavaScriptIntermediate

What is event delegation?

Answer

Attaching one event listener to a parent element to handle events from all its children, using event.target to identify which child triggered the event. Benefits: one listener instead of many, handles dynamically added children, better memory performance. Used heavily in vanilla JS for dynamic lists.

JavaScriptIntermediate

What is the difference between synchronous and asynchronous code?

Answer

Synchronous code blocks execution — the next line waits until the current one finishes. Asynchronous code doesn't block — the operation is scheduled, JS continues executing, and the callback/promise resolution runs when the operation completes. Network requests, file reads, and timers are always async in JavaScript.

JavaScriptIntermediate

What are Promises and why were they introduced?

Answer

Promises represent an eventual result of an async operation — pending, fulfilled, or rejected. They were introduced to solve callback hell (nested callbacks), providing .then()/.catch() chaining and eventually enabling async/await syntax. Promises make async code more readable and maintainable.

JavaScriptIntermediate

What is the spread operator and what can it do?

Answer

... spreads an iterable (array, object, string) into individual elements. Uses: copy arrays ([...arr]), merge arrays ([...a, ...b]), copy objects ({...obj}), merge objects ({...a, ...b}), pass array as function arguments (fn(...arr)). Creates shallow copies — nested objects still share references.

JavaScriptIntermediate

What is optional chaining (?.) and why is it useful?

Answer

It safely accesses nested object properties by short-circuiting to undefined if any part of the chain is null or undefined, instead of throwing a TypeError. user?.address?.city returns undefined if address is null, rather than crashing. Dramatically reduces defensive null-checking code.

JavaScriptAdvanced

What is the difference between microtasks and macrotasks in the event loop?

Answer

Microtasks (Promise .then callbacks, queueMicrotask, MutationObserver) run before the next macrotask — the microtask queue drains completely before the event loop picks the next macrotask. Macrotasks (setTimeout, setInterval, I/O) are processed one per event loop tick. This is why Promise callbacks always run before setTimeout(fn, 0).

JavaScriptAdvanced

Explain how garbage collection works in JavaScript.

Answer

JavaScript uses mark-and-sweep garbage collection. The GC starts from 'roots' (global variables, current call stack), marks everything reachable, then sweeps unmarked objects as garbage. Memory leaks happen when objects are still reachable (in closures, event listeners, or global variables) but no longer needed — the GC can't collect them.

JavaScriptAdvanced

What is a memory leak in JavaScript and how do you identify one?

Answer

Unintentional retention of objects in memory that are no longer needed. Common causes: global variables, forgotten event listeners, closures holding large objects, timers not cleared. Identify with Chrome DevTools Memory tab — take heap snapshots over time and look for growing retained sizes. Fix by removing listeners, clearing timers, and nullifying references.

JavaScriptAdvanced

What is the Temporal Dead Zone (TDZ)?

Answer

The period between entering a block scope and the let/const declaration being initialized. Accessing a let or const variable in this window throws a ReferenceError. This is why let/const are 'hoisted but not initialized' — they exist in scope but aren't accessible until their declaration line executes.

JavaScriptAdvanced

What are WeakMap and WeakSet and when would you use them?

Answer

WeakMap and WeakSet hold weak references — if there are no other references to a key/value, it can be garbage collected. Unlike Map/Set, they don't prevent GC. Use WeakMap for attaching metadata to objects without preventing their cleanup (e.g., storing DOM node data). Not iterable — you can't list their contents.

JavaScriptAdvanced

Explain JavaScript's module system — CommonJS vs ES Modules.

Answer

CommonJS (Node.js default): synchronous require(), module.exports, evaluated at runtime. ES Modules: async import/export, statically analyzed (enables tree-shaking), deferred execution. ESM is the standard for browser and modern Node.js. Key difference: ESM imports are live bindings (they reflect current value); CJS exports are copies.

JavaScriptAdvanced

What is memoization and how do you implement it?

Answer

Caching the result of expensive function calls keyed by their arguments, so identical calls return cached results. Implement with a closure holding a cache object: function memoize(fn) { const cache = {}; return (...args) => { const key = JSON.stringify(args); return cache[key] !== undefined ? cache[key] : (cache[key] = fn(...args)); }; }. React's useMemo is memoization for component renders.

JavaScriptAdvanced

What is currying and what problem does it solve?

Answer

Transforming a function that takes multiple arguments into a sequence of functions each taking one argument: add

2

3

instead of add(2, 3). Enables partial application — create specialized functions from general ones: const addTax = add(0.18); addTax

100

returns 118. Useful for creating reusable, configurable utility functions.

JavaScriptAdvanced

How does JavaScript handle concurrency given it's single-threaded?

Answer

Through the event loop and async non-blocking I/O. I/O operations (network, file system) are handled by the OS or thread pool (via libuv in Node.js) outside the main thread. JavaScript's single thread only runs JS code — it delegates I/O and gets notified via callbacks when complete. For CPU-heavy work, use Web Workers (browser) or worker_threads (Node.js) for true parallelism.

JavaScriptAdvanced

What is the difference between deep copy and shallow copy in JavaScript?

Answer

Shallow copy duplicates the top-level properties — nested objects/arrays still share the same reference. Deep copy recursively duplicates everything — completely independent. Shallow: spread operator ({...obj}, [...arr]), Object.assign(). Deep: structuredClone(obj) (modern, built-in), JSON.parse(JSON.stringify(obj)) (loses functions/undefined/Date), or lodash's _.cloneDeep().

Ready for a real challenge?

Master JavaScript