JavaScript MCQs

1. Which of the following is the correct way to declare a variable in JavaScript?A) var x;
B) int x;
C) let x;
D) Both A and C
✅ Answer: D) Both A and C
2. Which keyword is used to declare a block-scoped variable?A) var
B) let
C) const
D) Both B and C
✅ Answer: D) Both B and C
3. Which keyword is used to declare a constant variable in JavaScript?A) const
B) let
C) var
D) constant
✅ Answer: A) const
4. Which of the following is a primitive data type in JavaScript?A) Object
B) Number
C) Array
D) Function
✅ Answer: B) Number
5. Which of the following is NOT a JavaScript data type?A) Boolean
B) Float
C) Undefined
D) Symbol
✅ Answer: B) Float
6. What will typeof null return?A) null
B) object
C) undefined
D) number
✅ Answer: B) object
7. What will typeof NaN return?A) NaN
B) number
C) undefined
D) object
✅ Answer: B) number
8. Which operator is used for strict equality comparison?A) ==
B) =
C) ===
D) !=
✅ Answer: C) ===
9. Which operator is used for assignment in JavaScript?A) =
B) ==
C) ===
D) :=
✅ Answer: A) =
10. Which operator is used to check if two values are NOT equal?A) !=
B) !==
C) Both A and B
D) <>
✅ Answer: C) Both A and B
11. What is the output of console.log(2 + "2");?A) 4
B) "22"
C) 22
D) Error
✅ Answer: B) "22"
12. What is the output of console.log(2 - "2");?A) 0
B) "0"
C) "22"
D) Error
✅ Answer: A) 0
13. Which of the following is a falsy value in JavaScript?A) 0
B) “0”
C) []
D) {}
✅ Answer: A) 0
14. Which of the following is truthy in JavaScript?A) null
B) undefined
C) “false”
D) 0
✅ Answer: C) "false"
15. What is the output of console.log(typeof [])?A) array
B) object
C) list
D) undefined
✅ Answer: B) object
16. How can you create a function in JavaScript?A) function myFunc() {}
B) var myFunc = function() {}
C) let myFunc = () => {}
D) All of the above
✅ Answer: D) All of the above
17. What is the correct syntax for an arrow function?A) () => {}
B) function() => {}
C) => function {}
D) function() {}
✅ Answer: A) () => {}
18. What is the output of console.log([1,2,3] + [4,5]);?A) [1,2,3,4,5]
B) "1,2,34,5"
C) "1,2,34,5"
D) Error
✅ Answer: C) "1,2,34,5"
19. How do you write a single-line comment in JavaScript?A) // comment
B) /* comment */
C) <!-- comment -->
D) # comment
✅ Answer: A) // comment
20. How do you write a multi-line comment in JavaScript?A) /* comment */
B) // comment
C) <!-- comment -->
D) # comment
✅ Answer: A) /* comment */
21. What will console.log(1 + true); output?A) 2
B) "1true"
C) 1
D) Error
✅ Answer: A) 2
22. Which method converts a JSON string into an object?A) JSON.parse()
B) JSON.stringify()
C) JSON.toObject()
D) parseJSON()
✅ Answer: A) JSON.parse()
23. Which method converts an object into a JSON string?A) JSON.stringify()
B) JSON.parse()
C) JSON.toString()
D) toJSON()
✅ Answer: A) JSON.stringify()
24. Which global object represents the browser window in JavaScript?A) window
B) document
C) navigator
D) screen
✅ Answer: A) window
25. Which object allows you to access the HTML document?A) document
B) window
C) navigator
D) body
✅ Answer: A) document
26. Which method is used to get an element by its ID?A) document.getElementById()
B) document.getElementByClassName()
C) document.querySelector()
D) document.getElementsByTagName()
✅ Answer: A) document.getElementById()
27. Which method is used to select the first element that matches a CSS selector?A) document.querySelector()
B) document.getElementById()
C) document.getElementsByClassName()
D) document.getElementsByTagName()
✅ Answer: A) document.querySelector()
28. Which method selects all elements matching a CSS selector?A) document.querySelectorAll()
B) document.getElementsByClassName()
C) document.getElementsByTagName()
D) document.querySelector()
✅ Answer: A) document.querySelectorAll()
29. Which property changes the HTML content of an element?A) innerHTML
B) outerHTML
C) textContent
D) value
✅ Answer: A) innerHTML
30. Which property changes only the text content of an element?A) textContent
B) innerHTML
C) outerHTML
D) innerText
✅ Answer: A) textContent
31. How do you add a class to an element in JavaScript?A) element.classList.add("classname")
B) element.addClass("classname")
C) element.class += "classname"
D) element.setClass("classname")
✅ Answer: A) element.classList.add("classname")
32. How do you remove a class from an element?A) element.classList.remove("classname")
B) element.removeClass("classname")
C) element.class -= "classname"
D) element.deleteClass("classname")
✅ Answer: A) element.classList.remove("classname")
33. Which method attaches an event listener to an element?A) addEventListener()
B) attachEvent()
C) on()
D) listen()
✅ Answer: A) addEventListener()
34. Which event occurs when a user clicks on an element?A) click
B) mouseover
C) submit
D) keydown
✅ Answer: A) click
35. Which event occurs when a user presses a key?A) keydown
B) keypress
C) keyup
D) All of the above
✅ Answer: D) All of the above
36. How do you prevent a form from submitting?A) event.preventDefault()
B) event.stopPropagation()
C) return false
D) Both A and C
✅ Answer: D) Both A and C
37. Which operator is used for logical AND?A) &&
B) ||
C) !
D) &
✅ Answer: A) &&
38. Which operator is used for logical OR?A) ||
B) &&
C) !
D) |
✅ Answer: A) ||
39. Which operator is used for logical NOT?A) !
B) ~
C) ^
D) ?
✅ Answer: A) !
40. Which method converts a string to an integer?A) parseInt()
B) parseFloat()
C) Number()
D) toInteger()
✅ Answer: A) parseInt()
41. Which method converts a string to a floating-point number?A) parseFloat()
B) parseInt()
C) Number()
D) toFloat()
✅ Answer: A) parseFloat()
42. Which method converts a number to a string?A) toString()
B) String()
C) Both A and B
D) convertString()
✅ Answer: C) Both A and B
43. Which method rounds a number to the nearest integer?A) Math.round()
B) Math.ceil()
C) Math.floor()
D) Math.trunc()
✅ Answer: A) Math.round()
44. Which method rounds a number upward?A) Math.ceil()
B) Math.floor()
C) Math.round()
D) Math.trunc()
✅ Answer: A) Math.ceil()
45. Which method rounds a number downward?A) Math.floor()
B) Math.ceil()
C) Math.round()
D) Math.trunc()
✅ Answer: A) Math.floor()
46. Which method returns a random number between 0 and 1?A) Math.random()
B) Math.random(0,1)
C) Math.rand()
D) random()
✅ Answer: A) Math.random()
47. Which array method adds an element to the end of an array?A) push()
B) pop()
C) shift()
D) unshift()
✅ Answer: A) push()
48. Which array method removes the last element of an array?A) pop()
B) push()
C) shift()
D) unshift()
✅ Answer: A) pop()
49. Which array method adds an element to the beginning of an array?A) unshift()
B) push()
C) shift()
D) concat()
✅ Answer: A) unshift()
50. Which array method removes the first element of an array?A) shift()
B) unshift()
C) pop()
D) push()
✅ Answer: A) shift()51. What is the output of console.log(typeof function(){});?
A) function
B) object
C) undefined
D) string
✅ Answer: A) function
52. What is the default return value of a function that does not explicitly return anything?A) undefined
B) null
C) 0
D) NaN
✅ Answer: A) undefined
53. Which keyword is used to refer to the current object?A) this
B) self
C) current
D) object
✅ Answer: A) this
54. What is the output of console.log(this); in the global scope (non-strict mode)?A) window
B) undefined
C) null
D) document
✅ Answer: A) window
55. Which of the following is a correct way to define an object?A) var obj = {name: "John", age: 30};
B) var obj = Object();
C) var obj = new Object();
D) All of the above
✅ Answer: D) All of the above
56. How do you access the property name of an object obj?A) obj.name
B) obj["name"]
C) Both A and B
D) obj->name
✅ Answer: C) Both A and B
57. Which method removes the last element from an array and returns it?A) pop()
B) push()
C) shift()
D) unshift()
✅ Answer: A) pop()
58. Which method adds one or more elements to the end of an array?A) push()
B) pop()
C) shift()
D) unshift()
✅ Answer: A) push()
59. Which method removes the first element from an array and returns it?A) shift()
B) unshift()
C) pop()
D) push()
✅ Answer: A) shift()
60. Which method adds one or more elements to the beginning of an array?A) unshift()
B) shift()
C) push()
D) pop()
✅ Answer: A) unshift()
61. How can you create a new array from an existing array with some elements removed?A) slice()
B) splice()
C) concat()
D) filter()
✅ Answer: B) splice()
62. Which method creates a new array with elements that pass a test?A) filter()
B) map()
C) reduce()
D) find()
✅ Answer: A) filter()
63. Which method creates a new array with the results of calling a function on every element?A) map()
B) filter()
C) forEach()
D) reduce()
✅ Answer: A) map()
64. Which method executes a function for each array element without returning a new array?A) forEach()
B) map()
C) filter()
D) reduce()
✅ Answer: A) forEach()
65. Which method reduces an array to a single value?A) reduce()
B) map()
C) filter()
D) forEach()
✅ Answer: A) reduce()
66. Which method finds the first element in an array that satisfies a condition?A) find()
B) filter()
C) map()
D) some()
✅ Answer: A) find()
67. Which method checks if at least one element passes a test?A) some()
B) every()
C) find()
D) filter()
✅ Answer: A) some()
68. Which method checks if all elements pass a test?A) every()
B) some()
C) find()
D) filter()
✅ Answer: A) every()
69. Which method returns a shallow copy of a portion of an array?A) slice()
B) splice()
C) concat()
D) map()
✅ Answer: A) slice()
70. Which method merges two or more arrays?A) concat()
B) merge()
C) combine()
D) append()
✅ Answer: A) concat()
71. Which method checks if an array includes a certain element?A) includes()
B) indexOf()
C) find()
D) contains()
✅ Answer: A) includes()
72. Which method returns the index of the first occurrence of an element?A) indexOf()
B) findIndex()
C) search()
D) position()
✅ Answer: A) indexOf()
73. Which method returns the index of the first element that passes a test?A) findIndex()
B) indexOf()
C) search()
D) map()
✅ Answer: A) findIndex()
74. Which keyword declares a function expression?A) var myFunc = function() {}
B) function myFunc() {}
C) let myFunc = () => {}
D) All of the above
✅ Answer: D) All of the above
75. Which keyword declares a block-scoped variable?A) let
B) var
C) const
D) Both A and C
✅ Answer: D) Both A and C
76. What is a closure in JavaScript?A) A function that has access to its outer scope
B) A block of variables
C) An immediately invoked function
D) A method to close a program
✅ Answer: A) A function that has access to its outer scope
77. Which of the following is true about JavaScript objects?A) Objects are key-value pairs
B) Keys are strings or symbols
C) Values can be any type
D) All of the above
✅ Answer: D) All of the above
78. How do you add a new property to an object obj?A) obj.newProp = value;
B) obj["newProp"] = value;
C) Both A and B
D) obj.add(newProp, value);
✅ Answer: C) Both A and B
79. Which operator checks if a property exists in an object?A) in
B) has
C) contains
D) exists
✅ Answer: A) in
80. Which method returns an array of an object’s keys?A) Object.keys(obj)
B) Object.values(obj)
C) Object.entries(obj)
D) obj.keys()
✅ Answer: A) Object.keys(obj)
81. Which method returns an array of an object’s values?A) Object.values(obj)
B) Object.keys(obj)
C) Object.entries(obj)
D) obj.values()
✅ Answer: A) Object.values(obj)
82. Which method returns an array of [key, value] pairs from an object?A) Object.entries(obj)
B) Object.keys(obj)
C) Object.values(obj)
D) Object.pairs(obj)
✅ Answer: A) Object.entries(obj)
83. What is the output of console.log([] == false);?A) true
B) false
C) undefined
D) Error
✅ Answer: A) true
84. What is the output of console.log([] === false);?A) false
B) true
C) undefined
D) Error
✅ Answer: A) false
85. Which method converts an object to a JSON string?A) JSON.stringify()
B) JSON.parse()
C) toString()
D) Object.toJSON()
✅ Answer: A) JSON.stringify()
86. Which method parses a JSON string into an object?A) JSON.parse()
B) JSON.stringify()
C) eval()
D) Object.parse()
✅ Answer: A) JSON.parse()
87. What does setTimeout() do?A) Executes a function after a delay
B) Repeats a function continuously
C) Stops a function
D) Pauses the script
✅ Answer: A) Executes a function after a delay
88. What does setInterval() do?A) Executes a function repeatedly at specified intervals
B) Executes once
C) Stops execution
D) Delays function execution
✅ Answer: A) Executes a function repeatedly at specified intervals
89. Which method cancels a timeout set by setTimeout()?A) clearTimeout()
B) stopTimeout()
C) cancelTimeout()
D) clearInterval()
✅ Answer: A) clearTimeout()
90. Which method cancels an interval set by setInterval()?A) clearInterval()
B) stopInterval()
C) cancelInterval()
D) clearTimeout()
✅ Answer: A) clearInterval()
91. Which ES6 feature allows string interpolation?A) Template literals
B) String.concat()
C) String.replace()
D) String.format()
✅ Answer: A) Template literals
92. Which syntax defines a template literal?A) `Hello ${name}`
B) "Hello ${name}"
C) 'Hello ${name}'
D) <Hello ${name}>
✅ Answer: A) `Hello ${name}`
93. Which keyword declares a block-scoped constant?A) const
B) let
C) var
D) static
✅ Answer: A) const
94. Which syntax defines a class in ES6?A) class MyClass { constructor() {} }
B) function MyClass() {}
C) var MyClass = function() {}
D) object MyClass {}
✅ Answer: A) class MyClass { constructor() {} }
95. How do you create an instance of a class?A) let obj = new MyClass();
B) let obj = MyClass();
C) let obj = MyClass.new();
D) let obj = new Object(MyClass);
✅ Answer: A) let obj = new MyClass();
96. Which method defines a getter in a class?A) get propertyName() {}
B) getter propertyName() {}
C) function get propertyName() {}
D) propertyName.get() {}
✅ Answer: A) get propertyName() {}
97. Which method defines a setter in a class?A) set propertyName(value) {}
B) setter propertyName(value) {}
C) function set propertyName(value) {}
D) propertyName.set(value) {}
✅ Answer: A) set propertyName(value) {}
98. What is the output of console.log(typeof NaN);?A) number
B) NaN
C) undefined
D) object
✅ Answer: A) number
99. Which keyword is used to import modules in ES6?A) import
B) require
C) include
D) module
✅ Answer: A) import
100. Which keyword is used to export modules in ES6?A) export
B) module.exports
C) include
D) public
✅ Answer: A) export

