AbairTechSolutionDIGITAL SOLUTIONS FOR A SMARTER TOMORROW

Menu

Home › Programming MCQs › JavaScript

JavaScript · 40 MCQs with explanations

JavaScript MCQ Questions and Answers: 40 Practice Questions with Explanations

By AbairTech Solution · From the team that builds our web apps and demos

Published by AbairTech Solution · Reviewed November 2026 · Editorial policy
How to use this page. Questions use modern JavaScript (ES2015 and later). You can check any code question by opening your browser’s developer tools (F12 → Console) and pasting the code. Answers are hidden under Show answer.

JavaScript study notes

JavaScript runs in every web browser and, through Node.js, on servers. It is forgiving, which makes it easy to start with but also full of surprises. Interview questions tend to target exactly those surprises, so these notes focus on them.

Expression Result Why
"5" + 3 “53” + with a string joins text
"5" - 3 2 – converts to numbers
0 || "x" “x” || skips any falsy value
0 ?? "x" 0 ?? skips only null and undefined
1–10 Types & scope11–20 Functions & arrays21–30 Async & DOM31–40 Modern JS

Questions 1–10: types, coercion and scope

1. What does typeof null return?

  1. “null”
  2. “undefined”
  3. “object”
  4. “number”
Show answer

Answer: C – “object”. This is a long-standing bug from the first version of JavaScript, kept for compatibility. Check for null with value === null.

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

  1. === compares value and type without conversion; == converts types first
  2. == is faster and stricter
  3. They are identical
  4. === only works with numbers
Show answer

Answer: A. 0 == "" is true but 0 === "" is false. Strict equality avoids most comparison bugs.

3. How is let different from var?

  1. let cannot be reassigned
  2. let is block-scoped; var is function-scoped
  3. let creates a global variable
  4. There is no difference
Show answer

Answer: B. A let declared inside an if or loop exists only inside that block. A var leaks out to the whole function.

4. const user = {name: "Ali"}; user.name = "Sara"; What happens?

  1. A TypeError
  2. A SyntaxError
  3. The object is copied
  4. It works: const stops reassigning the variable, not changing the object
Show answer

Answer: D. user = {} would fail. To make an object’s properties read-only, use Object.freeze(user).

5. What is "5" + 3?

  1. “53”
  2. 8
  3. “8”
  4. NaN
Show answer

Answer: A – “53”. When either operand of + is a string, JavaScript converts the other one to a string and joins them.

6. What is "5" - 3?

  1. “53”
  2. “2”
  3. 2
  4. NaN
Show answer

Answer: C – 2. The minus operator only works on numbers, so the string is converted. This asymmetry with + is a favourite interview question.

7. What does 0.1 + 0.2 === 0.3 return?

  1. true
  2. false
  3. undefined
  4. A TypeError
Show answer

Answer: B – false. Numbers are binary floating point, so the sum is 0.30000000000000004. For money, work in whole units (paisa or cents) or round the result.

8. How do you reliably check whether a value is NaN?

  1. value === NaN
  2. value == NaN
  3. typeof value === "NaN"
  4. Number.isNaN(value)
Show answer

Answer: D. NaN is the only value not equal to itself, so NaN === NaN is false. typeof NaN is “number”.

9. What does console.log(x); var x = 5; print?

  1. undefined
  2. 5
  3. A ReferenceError
  4. null
Show answer

Answer: A – undefined. The var declaration is hoisted to the top of the scope, but the assignment stays where it is.

10. What does console.log(y); let y = 5; do?

  1. Prints undefined
  2. Prints 5
  3. Throws a ReferenceError
  4. Prints null
Show answer

Answer: C. let and const are hoisted but stay in the temporal dead zone until the declaration line runs, so accessing them earlier throws an error.

Questions 11–20: functions, closures and arrays

11. What is a key difference between arrow functions and regular functions?

  1. Arrow functions cannot take parameters
  2. Arrow functions do not have their own this; they use the surrounding one
  3. Arrow functions are always asynchronous
  4. Arrow functions are hoisted
Show answer

Answer: B. This makes them ideal for callbacks inside methods, but unsuitable as object methods that need this to be the object.

