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 »

21. What is the output of console.log(1 + "1")?

  • a) "11"
  • b) 2
  • c) NaN
  • d) TypeError
Answer: A - JavaScript coerces numbers to strings in string concatenation (1 becomes "1").

22. Which method flattens a nested array?

  • a) flat()
  • b) flatten()
  • c) join()
  • d) concat()
Answer: A - flat() creates a new array with all sub-array elements concatenated (e.g., [1, [2]].flat()[1, 2]).

23. What does !!"false" evaluate to?

  • a) true
  • b) false
  • c) "false"
  • d) TypeError
Answer: A - The !! operator converts values to booleans. Non-empty strings are truthy.

24. Which statement about var is true?

  • a) It is function-scoped
  • b) It is block-scoped
  • c) It cannot be reassigned
  • d) It hoists to the block level
Answer: A - var declarations are function-scoped (not block-scoped like let/const).

25. What is the purpose of Object.freeze()?

  • a) Prevents modifications to an object
  • b) Deep copies an object
  • c) Converts an object to JSON
  • d) Deletes all object properties
Answer: A - Object.freeze() makes an object immutable (no additions/deletions/changes).

26. What is the output of console.log(typeof [])?

  • a) "object"
  • b) "array"
  • c) "undefined"
  • d) "list"
Answer: A - Arrays are technically objects in JavaScript.

27. Which method converts an object to an array of its values?

  • a) Object.values()
  • b) Object.keys()
  • c) Object.entries()
  • d) Array.from()
Answer: A - Object.values(obj) returns an array of the object's property values.

28. What does setTimeout() return?

  • a) A timer ID
  • b) A promise
  • c) undefined
  • d) The callback function
Answer: A - setTimeout() returns a numeric ID for clearing the timer with clearTimeout().

29. What is the result of Boolean([])?

  • a) true
  • b) false
  • c) []
  • d) TypeError
Answer: A - Empty arrays are truthy in JavaScript.

30. Which operator returns the remainder of a division?

  • a) %
  • b) /
  • c) mod
  • d) rem
Answer: A - The modulo operator (%) returns the remainder (e.g., 10 % 31).

31. What is hoisting in JavaScript?

  • a) Moving declarations to the top of their scope
  • b) A type of loop
  • c) A way to clone objects
  • d) An error-handling technique
Answer: A - Hoisting allows variables/functions to be used before declaration (but only declarations are hoisted, not initializations).

32. What is the output of console.log(3 == "3")?

  • a) true
  • b) false
  • c) NaN
  • d) TypeError
Answer: A - Loose equality (==) coerces types, so "3" becomes 3.

33. Which method removes the first element from an array?

  • a) shift()
  • b) pop()
  • c) slice()
  • d) unshift()
Answer: A - shift() removes and returns the first element (mutating the original array).

34. What does Array.isArray([]) return?

  • a) true
  • b) false
  • c) undefined
  • d) "array"
Answer: A - Array.isArray() checks if the argument is an array.

35. What is the default value of an uninitialized variable?

  • a) undefined
  • b) null
  • c) 0
  • d) ""
Answer: A - Variables declared but not assigned default to undefined.

36. Which method merges two or more arrays?

  • a) concat()
  • b) push()
  • c) merge()
  • d) join()
Answer: A - concat() returns a new array combining the originals (e.g., [1].concat([2])[1, 2]).

37. What is the output of console.log("5" - 3)?

  • a) 2
  • b) "53"
  • c) NaN
  • d) TypeError
Answer: A - The - operator coerces "5" to a number, resulting in numeric subtraction.

38. Which keyword stops a loop iteration and moves to the next?

  • a) continue
  • b) break
  • c) skip
  • d) return
Answer: A - continue skips the current iteration and proceeds to the next.

39. What is the output of console.log(+"10")?

  • a) 10
  • b) "10"
  • c) NaN
  • d) TypeError
Answer: A - The unary + operator converts strings to numbers.

40. Which method checks if an array includes a value?

  • a) includes()
  • b) contains()
  • c) indexOf()
  • d) find()
Answer: A - includes() returns true if the value exists in the array.
« 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.