javascript MCQs with Answers
1. What does the following code return? console.log([1, 2, 3].map(num => num * 2));
- A. [2, 4, 6]
- B. [1, 4, 9]
- C. [1, 2, 3]
- D. [2, 3, 4]
Correct: A
Explanation: map() transforms each element.
2. What will console.log([...new Set([1,2,2,3])]); return?
- A. [1,2,3]
- B. [1,2,2,3]
- C. Set {1,2,3}
- D. {1,2,3}
Correct: A
Explanation: Set removes duplicate values.
3. Which storage API stores data persistently?
- A. localStorage
- B. sessionStorage
- C. cookies
- D. All of the above
Correct: D
Explanation: All can store data persistently with different scopes.
4. Which event is triggered when an input field loses focus?
- A. click
- B. blur
- C. focus
- D. change
Correct: B
Explanation: blur event fires when element loses focus.
5. How do you deep clone an object in JavaScript?
- A. Object.assign({}, obj)
- B. JSON.parse(JSON.stringify(obj))
- C. obj.clone()
- D. obj.copy()
Correct: B
Explanation: JSON methods create a deep copy (with limitations).
6. What will 2 + "2" evaluate to?
- A. 4
- B. "22"
- C. NaN
- D. Error
Correct: B
Explanation: Number is coerced to string, resulting in string concatenation.
7. What will console.log(parseInt("10px")) return?
- A. 10
- B. NaN
- C. "10px"
- D. Error
Correct: A
Explanation: parseInt stops parsing at first non-number.
8. Which method in a class is used to call the parent class constructor?
- A. parent()
- B. super()
- C. this()
- D. constructor()
Correct: B
Explanation: super() is used to call the parent class constructor.
9. What will be the output of this code? console.log("A"); setTimeout(() => console.log("B"), 0); console.log("C");
- A. A B C
- B. A C B
- C. B A C
- D. C A B
Correct: B
Explanation: setTimeout runs after synchronous code.
10. Which of the following executes first in the event loop?
- A. setTimeout()
- B. setInterval()
- C. Promise.resolve().then()
- D. console.log()
Correct: D
Explanation: Synchronous code executes first.
11. Output of this loop: for(;;){ console.log("Loop") }
- A. Infinite Loop
- B. Loop will not execute
- C. Error
- D. Only run once
Correct: A
Explanation: A for loop without conditions runs infinitely.
12. Which of the following is NOT an immutable operation?
- A. map()
- B. filter()
- C. splice()
- D. concat()
Correct: C
Explanation: splice() mutates the original array.
13. What is the event loop in JavaScript?
- A. A process that handles function calls
- B. A mechanism that allows async operations
- C. A feature that prevents infinite loops
- D. A method to execute code
Correct: B
Explanation: Event loop manages asynchronous execution.
14. Which method sends a GET request using Fetch API?
- A. fetch(url)
- B. fetch(url, { method: 'GET' })
- C. Both a and b
- D. None of the above
Correct: C
Explanation: Fetch uses GET by default and also supports explicit method.
15. What is a closure in JavaScript?
- A. A function inside another function with access to parent scope
- B. A block of code that runs automatically
- C. A way to define private variables
- D. Both a and c
Correct: D
Explanation: Closures allow scope access and private variables.
16. How do you check if a variable is an array?
- A. typeof x === "array"
- B. x.isArray()
- C. Array.isArray(x)
- D. x instanceof Object
Correct: C
Explanation: Array.isArray is the recommended approach.
17. What is the output of console.log(2 + "2" - 1);?
- A. "21"
- B. 21
- C. "22"
- D. 1
Correct: B
Explanation: "2" + 2 becomes "22", then minus 1 gives 21.
18. What does the spread operator ... do?
- A. Combines arrays
- B. Expands iterable elements
- C. All of the above
- D.
Correct: C
Explanation: Spread expands iterables and helps combine arrays.
19. Which function generates a random number between 0 and 1?
- A. Math.random()
- B. random()
- C. generateRandom()
- D. Math.rand()
Correct: A
Explanation: Math.random() returns a number between 0 and 1.
20. Which method is used to filter elements from an array?
- A. map()
- B. filter()
- C. reduce()
- D. slice()
Correct: B
Explanation: filter() returns elements matching a condition.
21. What will console.log(typeof function(){}()); return?
- A. "function"
- B. "object"
- C. "undefined"
- D. "null"
Correct: C
Explanation: The function executes and returns undefined by default.
22. How can you set an interval in JavaScript?
- A. setTimeout()
- B. setInterval()
- C. setRepeat()
- D. Interval()
Correct: B
Explanation: setInterval executes a function repeatedly.
23. What does navigator.geolocation.getCurrentPosition() do?
- A. Gets user’s IP address
- B. Gets user’s location
- C. Opens a Google Maps page
- D. None of the above
Correct: B
Explanation: It retrieves the geographical position of the user.
24. Which of the following is true about const variables?
- A. Their values cannot be changed
- B. They cannot be reassigned
- C. They are always immutable
- D. All of the above
Correct: B
Explanation: const prevents reassignment, not mutation.
25. Use of await keyword?
- A. Waits for async operation before continuing
- B. Make promise
- C. Stops execution
- D. All of the above
Correct: A
Explanation: await pauses execution until the promise resolves.
26. Output of: console.log(x); let x = 5;
- A. 5
- B. undefined
- C. ReferenceError
- D. NaN
Correct: C
Explanation: let variables are in temporal dead zone until initialized.
27. Which loop is guaranteed to execute at least once?
- A. for loop
- B. while loop
- C. do-while loop
- D. None of the above
Correct: C
Explanation: do-while executes once before checking condition.
28. How to make immutable object in JavaScript?
- A. final var ={name:'Anil'}
- B. const user={name:'Anil'}
- C. var user={name:'Anil'}; Object.freeze(user);
- D. There is no way to make immutable object
Correct: C
Explanation: Object.freeze() prevents modification of object properties.
29. Which of the following is a falsy value in JavaScript?
- A. "false"
- B. "0"
- C. undefined
- D. "undefined"
Correct: C
Explanation: undefined is a falsy value.
30. Which function combines array elements into a single value?
- A. reduce()
- B. map()
- C. join()
- D. concat()
Correct: A
Explanation: reduce() accumulates values.
31. Which storage API stores data persistently?
- A. localStorage
- B. sessionStorage
- C. cookies
- D. All of the above
Correct: D
Explanation: All can store data persistently with different scopes.
32. Which keyword is used to create a class in JavaScript?
- A. class
- B. function
- C. Class
- D. new Class
Correct: A
Explanation: class keyword defines a class.
33. Which of the following is NOT a primitive data type in JavaScript?
- A. Number
- B. String
- C. Object
- D. Symbol
Correct: C
Explanation: Object is a non-primitive data type.
34. Which method removes an element from an array?
- A. splice()
- B. slice()
- C. remove()
- D. delete()
Correct: A
Explanation: splice() mutates the array and removes elements.
35. What is the result of typeof "5" === "5"?
Correct: B
Explanation: typeof "5" returns "string", which is not equal to "5".
36. What does !!"false" evaluate to?
- A. true
- B. false
- C. undefined
- D. Error
Correct: A
Explanation: Non-empty strings are truthy. Double negation converts it to boolean true.
37. What is the output of console.log(typeof([]));?
- A. "object"
- B. "array"
- C. "undefined"
- D. "null"
Correct: A
Explanation: Arrays are objects in JavaScript.
38. Output of the following code?
- A. ['apple', 'banana', 'grapes']
- B. {'apple', 'banana', 'grapes'}
- C. apple banana grapes
- D. apple
Correct: C
Explanation: Spread operator expands arguments and logs them as separate values.
39. Which method adds an element at the end of an array?
- A. push()
- B. pop()
- C. shift()
- D. unshift()
Correct: A
Explanation: push() appends elements.
40. Which keyword is used for inheritance in JavaScript?
- A. implements
- B. extends
- C. inherits
- D. prototype
Correct: B
Explanation: extends enables class inheritance.
41. Which API is used for making HTTP requests in JavaScript?
- A. XMLHttpRequest
- B. Fetch API
- C. Axios
- D. All of the above
Correct: D
Explanation: All listed options can be used to make HTTP requests.
42. Which operator is used for strict equality in JavaScript?
Correct: B
Explanation: !== checks both value and type and ensures they are not equal.
43. Which of the following is true about closures?
- A. Access to own scope
- B. Access to parent scope
- C. Access to global scope
- D. All of the above
Correct: D
Explanation: Closures have access to all outer scopes.
44. How do you remove first 2 elements of an array?
- A. pop()
- B. shift()
- C. unshift()
- D. splice()
Correct: D
Explanation: splice() can remove multiple elements.
45. Which of the following is a falsy value in JavaScript?
- A. "false"
- B. "0"
- C. undefined
- D. "undefined"
Correct: C
Explanation: undefined is a falsy value.
46. What does event.preventDefault() do?
- A. Stops the default action of an event
- B. Stops event propagation
- C. Prevents event from being attached
- D. None of the above
Correct: A
Explanation: It prevents default browser behavior.
47. What does navigator.geolocation.getCurrentPosition() do?
- A. Gets user’s IP address
- B. Gets user’s location
- C. Opens a Google Maps page
- D. None of the above
Correct: B
Explanation: It retrieves the geographical position of the user.
48. What will this code output?
- A. 1 2
- B. 0 1
- C. 1 1
- D. Error
Correct: A
Explanation: Closure preserves count variable between calls.
49. What will console.log(..."Hello"); output?
- A. "H e l l o"
- B. ["H","e","l","l","o"]
- C. Syntax Error
- D. undefined
Correct: A
Explanation: Spread operator expands iterable characters.
50. How do you access a property in an object?
- A. obj[property]
- B. obj.property
- C. Both a and b
- D. None
Correct: C
Explanation: Dot and bracket notation both work.
51. What is the event loop in JavaScript?
- A. A process that handles function calls
- B. A mechanism that allows async operations
- C. A feature that prevents infinite loops
- D. A method to execute code
Correct: B
Explanation: Event loop manages asynchronous execution.
52. Which method is used to generate a custom error?
- A. throw new Error()
- B. console.error()
- C. generateError()
- D. raiseError()
Correct: A
Explanation: throw new Error creates custom errors.
53. Which of the following is NOT an immutable operation?
- A. map()
- B. filter()
- C. splice()
- D. concat()
Correct: C
Explanation: splice() mutates the original array.
54. What does the following code return? console.log([1, 2, 3].map(num => num * 2));
- A. [2, 4, 6]
- B. [1, 4, 9]
- C. [1, 2, 3]
- D. [2, 3, 4]
Correct: A
Explanation: map() transforms each element.
55. What is the purpose of the constructor method in a class?
- A. Create private variables
- B. Initialize object properties
- C. Call another class
- D. None
Correct: B
Explanation: Constructor initializes object properties.
56. Which method adds an event listener to an element?
- A. element.addEventListener()
- B. element.attachEvent()
- C. element.onEvent()
- D. element.setEventListener()
Correct: A
Explanation: addEventListener is the standard method.
57. What will be the output of this code? console.log("A"); setTimeout(() => console.log("B"), 0); console.log("C");
- A. A B C
- B. A C B
- C. B A C
- D. C A B
Correct: B
Explanation: setTimeout runs after synchronous code.
58. Which JavaScript engine is used in Google Chrome?
- A. SpiderMonkey
- B. V8
- C. Chakra
- D. Nitro
Correct: B
Explanation: Google Chrome uses the V8 engine.
59. Which of the following is a best practice in JavaScript?
- A. Using == instead of ===
- B. Avoiding global variables
- C. Using var instead of let
- D. Nesting loops deeply
Correct: B
Explanation: Avoiding globals prevents bugs and conflicts.
60. Which method in a class is used to call the parent class constructor?
- A. parent()
- B. super()
- C. this()
- D. constructor()
Correct: B
Explanation: super() is used to call the parent class constructor.
61. Which statement is used for error handling in JavaScript?
- A. try...catch
- B. throw
- C. finally
- D. All of the above
Correct: D
Explanation: All are used in error handling.
62. What will finally do in a try-catch-finally block?
- A. Execute only if no error occurs
- B. Execute only if error occurs
- C. Always execute
- D. None
Correct: C
Explanation: finally always runs.
63. Can we connect JavaScript directly with an actual database? Give the reason.
- A. Yes
- B. No
- C. Sometimes
- D. Some Database
Correct: B
Explanation: JavaScript running in the browser cannot directly connect to databases due to security reasons. Direct database access would expose credentials and allow unauthorized operations. A backend server (such as Node.js, Java, or Spring Boot) is required to securely interact with the database.
64. What is the output of console.log(2 + "2" - 1);?
- A. "21"
- B. 21
- C. "22"
- D. 1
Correct: B
Explanation: "2" + 2 becomes "22", then minus 1 gives 21.
65. What is the default return value of a function if no return is used?
- A. null
- B. undefined
- C. false
- D. 0
Correct: B
Explanation: JavaScript functions return undefined by default.
66. Which statement about JavaScript classes is true?
- A. They support multiple inheritance
- B. They are syntactic sugar over prototypes
- C. They can be redeclared
- D. They do not support inheritance
Correct: B
Explanation: JavaScript classes are syntactic sugar over prototype-based inheritance.
67. What will console.log(typeof function(){}); return?
- A. "function"
- B. "object"
- C. "undefined"
- D. "null"
Correct: A
Explanation: Functions have their own typeof result: "function".
68. What will console.log([] == false); return?
- A. true
- B. false
- C. undefined
- D. Error
Correct: A
Explanation: Empty array coerces to false in loose comparison.
69. Which of the following is NOT a primitive data type in JavaScript?
- A. Number
- B. String
- C. Object
- D. Symbol
Correct: C
Explanation: Object is a non-primitive data type.
70. What is the purpose of JavaScript Promises?
- A. Handle synchronous code
- B. Handle asynchronous operations
- C. Block execution until resolved
- D. Replace all callbacks
Correct: B
Explanation: Promises are used to handle asynchronous operations.
71. How do you create an object in JavaScript?
- A. let obj = {};
- B. let obj = new Object();
- C. Both a and b
- D. None
Correct: C
Explanation: Both syntaxes are valid.
72. How do you define an arrow function?
- A. const add = (a,b) => a+b;
- B. const add = function(a,b){return a+b;}
- C. Both
- D. None
Correct: A
Explanation: Arrow functions use => syntax.
73. Which method removes an element from an array?
- A. splice()
- B. slice()
- C. remove()
- D. delete()
Correct: A
Explanation: splice() mutates the array and removes elements.
74. Which statement about var and let is true?
- A. Both are function-scoped
- B. var is function-scoped, let is block-scoped
- C. Both are block-scoped
- D. var allows redeclaration, let doesn’t
Correct: B
Explanation: var is function-scoped, let is block-scoped.
75. What is localStorage used for?
- A. Storing session data
- B. Storing data persistently in the browser
- C. Making API requests
- D. Caching images
Correct: B
Explanation: localStorage persists data even after browser restart.
76. Which method selects an element by ID?
- A. document.getElementofId()
- B. document.getElementById()
- C. document.selectElementById()
- D. document.selectById()
Correct: B
Explanation: getElementById is the correct DOM method.
77. Which method converts a string into a number?
- A. parseInt()
- B. Number()
- C. Unary +
- D. All of the above
Correct: D
Explanation: All methods can convert strings to numbers.
78. What will typeof null return?
- A. "null"
- B. "object"
- C. "undefined"
- D. "string"
Correct: B
Explanation: Due to a historical bug in JavaScript, typeof null returns "object".
79. How can you set an interval in JavaScript?
- A. setTimeout()
- B. setInterval()
- C. setRepeat()
- D. Interval()
Correct: B
Explanation: setInterval executes a function repeatedly.
80. What will console.log(typeof NaN); print?
- A. "number"
- B. "NaN"
- C. "undefined"
- D. "object"
Correct: A
Explanation: NaN is considered a Number type in JavaScript.
81. Which of the following executes first in the event loop?
- A. setTimeout()
- B. setInterval()
- C. Promise.resolve().then()
- D. console.log()
Correct: D
Explanation: Synchronous code executes first.
82. Which queue does setTimeout() use in JavaScript?
- A. Microtask queue
- B. Callback queue
- C. Event loop queue
- D. Execution stack
Correct: B
Explanation: setTimeout uses the callback (macrotask) queue.
83. Which of the following is a best practice in JavaScript?
- A. Using == instead of ===
- B. Avoiding global variables
- C. Using var instead of let
- D. Nesting loops deeply
Correct: B
Explanation: Avoiding globals prevents bugs and conflicts.
84. Which method executes a function repeatedly with a time interval?
- A. setInterval()
- B. setTimeout()
- C. repeat()
- D. setLoop()
Correct: A
Explanation: setInterval runs repeatedly at intervals.
85. What will console.log([] == false); return?
- A. true
- B. false
- C. undefined
- D. Error
Correct: A
Explanation: Empty array coerces to false in loose comparison.
86. Which statement about arrow functions is true?
- A. They do not bind this
- B. They can be constructors
- C. They have prototype
- D. They support arguments
Correct: A
Explanation: Arrow functions do not have their own this.
87. What will console.log([1,2,3].length); return?
- A. 2
- B. 3
- C. 4
- D. undefined
Correct: B
Explanation: Array length counts elements.
88. What will console.log(x); inside a try block do if x is undefined?
- A. Print undefined
- B. Print null
- C. Throw a ReferenceError
- D. Nothing
Correct: C
Explanation: Accessing undeclared variable throws ReferenceError.
89. Which keyword allows block-scoped variable declarations?
- A. var
- B. let
- C. const
- D. Both b and c
Correct: D
Explanation: let and const are block-scoped.
90. Which queue does setTimeout() use in JavaScript?
- A. Microtask queue
- B. Callback queue
- C. Event loop queue
- D. Execution stack
Correct: B
Explanation: setTimeout uses the callback (macrotask) queue.
91. Which method sends a GET request using Fetch API?
- A. fetch(url)
- B. fetch(url, { method: 'GET' })
- C. Both a and b
- D. None of the above
Correct: C
Explanation: Fetch uses GET by default and also supports explicit method.
92. What is a template literal in JavaScript?
- A. A type of array
- B. A string enclosed in backticks
- C. A special function
- D. A new data type
Correct: B
Explanation: Template literals use backticks (`).
93. Which method converts a JavaScript object into a JSON string?
- A. JSON.stringify()
- B. JSON.parse()
- C. toJSON()
- D. parseJSON()
Correct: A
Explanation: JSON.stringify converts object to JSON string.
94. Which of the following is NOT a JavaScript data type?
- A. String
- B. Boolean
- C. Float
- D. Undefined
Correct: C
Explanation: JavaScript does not have a separate Float data type. Numbers are represented using a single Number type.
95. Which state is NOT valid for a Promise?
- A. Pending
- B. Fulfilled
- C. Rejected
- D. Running
Correct: D
Explanation: Promises have only Pending, Fulfilled, and Rejected states.
96. Which symbol is used for single-line comments in JavaScript?
Correct: A
Explanation: Single-line comments in JavaScript start with //.
97. What is the output of x + y when both are null?
- A. null
- B. object
- C. 0
- D. undefined
Correct: C
Explanation: null is coerced to 0 during addition.
98. Which JavaScript engine is used in Google Chrome?
- A. SpiderMonkey
- B. V8
- C. Chakra
- D. Nitro
Correct: B
Explanation: Google Chrome uses the V8 engine.
99. What will console.log(x); var x = 10; output?
- A. 10
- B. undefined
- C. ReferenceError
- D. NaN
Correct: B
Explanation: var is hoisted but initialized as undefined.
100. Which API is used for making HTTP requests in JavaScript?
- A. XMLHttpRequest
- B. Fetch API
- C. Axios
- D. All of the above
Correct: D
Explanation: All listed options can be used to make HTTP requests.
101. Which method converts a string into a number?
- A. parseInt()
- B. Number()
- C. Unary +
- D. All of the above
Correct: D
Explanation: All methods can convert strings to numbers.
102. Which function combines array elements into a single value?
- A. reduce()
- B. map()
- C. join()
- D. concat()
Correct: A
Explanation: reduce() accumulates values.
103. Which method is used to filter elements from an array?
- A. map()
- B. filter()
- C. reduce()
- D. slice()
Correct: B
Explanation: filter() returns elements matching a condition.
104. Which type of function executes immediately after its definition?
- A. Anonymous function
- B. Named function
- C. IIFE
- D. Arrow function
Correct: C
Explanation: IIFE runs immediately after being defined.
105. Which function generates a random number between 0 and 1?
- A. Math.random()
- B. random()
- C. generateRandom()
- D. Math.rand()
Correct: A
Explanation: Math.random() returns a number between 0 and 1.
106. Which statement about JavaScript classes is true?
- A. They support multiple inheritance
- B. They are syntactic sugar over prototypes
- C. They can be redeclared
- D. They do not support inheritance
Correct: B
Explanation: JavaScript classes are syntactic sugar over prototype-based inheritance.
107. What is the result of 5 == "5"?
Correct: A
Explanation: Loose equality performs type coercion.
108. What happens if an error occurs inside the try block?
- A. Script stops execution
- B. Error is caught in catch block
- C. Script crashes
- D. Error is ignored
Correct: B
Explanation: catch block handles the error.
109. How do you deep clone an object in JavaScript?
- A. Object.assign({}, obj)
- B. JSON.parse(JSON.stringify(obj))
- C. obj.clone()
- D. obj.copy()
Correct: B
Explanation: JSON methods create a deep copy (with limitations).