12. What is a closure?

  1. A function that closes the browser window
  2. A function with no return value
  3. A way to end a loop early
  4. A function that keeps access to variables from the scope where it was created
Show answer

Answer: D. Example: function counter(){ let n = 0; return () => ++n; } – each returned function keeps its own private n.

13. What does [1, 2, 3].map(n => n * 2) return?

  1. [2, 4, 6]
  2. 12
  3. [1, 2, 3]
  4. undefined
Show answer

Answer: A. map returns a new array of the same length and does not change the original array.

14. Which array method returns only the items that pass a test?

  1. map
  2. forEach
  3. filter
  4. find
Show answer

Answer: C – filter. find returns only the first matching item (or undefined). forEach returns nothing.

15. What does [1, 2, 3].reduce((sum, n) => sum + n, 0) return?

  1. [1, 3, 6]
  2. 6
  3. “123”
  4. 0
Show answer

Answer: B – 6. reduce combines all items into one value; the 0 is the starting value of sum. Always pass a starting value to avoid errors on empty arrays.

16. Given const a = [1, 2], what is [...a, 3]?

  1. [[1, 2], 3]
  2. An error
  3. [3, 1, 2]
  4. [1, 2, 3]
Show answer

Answer: D. The spread operator expands the array’s items. It is also a quick way to copy an array (shallowly).

17. What does const { city } = { name: "Ali", city: "Faisalabad" }; do?

  1. Creates a variable city with the value “Faisalabad”
  2. Creates an object called city
  3. Throws a SyntaxError
  4. Deletes the city property
Show answer

Answer: A. Object destructuring pulls properties into variables by name. You can set defaults: const { city = "Lahore" } = obj;

18. What does user?.address?.city return if user.address is undefined?

  1. A TypeError
  2. null
  3. undefined
  4. An empty string
Show answer

Answer: C – undefined. Optional chaining stops at the first null or undefined instead of throwing “Cannot read properties of undefined”.

19. What is 0 ?? 5?

  1. 5
  2. 0
  3. null
  4. false
Show answer

Answer: B – 0. The nullish coalescing operator only falls back for null or undefined, so a real 0 is kept – useful for quantities and prices.

20. What is 0 || 5?

  1. 0
  2. true
  3. false
  4. 5
Show answer

Answer: D – 5. || returns the first truthy value, and 0 is falsy. Compare with ?? in the previous question.

Questions 21–30: promises, async/await, the event loop and the DOM

21. Which are the three states of a Promise?

  1. Pending, fulfilled, rejected
  2. Started, running, stopped
  3. Open, closed, error
  4. Waiting, done, cancelled
Show answer

Answer: A. Once fulfilled or rejected (together called “settled”), a promise never changes state again.

22. What does an async function always return?

  1. undefined
  2. The raw value
  3. A Promise
  4. A callback
Show answer

Answer: C. If you return 5, callers receive a promise that resolves to 5. Thrown errors become rejected promises.

23. Where can you use await?

  1. Anywhere in any script
  2. Inside async functions and at the top level of ES modules
  3. Only inside loops
  4. Only with setTimeout
Show answer

Answer: B. Wrap awaits in try/catch to handle rejected promises.

24. In what order does this print? console.log("A"); setTimeout(() => console.log("B"), 0); Promise.resolve().then(() => console.log("C")); console.log("D");

  1. A B C D
  2. A C D B
  3. A D B C
  4. A D C B
Show answer

Answer: D – A D C B. Synchronous code runs first (A, D). Then the microtask queue (promise callback C) runs before the task queue (timer B), even with a 0 ms delay.

25. const res = await fetch(url); How do you get the JSON body?

  1. const data = await res.json();
  2. const data = res.body;
  3. const data = JSON(res);
  4. const data = res.json;
Show answer

Answer: A. res.json() also returns a promise. Note that fetch does not reject on HTTP errors like 404 – check res.ok.

26. What is event bubbling?

  1. An event that repeats itself
  2. An animation effect
  3. An event moving from the target element up through its ancestors
  4. An event that fires before the page loads
Show answer

Answer: C. A click on a button also triggers click listeners on its parent div, body and document, unless event.stopPropagation() is called.

