100 JavaScript MCQ (Multiple Choice Questions) with Answers

1) Which keyword is used to declare a block-scoped variable in JavaScript?
  1. var
  2. let
  3. set
  4. global
Show Answer
Answer: b
Explanation
let declares a variable that is scoped to the enclosing block { }. var is function-scoped, and set/global are not declaration keywords.


2) What is the result of typeof NaN?

  1. “number”
  2. “nan”
  3. “undefined”
  4. “object”
Show Answer
Answer: a
Explanation
NaN means “Not-a-Number”, but it is still a value of the Number type, so typeof NaN returns “number”.


3) Which operator is used for strict equality comparison (checks both value and type)?

  1. ==
  2. =
  3. ===
  4. !==
Show Answer
Answer: c
Explanation
=== compares both value and type with no type coercion. The == operator coerces the operands before comparing, and = is the assignment operator.


4) What will console.log(2 + "2") output?

  1. 4
  2. 22
  3. NaN
  4. TypeError
Show Answer
Answer: b
Explanation
When one operand of + 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?

  1. 0
  2. “0”
  3. 22
  4. NaN
Show Answer
Answer: a
Explanation
The - 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?

  1. shift()
  2. pop()
  3. push()
  4. slice()
Show Answer
Answer: b
Explanation
pop() removes and returns the last element of an array. 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?

  1. unshift()
  2. push()
  3. prepend()
  4. concat()
Show Answer
Answer: a
Explanation
unshift() adds one or more elements to the front of an array and returns the new length. push() adds to the end, and prepend() is not an array method.


8) What is the output of Boolean("")?

  1. true
  2. false
  3. null
  4. undefined
Show Answer
Answer: b
Explanation
The empty string is one of JavaScript’s falsy values, so Boolean("") returns false.


9) What does the isNaN() function do?

  1. Checks if a value is a number
  2. Checks if a value is Not-a-Number
  3. Converts a string to a number
  4. Returns true for all strings
Show Answer
Answer: b
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?

  1. Math
  2. Number
  3. Calc
  4. Numeric
Show Answer
Answer: a
Explanation
The built-in Math object provides mathematical constants and functions such as Math.max(), Math.round() and Math.PI.


11) How do you write a comment in JavaScript that spans multiple lines?

  1. // comment
  2. /* comment */
  3. ' comment
Show Answer
Answer: c
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?

  1. “null”
  2. “undefined”
  3. “object”
  4. “number”
Show Answer
Answer: c
Explanation
This is a long-standing JavaScript quirk: 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?

  1. JSON.stringify()
  2. JSON.parse()
  3. JSON.toObject()
  4. JSON.convert()
Show Answer
Answer: b
Explanation
JSON.parse() turns a JSON-formatted string into a JavaScript object. JSON.stringify() performs the reverse operation.


14) What is the output of console.log([] == false)?

  1. true
  2. false
  3. TypeError
  4. undefined
Show Answer
Answer: a
Explanation
With ==, 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?

  1. stop
  2. exit
  3. break
  4. return
Show Answer
Answer: c
Explanation
break terminates the innermost loop (or switch) immediately. 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?

  1. skip
  2. pass
  3. continue
  4. next
Show Answer
Answer: c
Explanation
continue jumps straight to the next iteration of the loop, skipping any remaining statements in the current pass.


17) How do you find the length of a string named str?

  1. str.length()
  2. str.length
  3. len(str)
  4. str.size
Show Answer
Answer: b
Explanation
Strings expose 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?

  1. charAt()
  2. getChar()
  3. characterAt()
  4. indexOf()
Show Answer
Answer: a
Explanation
charAt(index) returns the character at the given position. indexOf() does the opposite — it returns the position of a given character or substring.


19) What will console.log(1 + 2 + "3") output?

  1. “123”
  2. “33”
  3. 6
  4. NaN
Show Answer
Answer: b
Explanation
Evaluation is left to right: 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?

  1. “33”
  2. “312”
  3. 6
  4. NaN
