JavaScript - MCQ Questions and Answers

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

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

41. What is the output of console.log(typeof NaN)?

  • a) "number"
  • b) "NaN"
  • c) "undefined"
  • d) "string"
Answer: A - Despite its name ("Not a Number"), NaN is technically of type "number".

42. Which method creates a new array with transformed elements?

  • a) map()
  • b) filter()
  • c) reduce()
  • d) forEach()
Answer: A - map() applies a function to each element and returns a new array.

43. What does document.querySelector() return if no match is found?

  • a) null
  • b) undefined
  • c) false
  • d) throws an error
Answer: A - It returns null when no matching element exists in the DOM.

44. Which operator is used for exponentiation in JavaScript?

  • a) **
  • b) ^
  • c) Math.pow()
  • d) //
Answer: A - The exponentiation operator (**) raises a base to a power (e.g., 2 ** 38).

45. What is the output of console.log([] == ![])?

  • a) true
  • b) false
  • c) TypeError
  • d) undefined
Answer: A - Due to type coercion, both sides evaluate to false ([] becomes "" then 0, ![] becomes false then 0).

46. Which method converts an array to a string?

  • a) join()
  • b) toString()
  • c) String()
  • d) All of the above
Answer: D - All three methods can convert arrays to strings, but join() allows custom separators.

47. What is the purpose of the finally block in a try-catch?

  • a) Executes regardless of try/catch outcome
  • b) Handles uncaught errors
  • c) Replaces the catch block
  • d) Stops execution
Answer: A - finally runs whether the try succeeds or fails, useful for cleanup.

48. What does console.log(!!0) output?

  • a) false
  • b) true
  • c) 0
  • d) "0"
Answer: A - 0 is falsy, so !!0 converts it to boolean false.

49. Which method returns the first index where an element is found in an array?

  • a) indexOf()
  • b) findIndex()
  • c) search()
  • d) Both A and B
Answer: D - Both indexOf() (for primitive values) and findIndex() (for complex conditions) return the first matching index.

50. What is the output of console.log("b" + "a" + +"a" + "a")?

  • a) "baNaNa"
  • b) "baaa"
  • c) "baundefineda"
  • d) TypeError
Answer: A - The unary + coerces "a" to NaN, resulting in concatenation: "b" + "a" + "NaN" + "a".

51. Which statement about const is true?

  • a) It must be initialized during declaration
  • b) It can be reassigned
  • c) It is hoisted to the global scope
  • d) It allows duplicate declarations
Answer: A - const requires initialization and cannot be reassigned (though object properties can be modified).

52. What is the output of console.log(0.1 + 0.2)?

  • a) 0.30000000000000004
  • b) 0.3
  • c) NaN
  • d) "0.30"
Answer: A - Floating-point precision issues cause this result (not exactly 0.3).

53. Which method checks if an object has a specific property?

  • a) hasOwnProperty()
  • b) in operator
  • c) Object.keys()
  • d) Both A and B
Answer: D - hasOwnProperty() checks for non-inherited properties, while in includes inherited ones.

54. What does console.log(typeof (() => {})) output?

  • a) "function"
  • b) "object"
  • c) "arrow"
  • d) "undefined"
Answer: A - Arrow functions are still of type "function".

55. Which method delays execution until the call stack is clear?

  • a) setTimeout(fn, 0)
  • b) setImmediate() (Node.js only)
  • c) process.nextTick() (Node.js only)
  • d) All of the above
Answer: D - All three delay execution, but prioritization differs (nextTick() > setImmediate() > setTimeout() in Node.js).

56. What is the output of console.log(new Boolean(false))?

  • a) Boolean object (truthy)
  • b) false
  • c) "false"
  • d) TypeError
Answer: A - new Boolean(false) creates a truthy object wrapper, not a primitive.

57. Which method creates a shallow copy of an object?

  • a) Object.assign({}, obj)
  • b) {...obj} (spread operator)
  • c) JSON.parse(JSON.stringify(obj)) (deep copy)
  • d) Both A and B
Answer: D - Both Object.assign() and the spread operator create shallow copies.

58. What is the output of console.log(1 < 2 < 3)?

  • a) true
  • b) false
  • c) TypeError
  • d) undefined
Answer: A - Evaluated as (1 < 2) < 3true < 31 < 3true.

59. Which method reverses an array in place?

  • a) reverse()
  • b) toReversed() (ES2023)
  • c) sort() with a custom comparator
  • d) None of the above
Answer: A - reverse() mutates the original array. toReversed() (new in ES2023) returns a new reversed array.

60. What does console.log(+"") output?

  • a) 0
  • b) NaN
  • c) ""
  • d) TypeError
Answer: A - The unary + coerces an empty string to 0.
« First 2 3 4 5 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.