JavaScript - MCQ Questions and Answers

Test your knowledge of JavaScript with these interactive multiple-choice questions.

« First 1 2 3 4 Last »
« Previous Page Next Page »

1. What is the output of typeof null in JavaScript?

  • a) "object"
  • b) "null"
  • c) "undefined"
  • d) "string"
Answer: A - typeof null returns "object" due to a historical bug in JavaScript.

2. Which method removes the last element from an array?

  • a) pop()
  • b) shift()
  • c) splice()
  • d) push()
Answer: A - pop() removes and returns the last element of an array.

3. What does NaN stand for?

  • a) Not a Number
  • b) Null and Number
  • c) No Assignment
  • d) New Array Notation
Answer: A - NaN means "Not a Number" and represents an invalid number.

4. Which operator checks both value and type equality?

  • a) ===
  • b) ==
  • c) =
  • d) !==
Answer: A - === (strict equality) checks value and type without coercion.

5. What is the scope of a variable declared with let?

  • a) Block scope
  • b) Global scope
  • c) Function scope
  • d) Module scope
Answer: A - let variables are block-scoped (e.g., inside { }).

6. Which function executes after the current call stack is empty?

  • a) setTimeout(fn, 0)
  • b) setImmediate()
  • c) process.nextTick()
  • d) Promise.resolve()
Answer: A - setTimeout(fn, 0) schedules execution after the current stack clears.

7. What is the output of [] + [] in JavaScript?

  • a) "" (empty string)
  • b) [] (empty array)
  • c) 0
  • d) NaN
Answer: A - Arrays are coerced to strings, resulting in concatenation of two empty strings.

8. Which method creates a new array with filtered elements?

  • a) filter()
  • b) map()
  • c) reduce()
  • d) forEach()
Answer: A - filter() returns a new array with elements that pass a test.

9. What does this refer to in an arrow function?

  • a) Lexical context (surrounding scope)
  • b) Global object
  • c) The function itself
  • d) Undefined
Answer: A - Arrow functions inherit this from their enclosing scope.

10. Which keyword is used to declare a constant?

  • a) const
  • b) let
  • c) var
  • d) static
Answer: A - const declares a block-scoped constant that cannot be reassigned.

11. What is the purpose of use strict?

  • a) Enforces stricter parsing and error handling
  • b) Optimizes performance
  • c) Enables ES6 features
  • d) Disables hoisting
Answer: A - "use strict" catches common coding mistakes and unsafe actions.

12. What is the output of 0.1 + 0.2 === 0.3?

  • a) false
  • b) true
  • c) NaN
  • d) TypeError
Answer: A - Due to floating-point precision issues, 0.1 + 0.2 equals 0.30000000000000004.

13. Which method converts a JSON string to an object?

  • a) JSON.parse()
  • b) JSON.stringify()
  • c) JSON.decode()
  • d) JSON.toObject()
Answer: A - JSON.parse() parses a JSON string into a JavaScript object.

14. What is a closure in JavaScript?

  • a) A function with access to its outer function's scope
  • b) A built-in method
  • c) A way to hide variables
  • d) A type of loop
Answer: A - Closures allow functions to retain access to variables from their parent scope.

15. Which symbol is used for template literals?

  • a) ` (backtick)
  • b) ' (single quote)
  • c) " (double quote)
  • d) $ (dollar sign)
Answer: A - Template literals use backticks for multi-line strings and interpolation (e.g., `Hello ${name}`).

16. What does Array.prototype.slice() do?

  • a) Returns a shallow copy of a portion of an array
  • b) Modifies the original array
  • c) Sorts the array
  • d) Merges two arrays
Answer: A - slice(start, end) returns a new array with selected elements.

17. Which event triggers when a webpage finishes loading?

  • a) DOMContentLoaded
  • b) onload
  • c) ready
  • d) loaded
Answer: A - DOMContentLoaded fires when the HTML is fully parsed (without waiting for stylesheets/images).

18. What is the purpose of Promise.all()?

  • a) Resolves when all promises in an iterable resolve
  • b) Runs promises sequentially
  • c) Rejects if any promise rejects
  • d) Both A and C
Answer: D - Promise.all() resolves when all promises succeed or rejects if any fail.

19. Which method adds elements to the beginning of an array?

  • a) unshift()
  • b) push()
  • c) concat()
  • d) shift()
Answer: A - unshift() adds elements to the start of an array.

20. What is the output of typeof function(){}?

  • a) "function"
  • b) "object"
  • c) "undefined"
  • d) "string"
Answer: A - Functions have their own type in JavaScript ("function").
« First 1 2 3 4 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.