Show Answer
Answer: b
Explanation
Left to right again: "3" + 1 concatenates to “31”, then "31" + 2 gives “312”. Once a string is involved, all following + operations concatenate.


21) Which company developed JavaScript?

  1. Microsoft
  2. Netscape
  3. Sun Microsystems
  4. Google
Show Answer
Answer: b
Explanation
JavaScript was created by Brendan Eich at Netscape in 1995 (originally named Mocha, then LiveScript).


22) Which symbol is used for template literals in ES6?

  1. Single quotes (‘ ‘)
  2. Double quotes (” “)
  3. Backticks (` `)
  4. Forward slashes (/ /)
Show Answer
Answer: c
Explanation
Template literals use backticks and support multi-line strings plus interpolation with ${ }.


23) What will console.log(typeof []) output?

  1. “array”
  2. “object”
  3. “list”
  4. “undefined”
Show Answer
Answer: b
Explanation
Arrays are a kind of object in JavaScript, so 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?

  1. forEach()
  2. filter()
  3. map()
  4. reduce()
Show Answer
Answer: c
Explanation
map() returns a new array of the same length, built by applying the callback to each element. 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?

  1. find()
  2. filter()
  3. search()
  4. some()
Show Answer
Answer: a
Explanation
find() returns the first matching element (or 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?

  1. true
  2. false
  3. undefined
  4. null
Show Answer
Answer: a
Explanation
Array.isArray() reliably tests whether a value is an array and returns 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?

  1. null
  2. 0
  3. undefined
  4. false
Show Answer
Answer: c
Explanation
A let variable that is declared but not assigned holds the value undefined.


28) What will console.log(3 == "3") evaluate to?

  1. true
  2. false
  3. TypeError
  4. NaN
Show Answer
Answer: a
Explanation
The loose equality operator == 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?

  1. static
  2. const
  3. immutable
  4. final
Show Answer
Answer: b
Explanation
const declares a block-scoped binding that cannot be reassigned (though object contents can still be mutated).


30) What will console.log(0.1 + 0.2 === 0.3) output?

  1. true
  2. false
  3. undefined
  4. NaN
Show Answer
Answer: b
Explanation
Floating-point numbers are stored in binary, so 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?

  1. onchange
  2. onmouseover
  3. onclick
  4. onmouseclick
Show Answer
Answer: c
Explanation
onclick fires when the user clicks an element. onchange fires when a form value changes, and onmouseover when the pointer enters an element.


32) How do you call a function named myFunction?

  1. call myFunction()
  2. myFunction()
  3. call function myFunction()
  4. execute myFunction()
Show Answer
Answer: b
Explanation
A function is invoked simply by writing its name followed by parentheses: myFunction().


33) What is the scope of a variable declared with var inside a function?

  1. Block scope
  2. Function scope
  3. Global scope
  4. Module scope
Show Answer
Answer: b
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?

  1. Hoisting
  2. Closure
  3. Prototype
  4. Callback
Show Answer
Answer: b
Explanation
A closure is a function together with the lexical environment in which it was created, letting it “remember” outer variables after the outer function has returned.


35) What is the outcome of hoisting for var variables?

  1. The declaration and initialization are both moved to the top.
  2. Only the declaration is moved to the top, initialized as undefined.
  3. A ReferenceError is thrown if accessed early.
  4. Variables are moved to the bottom of the scope.
Show Answer
Answer: b
Explanation
Only the declaration is hoisted; the assignment stays in place. Until that line executes, the variable holds undefined rather than throwing.


36) What happens if you access a let variable before its declaration?

  1. Returns undefined
  2. Throws a ReferenceError (Temporal Dead Zone)
  3. Returns null
  4. Returns 0
Show Answer
Answer: b
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?

  1. join()
  2. concat()
  3. toString()
  4. merge()
Show Answer
Answer: a
Explanation
join() concatenates all elements into one string using an optional separator (default is a comma). concat() combines arrays, it does not stringify them.


38) What does arr.splice(1, 2) do to array arr?

  1. Copies 2 elements starting from index 1
  2. Removes 2 elements starting from index 1
  3. Inserts 2 elements at index 1
  4. Returns a slice of the array from index 1 to 2