101. Which method changes the style of an HTML element in JavaScript?

A) element.style.property = value
B) element.setStyle(property, value)
C) element.css(property, value)
D) element.style.set(property, value)
Answer: A) element.style.property = value


102. How do you access all elements with a specific class name?

A) document.getElementsByClassName("className")
B) document.getElementByClass("className")
C) document.querySelector(".className")
D) document.querySelectorAll("className")
Answer: A) document.getElementsByClassName("className")


103. How do you attach a click event listener to a button with id btn?

A) document.getElementById("btn").addEventListener("click", func)
B) btn.onclick(func)
C) document.click("btn", func)
D) document.getElementById("btn").on("click", func)
Answer: A) document.getElementById("btn").addEventListener("click", func)


104. Which method removes an element from the DOM?

A) element.remove()
B) element.delete()
C) element.destroy()
D) element.detach()
Answer: A) element.remove()


105. How do you create a new HTML element using JavaScript?

A) document.createElement("tagName")
B) document.newElement("tagName")
C) document.makeElement("tagName")
D) document.addElement("tagName")
Answer: A) document.createElement("tagName")


106. How do you append a child element to a parent?

A) parent.appendChild(child)
B) parent.addChild(child)
C) parent.attachChild(child)
D) parent.insertChild(child)
Answer: A) parent.appendChild(child)


