14 Sep 100 JavaScript MCQ (Multiple Choice Questions) with Answers
Show Answer
Explanation
{ }. var is function-scoped, and set/global are not declaration keywords.2) What is the result of typeof NaN?
Show Answer
Explanation
typeof NaN returns “number”.3) Which operator is used for strict equality comparison (checks both value and type)?
Show Answer
Explanation
== operator coerces the operands before comparing, and = is the assignment operator.4) What will console.log(2 + "2") output?
Show Answer
Explanation
+ is a string, JavaScript performs concatenation: the number 2 is coerced to “2”, giving the string “22”.5) What will console.log(2 - "2") output?
Show Answer
Explanation
- operator only performs numeric subtraction, so the string “2” is coerced to the number 2 and the result is 0 (a number).6) Which method removes the last element from an array and returns it?
Show Answer
Explanation
shift() removes the first element, and push() adds an element to the end.7) Which method adds one or more elements to the beginning of an array?
Show Answer
Explanation
push() adds to the end, and prepend() is not an array method.8) What is the output of Boolean("")?
Show Answer
Explanation
Boolean("") returns false.9) What does the isNaN() function do?
Show Answer
Explanation
isNaN() coerces its argument to a number and returns true if the result is Not-a-Number (NaN). Use Number.isNaN() for a strict, no-coercion check.10) Which built-in object is used to perform mathematical tasks?
Show Answer
Explanation
Math.max(), Math.round() and Math.PI.11) How do you write a comment in JavaScript that spans multiple lines?
Show Answer
Explanation
/* ... */ is a block comment that can span multiple lines. The // syntax comments out only the rest of a single line.12) What will console.log(typeof null) display?
Show Answer
Explanation
typeof null returns “object”, even though null is a primitive value. It is a legacy bug kept for backward compatibility.13) Which method converts a JSON string into a JavaScript object?
Show Answer
Explanation
JSON.stringify() performs the reverse operation.14) What is the output of console.log([] == false)?
Show Answer
Explanation
==, both sides are coerced to primitives: [] becomes the empty string “”, and false becomes 0. The empty string is also coerced to 0, so 0 == 0 is true.15) Which statement is used to stop the execution of a loop?
Show Answer
Explanation
return exits the whole function, not just the loop.16) Which statement skips the rest of the current loop iteration and continues with the next?
Show Answer
Explanation
17) How do you find the length of a string named str?
Show Answer
Explanation
length as a property, not a method, so you write str.length without parentheses.18) Which function returns the character at a specified index in a string?
Show Answer
Explanation
indexOf() does the opposite — it returns the position of a given character or substring.19) What will console.log(1 + 2 + "3") output?
Show Answer
Explanation
1 + 2 is the number 3, then 3 + "3" involves a string, so it concatenates to “33”.20) What will console.log("3" + 1 + 2) output?
Show Answer
Explanation
"3" + 1 concatenates to “31”, then "31" + 2 gives “312”. Once a string is involved, all following + operations concatenate.21) Which company developed JavaScript?
Show Answer
Explanation
22) Which symbol is used for template literals in ES6?
Show Answer
Explanation
${ }.23) What will console.log(typeof []) output?
Show Answer
Explanation
typeof [] is “object”. Use Array.isArray() to reliably test for an array.24) Which array method creates a new array populated with the results of calling a provided function on every element?
Show Answer
Explanation
forEach() returns undefined, filter() returns a subset, and reduce() returns a single value.25) Which array method returns the first element that satisfies a testing function?
Show Answer
Explanation
undefined if none match). filter() returns all matches as an array, and some() returns a boolean.26) What does Array.isArray([1, 2, 3]) return?
Show Answer
Explanation
true here. It is preferred over typeof, which returns “object” for arrays.27) What is the default value of an uninitialized variable declared with let?
Show Answer
Explanation
let variable that is declared but not assigned holds the value undefined.28) What will console.log(3 == "3") evaluate to?
Show Answer
Explanation
== coerces the string “3” into the number 3, so the comparison is true. With === it would be false.29) Which keyword is used to define a constant variable?
Show Answer
Explanation
30) What will console.log(0.1 + 0.2 === 0.3) output?
Show Answer
Explanation
0.1 + 0.2 yields 0.30000000000000004 — not exactly 0.3 — making the strict comparison false.31) Which event occurs when the user clicks on an HTML element?
Show Answer
Explanation
onchange fires when a form value changes, and onmouseover when the pointer enters an element.32) How do you call a function named myFunction?
Show Answer
Explanation
33) What is the scope of a variable declared with var inside a function?
Show Answer
Explanation
var is function-scoped: it is visible anywhere inside the function in which it is declared, and block statements such as if or for do not confine it.34) What feature allows functions to access variables from an outer enclosing scope even after that scope has finished executing?
Show Answer
Explanation
35) What is the outcome of hoisting for var variables?
Show Answer
Explanation
36) What happens if you access a let variable before its declaration?
Show Answer
Explanation
let and const are hoisted but not initialized. Accessing them before the declaration throws a ReferenceError — the period is known as the Temporal Dead Zone.37) Which method joins all elements of an array into a single string?
Show Answer
Explanation
concat() combines arrays, it does not stringify them.38) What does arr.splice(1, 2) do to array arr?
Show Answer
Explanation
splice(start, deleteCount) mutates the original array, here removing 2 elements beginning at index 1 and returning them. slice() is the non-mutating alternative.39) How do you create an object in JavaScript using literal syntax?
Show Answer
Explanation
{ } are the object literal syntax. Square brackets create an array, and () => {} creates an arrow function.40) What will console.log(typeof function(){}) return?
Show Answer
Explanation
typeof gives them their own result: “function”.41) Which method is used to remove whitespace from both ends of a string?
Show Answer
Explanation
trimStart() and trimEnd() handle one side only.42) What will console.log("hello".toUpperCase()) display?
Show Answer
Explanation
toUpperCase() returns a new string whose value is “HELLO”. console.log prints strings without the surrounding quote characters, but the value itself is still the string “HELLO”.43) Which statement is used to handle exceptions in JavaScript?
Show Answer
Explanation
try block runs, and if it throws, control passes to the catch block.44) What does the finally block in a try...catch...finally structure do?
Show Answer
Explanation
finally block always runs — whether an exception was thrown, caught, or never occurred at all. It is typically used for cleanup.45) Which keyword is used to explicitly throw a user-defined exception?
Show Answer
Explanation
throw new Error("Something went wrong");. raise is used in Python, not JavaScript.46) Which function delays the execution of a function by a specified number of milliseconds?
Show Answer
Explanation
setInterval() repeats it instead.47) Which function repeatedly calls a function at fixed time intervals?
Show Answer
Explanation
clearInterval().48) What does clearTimeout() accept as an argument?
Show Answer
Explanation
setTimeout() returns a numeric timer ID; you pass that ID to clearTimeout() to cancel the pending callback.49) What is the value of this in a standard function called in the global context (non-strict mode)?
Show Answer
Explanation
this bound to the global object — window in browsers, globalThis in modern environments.50) What is the value of this in a standard function called in strict mode ("use strict") without an explicit context?
Show Answer
Explanation
this is undefined in a plain function call.51) How do arrow functions handle the this keyword?
Show Answer
Explanation
this; they lexically inherit it from the enclosing scope at the time they are defined.52) Can arrow functions be used as constructors with the new keyword?
Show Answer
Explanation
prototype and no internal [[Construct]] method, so calling one with new throws a TypeError.53) Which method calls a function with a given this value and arguments provided individually?
Show Answer
Explanation
this and arguments listed one by one: fn.call(obj, a, b).54) Which method calls a function with a given this value and arguments provided as an array?
Show Answer
Explanation
call() but takes the arguments as an array (or array-like object): fn.apply(obj, [a, b]).55) Which method creates a new function that, when called, has its this keyword set to a provided value?
Show Answer
Explanation
this permanently bound — it does not invoke the function straight away, unlike call() and apply().56) What does the rest operator (...) do in function parameters?
Show Answer
Explanation
function sum(...nums) {}. In other contexts the same syntax acts as the spread operator.57) What is the result of [1, 2, ...[3, 4]]?
Show Answer
Explanation
[3, 4], producing the flattened array [1, 2, 3, 4].58) How do you extract x and y from an object const obj = {x: 10, y: 20} using object destructuring?
Show Answer
Explanation
const {x, y} = obj;. Square brackets are used for array destructuring.59) What will console.log(typeof Symbol("id")) output?
Show Answer
Explanation
Symbol() creates a primitive of type “symbol”, a unique and immutable value often used as an object property key.60) Are Symbol values unique?
Show Answer
Explanation
Symbol() produces a unique value, even when the descriptions are identical: Symbol("a") !== Symbol("a").61) What object does a Promise represent?
Show Answer
Explanation
62) What are the three states of a JavaScript Promise?
Show Answer
Explanation
63) Which method is attached to a Promise to handle resolution?
Show Answer
Explanation
.catch() handles only rejections.64) Which keyword is used inside an async function to pause execution until a Promise settles?
Show Answer
Explanation
async function (or a module’s top level).65) What does an async function always return?
Show Answer
Explanation
66) Which method executes when all promises in an iterable have resolved, or rejects as soon as one rejects?
Show Answer
Explanation
67) Which method returns a promise that fulfills or rejects as soon as one of the promises in an iterable settles?
Show Answer
Explanation
Promise.any() waits for the first fulfilment only.68) What is the Event Loop responsible for in JavaScript?
Show Answer
Explanation
69) Where are microtasks (like Promise.then callbacks) processed relative to macrotasks (like setTimeout)?
Show Answer
Explanation
setTimeout callbacks.70) What is DOM short for?
Show Answer
Explanation
71) Which DOM method selects the first element matching a specified CSS selector?
Show Answer
Explanation
null if none match). querySelectorAll() returns all matches.72) What type of collection does document.querySelectorAll() return?
Show Answer
Explanation
map, filter, etc. unless converted).73) What is event bubbling in the DOM?
Show Answer
Explanation
74) Which method stops the further propagation of an event in the bubbling/capturing phase?
Show Answer
Explanation
75) Which method prevents the default browser behavior associated with an event (e.g., form submission redirect)?
Show Answer
Explanation
76) Which object property is used to attach a prototype object to a standard object constructor function in ES5?
Show Answer
Explanation
prototype property holds the object that becomes the [[Prototype]] of instances created with new.77) How do you inherit properties from a parent class in an ES6 class definition?
Show Answer
Explanation
extends keyword: class Child extends Parent { } sets up the prototype chain between the two classes.78) Which keyword calls the constructor of a parent class in ES6?
Show Answer
Explanation
this can be used inside a derived class constructor.79) What will console.log(typeof NaN === "number") output?
Show Answer
Explanation
typeof NaN is the string “number”, so the comparison "number" === "number" evaluates to true.80) What is the purpose of "use strict";?
Show Answer
Explanation
this in plain calls change.81) What will console.log(1 == true) return?
Show Answer
Explanation
==, the boolean true is coerced to the number 1, so 1 == 1 is true.82) What will console.log(1 === true) return?
Show Answer
Explanation
83) Which built-in data structure stores unique values of any type?
Show Answer
Explanation
84) In a Map object, what types can be used as keys?
Show Answer
Explanation
85) What is the key characteristic of a WeakSet?
Show Answer
Explanation
size property.86) Which operator checks if a property exists in an object or its prototype chain?
Show Answer
Explanation
in operator returns true if the property exists on the object or anywhere in its prototype chain. Use hasOwnProperty() to check only the object itself.87) What does Object.freeze(obj) do?
Show Answer
Explanation
88) What does Object.seal(obj) allow that Object.freeze(obj) does not?
Show Answer
Explanation
89) What is the output of console.log(typeof (void 0))?
Show Answer
Explanation
void operator evaluates its operand and returns undefined, so void 0 is undefined and typeof yields “undefined”.90) What will console.log(Math.max()) return?
Show Answer
Explanation
Math.max() returns -Infinity, the identity element for a maximum operation (any real number is greater than it).91) What will console.log(Math.min()) return?
Show Answer
Explanation
Math.min() returns Infinity, the identity element for a minimum operation.92) What is the output of console.log(10 ?? 20) (Nullish Coalescing Operator)?
Show Answer
Explanation
?? operator returns the left operand unless it is null or undefined. Since 10 is neither, the result is 10.93) What is the output of console.log(null ?? 20)?
Show Answer
Explanation
null, the nullish coalescing operator falls back to the right-hand side and returns 20.94) What values trigger the right-hand side of the ?? operator?
Show Answer
Explanation
||, the nullish coalescing operator only falls back for null and undefined. Other falsy values such as 0, "" and false pass through unchanged.95) What feature does user?.address?.city represent?
Show Answer
Explanation
?.) short-circuits and returns undefined if any link in the chain is null or undefined, instead of throwing a TypeError.96) What is the output of console.log(typeof typeof 1)?
Show Answer
Explanation
typeof 1 is the string “number”, and typeof "number" is “string”.97) What is the output of console.log(a) if declared as var a = 5; on the line below it?
Show Answer
Explanation
var declarations are hoisted and initialized to undefined, so the log prints undefined rather than throwing. (With let it would be a ReferenceError.)98) What will console.log([1, 2] + [3, 4]) evaluate to?
Show Answer
Explanation
+ operator coerces both arrays to strings — “1,2” and “3,4” — and concatenates them, producing “1,23,4”.99) Which API allows client-side scripts to store key-value pairs locally with no expiration time?
Show Answer
Explanation
sessionStorage is cleared when the tab or browser session ends.100) How do you remove an item named "token" from localStorage?
Show Answer
Explanation
localStorage.clear() takes no arguments and would wipe every stored key.
No Comments