Show Answer
Answer: b
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?

  1. let obj = [];
  2. let obj = {};
  3. let obj = () => {};
  4. let obj = Object();
Show Answer
Answer: b
Explanation
Curly braces { } are the object literal syntax. Square brackets create an array, and () => {} creates an arrow function.


40) What will console.log(typeof function(){}) return?

  1. “object”
  2. “function”
  3. “method”
  4. “undefined”
Show Answer
Answer: b
Explanation
Functions are first-class objects in JavaScript, but typeof gives them their own result: “function”.


41) Which method is used to remove whitespace from both ends of a string?

  1. strip()
  2. trim()
  3. clean()
  4. cut()
Show Answer
Answer: b
Explanation
trim() returns a new string with whitespace removed from both the start and the end. trimStart() and trimEnd() handle one side only.


42) What will console.log("hello".toUpperCase()) display?

  1. HELLO
  2. Hello
  3. “HELLO”
  4. TypeError
Show Answer
Answer: c
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?

  1. try…catch
  2. do…except
  3. throw…catch
  4. catch…finally
Show Answer
Answer: a
Explanation
try…catch is the standard error-handling structure: code in the 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?

  1. Runs only if an exception is caught
  2. Runs only if no exception occurs
  3. Runs regardless of whether an exception was thrown or caught
  4. Prevents any code after it from executing
Show Answer
Answer: c
Explanation
The 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?

  1. error
  2. throw
  3. raise
  4. dispatch
Show Answer
Answer: b
Explanation
throw raises an exception, e.g. 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?

  1. setInterval()
  2. setTimer()
  3. setTimeout()
  4. delay()
Show Answer
Answer: c
Explanation
setTimeout(fn, ms) schedules a callback to run once after the given delay. setInterval() repeats it instead.


47) Which function repeatedly calls a function at fixed time intervals?

  1. setRepeat()
  2. setInterval()
  3. setTimeout()
  4. loopTimer()
Show Answer
Answer: b
Explanation
setInterval(fn, ms) calls the function repeatedly every ms milliseconds until it is stopped with clearInterval().


48) What does clearTimeout() accept as an argument?

  1. The function name
  2. The delay time
  3. The timer ID returned by setTimeout()
  4. No arguments
Show Answer
Answer: c
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)?

  1. undefined
  2. The global object (e.g., window)
  3. null
  4. The function itself
Show Answer
Answer: b
Explanation
In non-strict mode, a plain function call has this bound to the global objectwindow 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?

  1. window
  2. undefined
  3. null
  4. globalThis
Show Answer
Answer: b
Explanation
Strict mode removes the automatic global binding, so this is undefined in a plain function call.


51) How do arrow functions handle the this keyword?

  1. They define their own dynamic this
  2. They bind this to the global object always
  3. They lexically inherit this from the surrounding scope
  4. They do not have access to any scope
Show Answer
Answer: c
Explanation
Arrow functions do not have their own 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?

  1. Yes
  2. No
  3. Only if they return an object
  4. Only in strict mode
Show Answer
Answer: b
Explanation
Explanation
No. Arrow functions have no 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?

  1. apply()
  2. call()
  3. bind()
  4. execute()
Show Answer
Answer: b
Explanation
call() invokes the function immediately with a specified 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?

  1. apply()
  2. call()
  3. bind()
  4. set()
Show Answer
Answer: a
Explanation
apply() is like 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?

  1. apply()
  2. call()
  3. bind()
  4. attach()
Show Answer
Answer: c
Explanation
bind() returns a new function with 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?

  1. Spreads an array into individual elements
  2. Collects remaining arguments into an array
  3. Resets all parameters to undefined
  4. Merges two functions together
Show Answer
Answer: b
Explanation
Explanation
In a parameter list, the rest operator gathers the remaining arguments into a real array, e.g. function sum(...nums) {}. In other contexts the same syntax acts as the spread operator.


57) What is the result of [1, 2, ...[3, 4]]?

  1. [1, 2, [3, 4]]
  2. [1, 2, 3, 4]
  3. [1, 2, 7]
  4. SyntaxError