107. How do you remove a child element from its parent?

A) parent.removeChild(child)
B) child.removeParent()
C) parent.deleteChild(child)
D) child.detach()
Answer: A) parent.removeChild(child)


108. Which property retrieves the value of an input element?

A) element.value
B) element.innerHTML
C) element.textContent
D) element.content
Answer: A) element.value


109. Which event occurs when an input loses focus?

A) blur
B) focusout
C) focus
D) change
Answer: A) blur


110. Which event occurs when an input gains focus?

A) focus
B) blur
C) focusin
D) input
Answer: A) focus


111. Which event occurs when the user types into an input?

A) input
B) change
C) keydown
D) keypress
Answer: A) input


112. Which method prevents an event from bubbling up the DOM?

A) event.stopPropagation()
B) event.preventDefault()
C) event.cancel()
D) event.stop()
Answer: A) event.stopPropagation()


113. Which method prevents the default action of an event?

A) event.preventDefault()
B) event.stopPropagation()
C) event.cancel()
D) event.block()
Answer: A) event.preventDefault()


114. Which property retrieves the event target element?

A) event.target
B) event.srcElement
C) this
D) All of the above
Answer: D) All of the above


115. What does let keyword in JavaScript provide?

A) Block-scoped variables
B) Function-scoped variables
C) Global variables
D) Constant variables
Answer: A) Block-scoped variables


