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.
Type coercion.== converts types before comparing ("1" == 1 is true); === does not. Use === by default. The + operator joins strings if either side is a string, while -, * and / convert to numbers.
Scope.var is function-scoped and hoisted with the value undefined. let and const are block-scoped and cannot be used before their declaration (the “temporal dead zone”).
Closures. A function remembers the variables of the scope where it was created, even after that outer function has finished. Closures power callbacks, event handlers and private state.
Asynchronous code. JavaScript runs one thing at a time on a single thread. Timers, network calls and promises are queued and handled by the event loop: promise callbacks (microtasks) run before timer callbacks (tasks).
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 ===?
=== compares value and type without conversion; == converts types first
== is faster and stricter
They are identical
=== 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?
let cannot be reassigned
let is block-scoped; var is function-scoped
let creates a global variable
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?
A TypeError
A SyntaxError
The object is copied
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?
“53”
8
“8”
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?
“53”
“2”
2
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?
true
false
undefined
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?
value === NaN
value == NaN
typeof value === "NaN"
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?
undefined
5
A ReferenceError
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?
Prints undefined
Prints 5
Throws a ReferenceError
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?
Arrow functions cannot take parameters
Arrow functions do not have their own this; they use the surrounding one
Arrow functions are always asynchronous
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?
A function that closes the browser window
A function with no return value
A way to end a loop early
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?
[2, 4, 6]
12
[1, 2, 3]
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?
map
forEach
filter
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, 3, 6]
6
“123”
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, 2], 3]
An error
[3, 1, 2]
[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?
Creates a variable city with the value “Faisalabad”
Creates an object called city
Throws a SyntaxError
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?
A TypeError
null
undefined
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?
5
0
null
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?
0
true
false
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?
Pending, fulfilled, rejected
Started, running, stopped
Open, closed, error
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?
undefined
The raw value
A Promise
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?
Anywhere in any script
Inside async functions and at the top level of ES modules
Only inside loops
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");
A B C D
A C D B
A D B C
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?
const data = await res.json();
const data = res.body;
const data = JSON(res);
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?
An event that repeats itself
An animation effect
An event moving from the target element up through its ancestors
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?
Deletes the form
Stops the browser’s default action – submitting and reloading the page
Stops the event from bubbling
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?
All elements with class item
An array
The number of matches
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?
It allows several listeners for the same event
It runs faster
onclick does not work in modern browsers
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?
Moving events to a web worker
Removing all listeners
Adding one listener on a parent and using event.target to handle clicks on its children
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?
It is sent to the server with every request
It stores string values that persist after the browser is closed
It expires after one hour
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?
An object
An array
undefined
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?
Array.isArray(value)
typeof value === "array"
value.isArray()
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?
“object”
“method”
“function”
“undefined”
Show answer
Answer: C. Functions are objects in JavaScript, but typeof reports them as “function”.
35. What does "use strict"; do?
Makes code run on the server
Turns some silent mistakes into errors, such as assigning to undeclared variables
Disables all warnings
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}?
Single quotes
Double quotes
Parentheses
Backticks
Show answer
Answer: D.`Total: ${qty * price}`. Template literals can also span several lines.
37. What are JavaScript classes underneath?
Cleaner syntax over the existing prototype-based inheritance
A completely separate object system
Copies of Java classes
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()?
The window object
The function itself
undefined
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?
<script module>
<script type="module" src="app.js">
<import src="app.js">
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?
Running a function on every key press
Caching search results forever
Blocking the input field
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.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.