Show Answer
Answer: b
Explanation
Inside an array literal the spread operator expands the elements of [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?

  1. const (x, y) = obj;
  2. const {x, y} = obj;
  3. const [x, y] = obj;
  4. const x, y = obj;
Show Answer
Answer: b
Explanation
Object destructuring uses curly braces and the property names: const {x, y} = obj;. Square brackets are used for array destructuring.


59) What will console.log(typeof Symbol("id")) output?

  1. “string”
  2. “symbol”
  3. “object”
  4. “identifier”
Show Answer
Answer: b
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?

  1. Yes, every Symbol() call creates a guaranteed unique value
  2. No, symbols with the same description are identical
  3. Only if created with let
  4. Only in ES5
Show Answer
Answer: a
Explanation
Every call to Symbol() produces a unique value, even when the descriptions are identical: Symbol("a") !== Symbol("a").


61) What object does a Promise represent?

  1. An synchronous operation
  2. The eventual completion (or failure) of an asynchronous operation
  3. A DOM tree node
  4. An HTTP request header
Show Answer
Answer: b
Explanation
A Promise is a placeholder for the eventual result (or failure) of an asynchronous operation and its resulting value.


62) What are the three states of a JavaScript Promise?

  1. Starting, Running, Stopped
  2. Pending, Fulfilled, Rejected
  3. Active, Waiting, Done
  4. Open, Processing, Closed
Show Answer
Answer: b
Explanation
A promise starts as pending and then settles permanently as either fulfilled (resolved) or rejected.


63) Which method is attached to a Promise to handle resolution?

  1. .catch()
  2. .then()
  3. .finally()
  4. .resolve()
Show Answer
Answer: b
Explanation
.then() registers a handler for the fulfilled value (and can take a second handler for rejection). .catch() handles only rejections.


64) Which keyword is used inside an async function to pause execution until a Promise settles?

  1. wait
  2. pause
  3. await
  4. defer
Show Answer
Answer: c
Explanation
await pauses the async function, returning the resolved value of the promise or throwing its rejection. It can only be used inside an async function (or a module’s top level).


65) What does an async function always return?

  1. A Promise
  2. undefined
  3. The raw value
  4. A callback function
Show Answer
Answer: a
Explanation
An async function always returns a Promise. A returned value is wrapped in a resolved promise, and a thrown error becomes a rejected promise.


66) Which method executes when all promises in an iterable have resolved, or rejects as soon as one rejects?

  1. Promise.any()
  2. Promise.race()
  3. Promise.all()
  4. Promise.allSettled()
Show Answer
Answer: c
Explanation
Promise.all() fulfils with an array of all results once every promise resolves, and rejects immediately if any one of them rejects.


67) Which method returns a promise that fulfills or rejects as soon as one of the promises in an iterable settles?

  1. Promise.race()
  2. Promise.all()
  3. Promise.any()
  4. Promise.every()
Show Answer
Answer: a
Explanation
Promise.race() settles as soon as the first promise settles — adopting its value or its rejection reason. Promise.any() waits for the first fulfilment only.


68) What is the Event Loop responsible for in JavaScript?

  1. Executing synchronous code line by line
  2. Monitoring the Call Stack and Task Queue to execute asynchronous callbacks
  3. Garbage collection of unused variables
  4. Compiling JS into machine code
Show Answer
Answer: b
Explanation
The event loop continuously checks whether the call stack is empty and, if so, moves queued callbacks from the task and microtask queues onto the stack.


69) Where are microtasks (like Promise.then callbacks) processed relative to macrotasks (like setTimeout)?

  1. After all macrotasks finish
  2. Immediately before the next macrotask is processed from the queue
  3. Concurrently on a parallel thread
  4. Microtasks are always converted to macrotasks
Show Answer
Answer: b
Explanation
After each macrotask, the entire microtask queue is drained before the next macrotask runs — so promise callbacks always execute before setTimeout callbacks.


70) What is DOM short for?

  1. Data Object Model
  2. Document Object Model
  3. Desktop Oriented Mode
  4. Digital Ordinance Mapping