116. What does the const keyword in JavaScript provide?

A) Block-scoped constant variables
B) Function-scoped variables
C) Global variables
D) Mutable variables
Answer: A) Block-scoped constant variables


117. What is a Promise in JavaScript?

A) An object representing eventual completion or failure of an async operation
B) A function that returns immediately
C) A synchronous operation
D) A callback function
Answer: A) An object representing eventual completion or failure of an async operation


118. Which method is used to handle a successful Promise?

A) then()
B) catch()
C) finally()
D) resolve()
Answer: A) then()


119. Which method is used to handle a rejected Promise?

A) catch()
B) then()
C) finally()
D) reject()
Answer: A) catch()


120. Which method executes code after a Promise settles, regardless of outcome?

A) finally()
B) then()
C) catch()
D) done()
Answer: A) finally()


121. Which keyword is used to declare an async function?

A) async
B) await
C) promise
D) defer
Answer: A) async


122. Which keyword is used to wait for a Promise to resolve inside an async function?

A) await
B) async
C) then
D) resolve
Answer: A) await


123. What is the output of console.log(typeof Symbol("id"));?

A) symbol
B) string
C) object
D) undefined
Answer: A) symbol


124. Which ES6 feature allows default parameter values in functions?

A) function f(a = 10) {}
B) function f(a?:10) {}
C) function f(a:10) {}
D) function f(default a = 10) {}
Answer: A) function f(a = 10) {}


