Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Interview questions › JavaScript

JavaScript interview questions

1. var vs let vs const?

Easy

var is function-scoped, hoisted (initialised as undefined) and can be redeclared. let and const are block-scoped and in the temporal dead zone until declared. const cannot be reassigned (but objects it points to can still be mutated).

2. What is hoisting?

Medium

JavaScript moves declarations to the top of their scope before execution. var variables are hoisted with value undefined; function declarations are hoisted fully (can be called before they appear); let/const/classes are hoisted but not initialised (TDZ).

3. What is the temporal dead zone?

Medium

The period between entering a scope and the let/const declaration line. Accessing the variable there throws a ReferenceError.

4. == vs ===?

Easy

== compares after type coercion ("5" == 5 is true). === compares value and type without coercion ("5" === 5 is false). Prefer ===.

5. What are the data types in JavaScript?

Easy

Primitives: string, number, bigint, boolean, undefined, null, symbol. Everything else is an object (including arrays and functions).

6. null vs undefined?

Easy

undefined means a variable was declared but not assigned (or a missing property). null is an intentional "no value" set by the programmer. typeof null is "object" (a historical bug).

7. What is a closure?

Medium

A function that remembers the variables of the scope where it was created, even after that scope has finished. Used for data privacy, function factories, memoisation and callbacks.

function counter() { let c = 0; return () => ++c; }
const next = counter(); next(); // 1

8. What is the `this` keyword?

Medium

Its value depends on how a function is called: method call → the object; plain call → undefined in strict mode (global object otherwise); new → the new object; call/apply/bind → the given value. Arrow functions don't have their own this — they use the surrounding one.

9. Arrow functions vs regular functions?

Medium

Arrow functions have shorter syntax, no own this, arguments or prototype, and can't be used as constructors with new. They are great for callbacks; use regular functions for object methods that need this.

10. call vs apply vs bind?

Medium

All set this explicitly. call(thisArg, a, b) invokes immediately with arguments listed; apply(thisArg, [a, b]) invokes with an array; bind(thisArg) returns a new function with this fixed, to call later.

11. What is the event loop?

Hard

JavaScript runs on a single thread. Synchronous code runs on the call stack; async callbacks wait in queues. When the stack is empty, the event loop first runs all microtasks (promises) and then one macrotask (setTimeout, I/O), repeatedly.

12. Microtasks vs macrotasks?

Hard

Microtasks: promise callbacks, queueMicrotask, MutationObserver — run right after the current script, before rendering. Macrotasks: setTimeout, setInterval, I/O, UI events. So Promise.resolve().then(...) runs before setTimeout(..., 0).

13. What is a Promise?

Easy

An object representing the eventual result of an async operation, in one of three states: pending, fulfilled or rejected. You handle results with .then(), errors with .catch(), and cleanup with .finally().

14. What is async/await?

Easy

Syntax on top of promises: an async function always returns a promise, and await pauses inside it until a promise settles, letting you write async code like synchronous code. Errors are handled with try/catch.

15. Promise.all vs allSettled vs race vs any?

Medium

all waits for all and rejects on the first failure. allSettled waits for all and reports each result. race settles with the first settled promise. any fulfils with the first success and rejects only if all fail.

16. What is callback hell?

Easy

Deeply nested callbacks for sequential async operations, making code hard to read and handle errors. Solved with promises chaining and async/await.

17. What is the prototype chain?

Medium

Every object has an internal link to a prototype object. When a property isn't found on the object, JavaScript looks it up the prototype chain until null. This is how inheritance works in JS (classes are syntax sugar over it).

18. What are classes in JavaScript?

Easy