Show Answer
Answer: b
Explanation
DOM stands for Document Object Model — the tree-structured, programmatic representation of an HTML or XML document.


71) Which DOM method selects the first element matching a specified CSS selector?

  1. document.getElementByClass()
  2. document.querySelector()
  3. document.querySelectorAll()
  4. document.getElementById()
Show Answer
Answer: b
Explanation
document.querySelector() returns the first element that matches any valid CSS selector (or null if none match). querySelectorAll() returns all matches.


72) What type of collection does document.querySelectorAll() return?

  1. HTMLCollection
  2. NodeList
  3. Array
  4. Set
Show Answer
Answer: b
Explanation
It returns a static NodeList. A NodeList is array-like and iterable but is not a real Array (no map, filter, etc. unless converted).


73) What is event bubbling in the DOM?

  1. Events trigger from the target element upward to parent elements
  2. Events trigger from the top window node downward to target elements
  3. Events trigger simultaneously across all nodes
  4. Events refresh the DOM tree
Show Answer
Answer: a
Explanation
Explanation
In the bubbling phase the event fires first on the target element and then propagates upward through its ancestors. The opposite direction (window down to target) is the capturing phase.


74) Which method stops the further propagation of an event in the bubbling/capturing phase?

  1. event.preventDefault()
  2. event.stopPropagation()
  3. event.stop()
  4. event.cancel()
Show Answer
Answer: b
Explanation
event.stopPropagation() stops the event from travelling further up or down the DOM tree. It does not cancel the element’s default behaviour.


75) Which method prevents the default browser behavior associated with an event (e.g., form submission redirect)?

  1. event.preventDefault()
  2. event.stopPropagation()
  3. event.halt()
  4. event.block()
Show Answer
Answer: a
Explanation
event.preventDefault() cancels the browser’s built-in action for the event — for example following a link or submitting a form — while the event still propagates.


76) Which object property is used to attach a prototype object to a standard object constructor function in ES5?

  1. __proto__
  2. prototype
  3. [[Prototype]]
  4. parent
Show Answer
Answer: b
Explanation
A constructor function’s 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?

  1. class Child inherits Parent
  2. class Child extends Parent
  3. class Child implements Parent
  4. class Child includes Parent
Show Answer
Answer: b
Explanation
ES6 uses the 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?

  1. parent()
  2. super()
  3. base()
  4. this.parent()
Show Answer
Answer: b
Explanation
super() calls the parent class’s constructor and must be invoked before this can be used inside a derived class constructor.


79) What will console.log(typeof NaN === "number") output?

  1. true
  2. false
  3. undefined
  4. TypeError
Show Answer
Answer: a
Explanation
typeof NaN is the string “number”, so the comparison "number" === "number" evaluates to true.


80) What is the purpose of "use strict";?

  1. To enable strict typing like TypeScript
  2. To enforce stricter parsing and error handling in code
  3. To prevent asynchronous operations from executing
  4. To automatically minify code
Show Answer
Answer: b
Explanation
Explanation
“use strict” opts into a stricter variant of JavaScript: silent errors become thrown errors, undeclared variables are disallowed, and semantics such as this in plain calls change.


81) What will console.log(1 == true) return?

  1. true
  2. false
  3. NaN
  4. TypeError
Show Answer
Answer: a
Explanation
With the loose operator ==, the boolean true is coerced to the number 1, so 1 == 1 is true.


82) What will console.log(1 === true) return?

  1. true
  2. false
  3. undefined
  4. NaN
Show Answer
Answer: b
Explanation
Strict equality does not coerce types. A number and a boolean are different types, so the result is false.


83) Which built-in data structure stores unique values of any type?

  1. Map
  2. Set
  3. WeakMap
  4. Object
Show Answer
Answer: b
Explanation
A Set is a collection of unique values of any type; duplicate insertions are automatically ignored. A Map, by contrast, stores key–value pairs.


84) In a Map object, what types can be used as keys?

  1. Strings only
  2. Strings and Symbols only
  3. Any value (objects, functions, primitives)
  4. Integers only