125. Which ES6 feature allows destructuring arrays?

A) [a, b] = [1, 2]
B) {a, b} = {1,2}
C) array(a, b) = [1,2]
D) let a,b = [1,2]
Answer: A) [a, b] = [1, 2]


126. Which ES6 feature allows destructuring objects?

A) {a, b} = {a: 1, b: 2}
B) [a, b] = [1,2]
C) object(a,b) = {1,2}
D) let a,b = {1,2}
Answer: A) {a, b} = {a: 1, b: 2}


127. What is the output of console.log([...new Set([1,2,2,3])]);?

A) [1,2,3]
B) [1,2,2,3]
C) [1,3]
D) Error
Answer: A) [1,2,3]


128. What is the output of console.log([1,2,3].includes(2));?

A) true
B) false
C) undefined
D) Error
Answer: A) true


129. Which ES6 feature allows for string interpolation?

A) Template literals using backticks `Hello ${name}`
B) String.concat()
C) String.format()
D) "Hello ${name}"
Answer: A) Template literals using backticks `Hello ${name}`


130. Which ES6 method creates a new array by removing duplicates?

A) new Set(array)
B) array.unique()
C) array.distinct()
D) array.filter()
Answer: A) new Set(array)


131. How do you handle multiple asynchronous operations at once?

A) Promise.all()
B) Promise.race()
C) Promise.any()
D) All of the above
Answer: D) All of the above


132. Which method resolves the first settled Promise among multiple Promises?

A) Promise.race()
B) Promise.all()
C) Promise.any()
D) Promise.allSettled()
Answer: A) Promise.race()


133. Which method resolves if at least one Promise resolves?

A) Promise.any()
B) Promise.all()
C) Promise.race()
D) Promise.settle()
Answer: A) Promise.any()


134. Which method waits for all Promises to settle, regardless of outcome?

A) Promise.allSettled()
B) Promise.all()
C) Promise.race()
D) Promise.any()
Answer: A) Promise.allSettled()


135. How do you export a default function in ES6 module?

A) export default function myFunc() {}
B) export function myFunc() {}
C) module.exports = myFunc
D) export func myFunc()
Answer: A) export default function myFunc() {}


136. How do you import a default export?

A) import myFunc from './file.js';
B) import {myFunc} from './file.js';
C) require('./file.js')
D) import * as myFunc from './file.js';
Answer: A) import myFunc from './file.js';


137. How do you import named exports?

A) import {name1, name2} from './file.js';
B) import name1, name2 from './file.js';
C) import * as name from './file.js';
D) require('./file.js')
Answer: A) import {name1, name2} from './file.js';


138. Which method returns an object’s entries as [key, value] pairs?

A) Object.entries(obj)
B) Object.keys(obj)
C) Object.values(obj)
D) Object.pairs(obj)
Answer: A) Object.entries(obj)


139. Which method freezes an object so that properties cannot be changed?

A) Object.freeze(obj)
B) Object.seal(obj)
C) Object.lock(obj)
D) Object.prevent(obj)
Answer: A) Object.freeze(obj)


140. Which method prevents new properties from being added to an object?

A) Object.seal(obj)
B) Object.freeze(obj)
C) Object.preventExtensions(obj)
D) Object.lock(obj)
Answer: C) Object.preventExtensions(obj)


141. Which method checks if a property exists in an object?

A) obj.hasOwnProperty("prop")
B) "prop" in obj
C) Both A and B
D) obj.exists("prop")
Answer: C) Both A and B


142. Which method merges multiple objects into one?

A) Object.assign(target, ...sources)
B) Object.merge(target, ...sources)
C) Object.combine(target, ...sources)
D) Object.concat(target, ...sources)
Answer: A) Object.assign(target, ...sources)


143. Which method returns the keys of an object?

A) Object.keys(obj)
B) Object.values(obj)
C) Object.entries(obj)
D) Object.getKeys(obj)
Answer: A) Object.keys(obj)


144. Which method returns the values of an object?

A) Object.values(obj)
B) Object.keys(obj)
C) Object.entries(obj)
D) Object.getValues(obj)
Answer: A) Object.values(obj)


145. What is the difference between == and ===?

A) == compares values, === compares value and type
B) == compares type, === compares value
C) Both compare value and type
D) None of the above
Answer: A) == compares values, === compares value and type