ES6 class syntax creates constructor functions with methods on the prototype, supports extends, super, static methods and private fields (#field). Under the hood it still uses prototypal inheritance.

19. What is destructuring?

Easy

Unpacking values from arrays or properties from objects into variables: const { name, age } = user; const [first, second] = list; with defaults and renaming support.

20. Spread vs rest operator?

Easy

Both use .... Spread expands an iterable/object: [...a, ...b], { ...obj, x: 1 }. Rest collects remaining items into an array/object: function f(...args), const { a, ...others } = obj.

21. Shallow copy vs deep copy in JS?

Medium

Spread and Object.assign make shallow copies — nested objects are still shared. For a deep copy use structuredClone(obj) (or a library); JSON.parse(JSON.stringify()) works only for plain JSON data.

22. map vs forEach vs filter vs reduce?

Easy

forEach just loops (returns undefined). map returns a new array of transformed items. filter returns items that pass a test. reduce combines all items into one value (sum, object, etc.).

23. What is debouncing vs throttling?

Medium

Debouncing runs a function only after events stop for a delay (e.g. search after the user stops typing). Throttling runs it at most once per interval (e.g. scroll handler every 200 ms).

24. What is event bubbling and capturing?

Medium

When an event occurs, it first travels down from the window to the target (capturing), then back up to ancestors (bubbling). Listeners run in the bubbling phase by default; stopPropagation() stops it.

25. What is event delegation?

Medium

Attaching one listener to a parent instead of many children, and using event.target to see which child was clicked. It works because events bubble, saves memory and handles dynamically added elements.

26. What is the DOM?

Easy

The Document Object Model is a tree representation of the HTML page that JavaScript can read and modify (document.querySelector, createElement, addEventListener).

27. localStorage vs sessionStorage vs cookies?

Easy

localStorage: ~5–10 MB, persists until cleared. sessionStorage: per tab, cleared when the tab closes. Cookies: ~4 KB, sent to the server with every request, can have expiry and HttpOnly/Secure flags.

28. What is CORS?

Medium

Cross-Origin Resource Sharing is a browser security mechanism: a page can call an API on another origin only if the server responds with headers like Access-Control-Allow-Origin. Preflight OPTIONS requests are sent for non-simple requests.

29. What is a higher-order function?

Easy

A function that takes a function as an argument or returns a function — e.g. map, filter, setTimeout, or a function factory.

30. What is currying?

Medium

Transforming a function of many arguments into a chain of functions taking one argument each: add(1)(2)(3). Useful for partial application and reusable helpers.

31. What are pure functions?

Easy

Functions that always return the same output for the same input and have no side effects (don't modify outside state). They are easy to test and are the foundation of React and Redux patterns.

32. What is memoization in JS?

Medium

Caching a function's results by its arguments so repeated calls return instantly — e.g. using a Map inside a closure. React has useMemo and React.memo for the same idea.

33. What are ES modules?

Easy

The standard module system using import and export. Modules have their own scope, are strict by default, and are loaded statically, enabling tree shaking. CommonJS (require) is the older Node.js system.

34. What is NaN? How do you check for it?

Easy

NaN means "Not a Number", the result of invalid math like 0/0. It is the only value not equal to itself (NaN === NaN is false). Check with Number.isNaN(x).

35. What is the difference between Map and Object?

Medium

A Map allows keys of any type, keeps insertion order, has a size property and is optimised for frequent additions/removals. Objects have string/symbol keys and a prototype that may contain inherited keys.

36. What are Set and WeakMap?

Hard

A Set stores unique values of any type. A WeakMap has object keys held weakly, so entries disappear when the key object is garbage-collected — useful for private data and caches.

37. What is strict mode?

Easy

"use strict" turns silent errors into thrown errors (e.g. assigning undeclared variables), makes this undefined in plain functions and disallows some unsafe syntax. Modules and classes are strict by default.

38. What is the output of `console.log(0.1 + 0.2 === 0.3)`?

Easy

false. Numbers are IEEE-754 doubles, so 0.1 + 0.2 equals 0.30000000000000004. Compare with a tolerance: Math.abs(a - b) < Number.EPSILON.

39. What is typeof for arrays and how do you check for an array?

Easy

typeof [] is "object". Use Array.isArray(value).

40. What is a generator function?

Hard

A function declared with function* that can pause with yield and resume later, returning an iterator. Useful for lazy sequences and custom iteration.

41. What is Node.js?

Easy

A JavaScript runtime built on Chrome's V8 engine that runs JS on the server. It uses an event-driven, non-blocking I/O model, making it efficient for APIs and real-time apps.

42. What is Express.js?

Easy

A minimal web framework for Node.js that provides routing, middleware and helpers to build REST APIs and web servers quickly.

43. What is middleware in Express?

Medium

A function (req, res, next) that runs during the request lifecycle — for logging, authentication, parsing bodies, error handling. It either ends the response or calls next() to pass control.

44. What is React?

Easy

A JavaScript library for building user interfaces from reusable components. It uses a virtual DOM and a declarative style: you describe the UI for a given state and React updates the real DOM efficiently.

45. What is the virtual DOM?

Medium

A lightweight in-memory copy of the DOM. When state changes, React builds a new virtual tree, diffs it with the previous one (reconciliation) and applies only the minimal real DOM updates.

46. State vs props in React?

Easy

Props are read-only inputs passed from parent to child. State is data owned by a component that can change over time (via useState), causing a re-render.

47. What are React hooks? Name common ones.

Easy

Functions that let function components use state and lifecycle features: useState, useEffect, useContext, useRef, useMemo, useCallback, useReducer. Rules: call them at the top level and only in React functions.

48. What does useEffect do?

Medium

Runs side effects (data fetching, subscriptions, timers) after render. The dependency array controls when it re-runs; the returned function cleans up before the next run or on unmount.

49. Why are keys needed in React lists?

Medium

Keys give each list item a stable identity so React can match items between renders during reconciliation. Use unique IDs, not array indexes, when items can be reordered or removed.

50. What is the difference between controlled and uncontrolled components?

Medium

In a controlled component, form input values come from React state and change through onChange. In an uncontrolled component, the DOM keeps the value and you read it with a ref.

More topics