27. What does event.preventDefault() do on a form’s submit event?

  1. Deletes the form
  2. Stops the browser’s default action – submitting and reloading the page
  3. Stops the event from bubbling
  4. Clears all inputs
Show answer

Answer: B. This is how single-page forms validate and send data with fetch instead of a full page reload.

28. What does document.querySelector(".item") return?

  1. All elements with class item
  2. An array
  3. The number of matches
  4. The first element that matches, or null
Show answer

Answer: D. Use querySelectorAll for all matches; it returns a NodeList.

29. Why prefer addEventListener over assigning element.onclick?

  1. It allows several listeners for the same event
  2. It runs faster
  3. onclick does not work in modern browsers
  4. It prevents bubbling
Show answer

Answer: A. Assigning onclick again replaces the previous handler. addEventListener also supports options such as { once: true }.

30. What is event delegation?

  1. Moving events to a web worker
  2. Removing all listeners
  3. Adding one listener on a parent and using event.target to handle clicks on its children
  4. Firing an event manually
Show answer

Answer: C. It works for elements added later and uses less memory than one listener per row in a long list.

Questions 31–40: modern JavaScript and the browser

31. Which statement about localStorage is true?

  1. It is sent to the server with every request
  2. It stores string values that persist after the browser is closed
  3. It expires after one hour
  4. It is a safe place to store passwords
Show answer

Answer: B. Store objects with JSON.stringify and read them with JSON.parse. Any script on the page can read it, so never store secrets there.

32. What does JSON.stringify({a: 1}) return?

  1. An object
  2. An array
  3. undefined
  4. The string ‘{“a”:1}’
Show answer

Answer: D. It converts values into JSON text for sending or storing. Functions and undefined properties are left out.

33. How do you reliably check whether a value is an array?

  1. Array.isArray(value)
  2. typeof value === "array"
  3. value.isArray()
  4. value instanceof Object
Show answer

Answer: A. typeof [] returns “object”, so typeof cannot tell arrays apart from other objects.

34. What does typeof function(){} return?

  1. “object”
  2. “method”
  3. “function”
  4. “undefined”
Show answer

Answer: C. Functions are objects in JavaScript, but typeof reports them as “function”.

35. What does "use strict"; do?

  1. Makes code run on the server
  2. Turns some silent mistakes into errors, such as assigning to undeclared variables
  3. Disables all warnings
  4. Requires TypeScript
Show answer

Answer: B. ES modules and class bodies are strict automatically.

36. Which syntax creates a template literal that can include ${variables}?

  1. Single quotes
  2. Double quotes
  3. Parentheses
  4. Backticks
Show answer

Answer: D. `Total: ${qty * price}`. Template literals can also span several lines.

37. What are JavaScript classes underneath?

  1. Cleaner syntax over the existing prototype-based inheritance
  2. A completely separate object system
  3. Copies of Java classes
  4. Compiled C++ objects
Show answer

Answer: A. Methods defined in a class live on its prototype, shared by all instances.

38. In strict mode, what is this inside a regular function called on its own, like show()?

  1. The window object
  2. The function itself
  3. undefined
  4. An empty object
Show answer

Answer: C – undefined. In non-strict scripts it falls back to the global object. this depends on how a function is called, not where it is written.

39. How do you load an ES module in a web page?

  1. <script module>
  2. <script type="module" src="app.js">
  3. <import src="app.js">
  4. require("app.js") in HTML
Show answer

Answer: B. Modules use export and import, run in strict mode and are deferred by default.

40. What is debouncing, for example on a search box?

  1. Running a function on every key press
  2. Caching search results forever
  3. Blocking the input field
  4. Waiting until the user stops typing for a short time before running the function
Show answer

Answer: D. It avoids sending a server request on every key press. Throttling, by contrast, runs a function at most once per time interval.

What to practise next

Questions 5–10, 19–20 and 24 cover the parts of JavaScript that surprise even experienced developers. If you got them right, you understand the language well. Next, try the HTML & CSS MCQs to round out front-end skills, or the Python MCQs. All topics are on the Programming MCQs hub.

Need a website or web application?

AbairTech designs and builds fast websites, web apps and dashboards, with APIs and mobile apps where you need them.

Website DevelopmentMore MCQs