146. What is the difference between let and var?

A) let is block-scoped, var is function-scoped
B) let is global, var is local
C) let cannot be redeclared in scope, var can
D) Both A and C
Answer: D) Both A and C


147. Which method returns a shallow copy of part of an array?

A) slice()
B) splice()
C) concat()
D) copy()
Answer: A) slice()


148. Which method changes the content of an array by removing, replacing, or adding elements?

A) splice()
B) slice()
C) concat()
D) modify()
Answer: A) splice()


149. Which method returns the first index at which a given element can be found?

A) indexOf()
B) findIndex()
C) search()
D) find()
Answer: A) indexOf()


150. Which method returns the first element that passes a test in an array?

A) find()
B) findIndex()
C) filter()
D) some()
Answer: A) find()

151. Which method is used to create a deep copy of an object?A) JSON.parse(JSON.stringify(obj))
B) Object.assign({}, obj)
C) Object.copy(obj)
D) obj.clone()
Answer: A) JSON.parse(JSON.stringify(obj))
152. What is the output of console.log([...'hello']);?A) ['h','e','l','l','o']
B) ['hello']
C) 'hello'
D) Error
Answer: A) ['h','e','l','l','o']
153. Which method sends an HTTP request in vanilla JavaScript?A) XMLHttpRequest
B) fetch()
C) Both A and B
D) ajax()
Answer: C) Both A and B
154. Which method fetches resources asynchronously and returns a Promise?A) fetch()
B) XMLHttpRequest
C) ajax()
D) get()
Answer: A) fetch()
155. How do you handle errors in Fetch API?A) .catch()
B) .then()
C) try…catch with async/await
D) Both A and C
Answer: D) Both A and C
156. What does async function return?A) A Promise
B) Undefined
C) A value directly
D) A callback function
Answer: A) A Promise
157. Which of the following is true about await?A) Can only be used inside async functions
B) Pauses execution until the Promise resolves
C) Returns the resolved value
D) All of the above
Answer: D) All of the above
158. Which regular expression pattern matches a single digit?A) \d
B) \D
C) [0-9]
D) Both A and C
Answer: D) Both A and C
159. Which regular expression pattern matches a non-digit character?A) \D
B) \d
C) [0-9]
D) [a-z]
Answer: A) \D
160. Which regular expression pattern matches any whitespace character?A) \s
B) \S
C) [ ]
D) \w
Answer: A) \s
161. Which regular expression pattern matches a non-whitespace character?A) \S
B) \s
C) \w
D) \d
Answer: A) \S
162. Which regular expression pattern matches a word character (alphanumeric + underscore)?A) \w
B) \W
C) \s
D) .
Answer: A) \w
163. Which regular expression pattern matches a non-word character?A) \W
B) \w
C) \s
D) .
Answer: A) \W
164. Which regular expression pattern matches the start of a string?A) ^
B) $
C) \b
D) .
Answer: A) ^
165. Which regular expression pattern matches the end of a string?A) $
B) ^
C) \b
D) .
Answer: A) $
166. Which regular expression pattern matches zero or more occurrences of a character?A) *
B) +
C) ?
D) {}
Answer: A) *
167. Which regular expression pattern matches one or more occurrences of a character?A) +
B) *
C) ?
D) {}
Answer: A) +
168. Which regular expression pattern matches zero or one occurrence of a character?A) ?
B) *
C) +
D) {}
Answer: A) ?
169. Which regular expression pattern matches exactly n occurrences?A) {n}
B) {n,}
C) {,n}
D) *
Answer: A) {n}
170. Which regular expression pattern matches between n and m occurrences?A) {n,m}
B) {n}
C) *
D) +
Answer: A) {n,m}
171. How do you create a regular expression in JavaScript?A) /pattern/flags
B) new RegExp("pattern", "flags")
C) Both A and B
D) RegExp("pattern")
Answer: C) Both A and B
172. Which flag in regular expressions makes it case-insensitive?A) i
B) g
C) m
D) c
Answer: A) i
173. Which flag in regular expressions matches globally?A) g
B) i
C) m
D) c
Answer: A) g
174. Which flag in regular expressions enables multiline mode?A) m
B) g
C) i
D) s
Answer: A) m
175. What is the output of console.log(/[a-z]/.test("A"));?A) false
B) true
C) undefined
D) Error
Answer: A) false
176. What is the output of console.log(/[a-z]/i.test("A"));?A) true
B) false
C) undefined
D) Error
Answer: A) true
177. Which property returns the length of an array?A) array.length
B) array.size
C) array.count
D) array.total
Answer: A) array.length
178. Which method reverses the elements of an array?A) reverse()
B) sort()
C) rotate()
D) flip()
Answer: A) reverse()
179. Which method sorts the elements of an array?A) sort()
B) reverse()
C) order()
D) arrange()
Answer: A) sort()
180. How do you check if a variable is an array?A) Array.isArray(variable)
B) variable instanceof Array
C) Both A and B
D) variable.isArray()
Answer: C) Both A and B
181. Which method merges two or more arrays?A) concat()
B) merge()
C) combine()
D) join()
Answer: A) concat()
182. Which method joins all array elements into a string?A) join()
B) concat()
C) toString()
D) Both A and C
Answer: D) Both A and C
183. Which method tests whether at least one element passes a test?A) some()
B) every()
C) find()
D) filter()
Answer: A) some()
184. Which method tests whether all elements pass a test?A) every()
B) some()
C) find()
D) filter()
Answer: A) every()
185. Which method returns a new array with elements that pass a test?A) filter()
B) map()
C) forEach()
D) reduce()
Answer: A) filter()
186. Which method applies a function to each array element and returns a new array?A) map()
B) filter()
C) forEach()
D) reduce()
Answer: A) map()
187. Which method applies a function to reduce the array to a single value?A) reduce()
B) map()
C) filter()
D) forEach()
Answer: A) reduce()
188. Which method returns a new array with elements reversed in order?A) reverse()
B) sort()
C) flip()
D) rotate()
Answer: A) reverse()
189. Which method extracts a portion of an array into a new array?A) slice()
B) splice()
C) concat()
D) copy()
Answer: A) slice()
190. Which method changes the content of an array by removing, replacing, or adding elements?A) splice()
B) slice()
C) concat()
D) modify()
Answer: A) splice()
191. Which of the following is true about JavaScript modules?A) Modules help in organizing code
B) Modules can export and import functions, objects, or values
C) Modules support default and named exports
D) All of the above
Answer: D) All of the above
192. Which method converts a string into an integer?A) parseInt()
B) parseFloat()
C) Number()
D) Both A and C
Answer: D) Both A and C
193. Which method converts a string into a floating-point number?A) parseFloat()
B) parseInt()
C) Number()
D) toFloat()
Answer: A) parseFloat()
194. Which method converts a number to a string?A) toString()
B) String()
C) Both A and B
D) Number.toString()
Answer: C) Both A and B
195. Which method rounds a number to the nearest integer?A) Math.round()
B) Math.ceil()
C) Math.floor()
D) Math.trunc()
Answer: A) Math.round()
196. Which method rounds a number upward to the nearest integer?A) Math.ceil()
B) Math.floor()
C) Math.round()
D) Math.trunc()
Answer: A) Math.ceil()
197. Which method rounds a number downward to the nearest integer?A) Math.floor()
B) Math.ceil()
C) Math.round()
D) Math.trunc()
Answer: A) Math.floor()
198. Which method returns a random number between 0 and 1?A) Math.random()
B) Math.rand()
C) Math.random(0,1)
D) Random()
Answer: A) Math.random()
199. Which method truncates the decimal part of a number?A) Math.trunc()
B) Math.floor()
C) Math.ceil()
D) Math.round()
Answer: A) Math.trunc()
200. Which operator is used to concatenate strings in JavaScript?A) +
B) &
C) .
D) concat()
Answer: A) +

201. What is Event Delegation in JavaScript?

A) Attaching a single event listener to a parent element to handle events for its children
B) Adding multiple event listeners to each child element
C) Delegating events to another function
D) Assigning events to global variables
Answer: A) Attaching a single event listener to a parent element to handle events for its children


202. Which method stores data in the browser that persists even after closing the browser?

A) localStorage.setItem(key, value)
B) sessionStorage.setItem(key, value)
C) cookie.setItem(key, value)
D) document.store(key, value)
Answer: A) localStorage.setItem(key, value)


203. Which method stores data in the browser that is cleared when the tab is closed?

A) sessionStorage.setItem(key, value)
B) localStorage.setItem(key, value)
C) cookie.setItem(key, value)
D) document.store(key, value)
Answer: A) sessionStorage.setItem(key, value)


204. How do you retrieve a value from localStorage?

A) localStorage.getItem(key)
B) localStorage.fetch(key)
C) localStorage[key]
D) Both A and C
Answer: D) Both A and C


205. How do you remove an item from localStorage?

A) localStorage.removeItem(key)
B) localStorage.deleteItem(key)
C) localStorage.clear(key)
D) localStorage.remove(key)
Answer: A) localStorage.removeItem(key)


206. Which method clears all localStorage data?

A) localStorage.clear()
B) sessionStorage.clear()
C) document.clearStorage()
D) storage.clear()
Answer: A) localStorage.clear()


207. How do you store an object in localStorage?

A) localStorage.setItem("key", JSON.stringify(obj))
B) localStorage.setItem("key", obj)
C) localStorage.save(obj)
D) localStorage.store(obj)
Answer: A) localStorage.setItem("key", JSON.stringify(obj))


208. How do you retrieve an object from localStorage?

A) JSON.parse(localStorage.getItem("key"))
B) localStorage.getItem("key")
C) localStorage.parse("key")
D) JSON.get(localStorage.getItem("key"))
Answer: A) JSON.parse(localStorage.getItem("key"))


209. What does navigator.geolocation.getCurrentPosition() do?

A) Gets the user’s current geographic location
B) Sets the user’s location
C) Updates location on the server
D) Returns a random location
Answer: A) Gets the user’s current geographic location


210. How do you make a fetch request to get JSON data?

A) fetch(url).then(res => res.json())
B) fetch(url).json()
C) XMLHttpRequest(url).json()
D) fetchJSON(url)
Answer: A) fetch(url).then(res => res.json())


211. Which method catches errors in Promises?

A) .catch()
B) .then()
C) .finally()
D) try…catch
Answer: A) .catch()


212. Which global function evaluates a string as JavaScript code?

A) eval()
B) parseInt()
C) Function()
D) execute()
Answer: A) eval()


213. Which method creates a new function from a string?

A) new Function("a", "b", "return a+b")
B) eval("function(a,b){return a+b}")
C) Function.create("a","b")
D) func("a","b")
Answer: A) new Function("a", "b", "return a+b")


214. Which object represents the browser window?

A) window
B) document
C) navigator
D) screen
Answer: A) window


215. Which object represents the HTML document?

A) document
B) window
C) navigator
D) screen
Answer: A) document


216. Which object contains information about the browser?

A) navigator
B) window
C) screen
D) document
Answer: A) navigator


217. Which object contains information about the screen size?

A) screen
B) window
C) navigator
D) document
Answer: A) screen


218. Which property gives the height of the window’s content area?

A) window.innerHeight
B) window.outerHeight
C) screen.height
D) document.height
Answer: A) window.innerHeight


219. Which property gives the width of the window’s content area?

A) window.innerWidth
B) window.outerWidth
C) screen.width
D) document.width
Answer: A) window.innerWidth


220. Which property gives the width of the entire browser window including borders?

A) window.outerWidth
B) window.innerWidth
C) screen.width
D) document.width
Answer: A) window.outerWidth


221. Which method triggers an alert dialog?

A) alert("message")
B) confirm("message")
C) prompt("message")
D) notify("message")
Answer: A) alert("message")


222. Which method triggers a confirm dialog?

A) confirm("message")
B) alert("message")
C) prompt("message")
D) notify("message")
Answer: A) confirm("message")


223. Which method triggers a prompt dialog?

A) prompt("message")
B) alert("message")
C) confirm("message")
D) notify("message")
Answer: A) prompt("message")


224. Which property gives the URL of the current page?

A) window.location.href
B) document.URL
C) Both A and B
D) window.href
Answer: C) Both A and B


225. Which method reloads the current page?

A) location.reload()
B) window.reload()
C) document.reload()
D) window.location.reset()
Answer: A) location.reload()


226. Which method navigates to a new URL?

A) location.assign("URL")
B) window.navigate("URL")
C) document.goto("URL")
D) window.location.change("URL")
Answer: A) location.assign("URL")


227. How do you check if a variable exists?

A) if(typeof variable !== "undefined")
B) if(variable != null)
C) if(variable)
D) if(variable.exists)
Answer: A) if(typeof variable !== "undefined")


228. Which object represents errors in JavaScript?

A) Error
B) Exception
C) Throwable
D) Fault
Answer: A) Error


229. How do you create a custom error?

A) throw new Error("message")
B) throw Error("message")
C) throw "message"
D) All of the above
Answer: D) All of the above


230. Which block executes code even if an error occurs?

A) finally
B) catch
C) throw
D) try
Answer: A) finally


231. Which object handles events globally in the browser?

A) window
B) document
C) navigator
D) screen
Answer: A) window


232. Which property returns the number of milliseconds since January 1, 1970?

A) Date.now()
B) Date.getTime()
C) new Date().time()
D) Date.millis()
Answer: A) Date.now()


233. How do you get the current year from a Date object?

A) new Date().getFullYear()
B) new Date().getYear()
C) Date.year()
D) Date.getFullYear()
Answer: A) new Date().getFullYear()


234. How do you get the current month from a Date object?

A) new Date().getMonth()
B) new Date().getMonthNumber()
C) Date.getMonth()
D) new Date().month()
Answer: A) new Date().getMonth()


235. How do you get the current day of the week from a Date object?

A) new Date().getDay()
B) new Date().getWeekday()
C) Date.getDay()
D) new Date().day()
Answer: A) new Date().getDay()

You cannot copy content of this page

Scroll to Top