Show Answer
Answer: c
Explanation
Any value can be a Map key — objects, functions and primitives alike — unlike plain objects, whose keys are limited to strings and symbols.


85) What is the key characteristic of a WeakSet?

  1. It can store primitives as well as objects
  2. Its values must be objects, and references are held weakly (garbage collectible)
  3. It maintains an ordered index of elements
  4. It can be iterated using a for...of loop
Show Answer
Answer: b
Explanation
WeakSet members must be objects and are held weakly, so they do not prevent garbage collection. WeakSets are not iterable and have no size property.


86) Which operator checks if a property exists in an object or its prototype chain?

  1. has
  2. in
  3. exists
  4. contains
Show Answer
Answer: b
Explanation
The 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?

  1. Prevents adding new properties, but allows modifying existing ones
  2. Makes an object completely immutable (prevents adding, deleting, or modifying properties)
  3. Converts object keys to lower case
  4. Hides all properties from iteration
Show Answer
Answer: b
Explanation
Explanation
Object.freeze() makes an object immutable: properties cannot be added, removed or changed, and its prototype cannot be reassigned. The freeze is shallow.


88) What does Object.seal(obj) allow that Object.freeze(obj) does not?

  1. Adding new properties
  2. Modifying existing property values
  3. Deleting existing properties
  4. Changing prototype
Show Answer
Answer: b
Explanation
Object.seal() prevents adding or deleting properties but still allows modifying the values of existing properties, which freeze does not.


89) What is the output of console.log(typeof (void 0))?

  1. “undefined”
  2. “void”
  3. “null”
  4. “object”
Show Answer
Answer: a
Explanation
The 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?

  1. 0
  2. Infinity
  3. -Infinity
  4. NaN
Show Answer
Answer: c
Explanation
Explanation
Called with no arguments, 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?

  1. 0
  2. Infinity
  3. -Infinity
  4. NaN
Show Answer
Answer: b
Explanation
Called with no arguments, Math.min() returns Infinity, the identity element for a minimum operation.


92) What is the output of console.log(10 ?? 20) (Nullish Coalescing Operator)?

  1. 10
  2. 20
  3. true
  4. undefined
Show Answer
Answer: a
Explanation
The ?? 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)?

  1. null
  2. 20
  3. undefined
  4. TypeError
Show Answer
Answer: b
Explanation
Because the left operand is 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?

  1. false, 0, "", null, undefined (all falsy values)
  2. null and undefined only
  3. false and 0 only
  4. NaN only
Show Answer
Answer: b
Explanation
Explanation
Unlike ||, 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?

  1. Nullish coalescing
  2. Optional chaining
  3. Ternary evaluation
  4. Computed property names
Show Answer
Answer: b
Explanation
Optional chaining (?.) 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)?

  1. “number”
  2. “string”
  3. “undefined”
  4. “object”
Show Answer
Answer: b
Explanation
Explanation
It is evaluated inside out: 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?

  1. 5
  2. undefined
  3. ReferenceError
  4. null
Show Answer
Answer: b
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?

  1. [1, 2, 3, 4]
  2. "1,23,4"
  3. NaN
  4. TypeError
Show Answer
Answer: b
Explanation
Explanation
The + 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?

  1. sessionStorage
  2. localStorage
  3. cookie
  4. indexedDB
Show Answer
Answer: b
Explanation
Explanation
localStorage persists string key–value pairs indefinitely until they are explicitly removed. sessionStorage is cleared when the tab or browser session ends.


100) How do you remove an item named "token" from localStorage?

  1. localStorage.delete(“token”)
  2. localStorage.removeItem(“token”)
  3. localStorage.clear(“token”)
  4. localStorage.exclude(“token”)
Show Answer
Answer: b
Explanation
Explanation
localStorage.removeItem(“token”) deletes that single key. localStorage.clear() takes no arguments and would wipe every stored key.
100 Bootstrap MCQ (Multiple Choice Questions) with Answers
100 jQuery MCQ (Multiple Choice Questions) with Answers
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment