AI-assisted, automatically checked• Updated August 2026

JavaScript MCQs

JavaScript ES6+, closures, promises, event loop, DOM, and async/await.

48 MCQsProgrammingAll Levels
Key Concept Overview & Exam Context

Review core principles of JavaScript before answering questions. These questions simulate placement technical assessments (TCS, Infosys, Amazon), university exams (CS/IT), and technical certifications.

Target Readiness
Placements & Job Interviews
Question Format
Multiple-Choice with Explanations
Explained Solutions
Written explanations

JavaScript Essentials Quiz

10 questions · Interactive timed test · Instant score & review

JavaScript MCQ Questions

Page 1 of 1 (48 total)
Q1

What is the difference between 'let' and 'var' in JavaScript?

medium
View Answer & Explanation
Correct Answer: 'let' is block-scoped, while 'var' is function-scoped
Explanation:
A variable declared with 'let' inside a block, such as an if statement or loop, is only accessible within that block, while 'var' ignores block boundaries and is instead scoped to the nearest enclosing function.
Q2

What is a closure in JavaScript?

hard
View Answer & Explanation
Correct Answer: A function that retains access to variables from its outer scope even after that outer function has finished executing
Explanation:
Closures are created naturally whenever a function is defined inside another function, and they are commonly used to create private variables or maintain state between function calls.
Q3

What is the difference between '==' and '===' in JavaScript?

easy
View Answer & Explanation
Correct Answer: '===' checks both value and type without converting types, while '==' converts types before comparing
Explanation:
Because '==' performs type coercion, comparisons like 1 == '1' return true, while the strict equality operator '===' would return false since it also checks that both operands are of the same type.
Q4

What is 'hoisting' in JavaScript?

hard
View Answer & Explanation
Correct Answer: A behavior where variable and function declarations are moved to the top of their scope during compilation, before code execution
Explanation:
Because of hoisting, a function declared with the 'function' keyword can be called before its actual definition appears in the code, though variables declared with 'let' or 'const' are hoisted differently and remain inaccessible until their declaration is reached.
Q5

What does the JavaScript 'this' keyword refer to inside a regular function called as a method of an object?

hard
View Answer & Explanation
Correct Answer: The object the method was called on
Explanation:
The value of 'this' in JavaScript depends on how a function is called rather than where it is defined, and when a function is invoked as a method on an object, 'this' refers to that object.
Q6

What is a Promise in JavaScript used for?

medium
View Answer & Explanation
Correct Answer: To represent the eventual completion or failure of an asynchronous operation
Explanation:
A Promise starts in a pending state and eventually resolves to a success value or rejects with an error, which lets asynchronous code, such as a network request, be handled more cleanly than with nested callbacks.
Q7

What does the JavaScript array method 'map()' do?

medium
View Answer & Explanation
Correct Answer: Creates a new array by applying a given function to every element of the original array
Explanation:
map() always returns a new array of the same length as the original, with each element transformed according to the function provided, leaving the original array unchanged.
Q8

What is the purpose of the JavaScript 'filter()' array method?

medium
View Answer & Explanation
Correct Answer: To create a new array containing only the elements that satisfy a given condition
Explanation:
filter() tests each element against a function that returns true or false, and only elements for which the function returns true are included in the resulting new array.
Q9

What is the difference between 'null' and 'undefined' in JavaScript?

medium
View Answer & Explanation
Correct Answer: 'undefined' means a variable has been declared but not yet assigned a value, while 'null' represents an intentional absence of value
Explanation:
JavaScript automatically assigns 'undefined' to a declared variable that has no assigned value, while 'null' must be explicitly assigned by a developer to indicate that a variable intentionally holds no value.
Q10

What is the JavaScript event loop responsible for?

hard
View Answer & Explanation
Correct Answer: Managing the execution of asynchronous callbacks by coordinating the call stack, task queue, and microtask queue
Explanation:
Because JavaScript runs on a single thread, the event loop is what allows asynchronous operations like timers or network requests to be handled without blocking the main thread, by queuing their callbacks to run once the call stack is empty.
Q11

What does an arrow function in JavaScript do differently regarding 'this'?

hard
View Answer & Explanation
Correct Answer: It does not have its own 'this' binding and instead inherits 'this' from its surrounding lexical scope
Explanation:
This behavior makes arrow functions especially useful inside methods where a regular nested function would otherwise lose access to the outer object's 'this' value.
Q12

What is the purpose of JSON.stringify() in JavaScript?

easy
View Answer & Explanation
Correct Answer: To convert a JavaScript object into a JSON formatted string
Explanation:
JSON.stringify() is commonly used when preparing data to be sent over a network or stored as text, since many systems expect data in a plain text JSON format rather than a native JavaScript object.
Q13

What is the reverse operation of JSON.stringify()?

easy
View Answer & Explanation
Correct Answer: JSON.parse()
Explanation:
JSON.parse() takes a JSON formatted string and converts it back into a native JavaScript object or array, which is the opposite of what JSON.stringify() does.
Q14

What does the spread operator (...) allow you to do in JavaScript?

medium
View Answer & Explanation
Correct Answer: Expand an iterable, such as an array, into individual elements, for example when copying or merging arrays
Explanation:
The spread operator is often used to create a shallow copy of an array or object, or to merge multiple arrays together, such as writing [...arr1, ...arr2] to combine two arrays into a new one.
Q15

What is event bubbling in the context of the DOM?

hard
View Answer & Explanation
Correct Answer: A behavior where an event triggered on a nested element also triggers on its parent elements, moving upward through the DOM tree
Explanation:
Because of event bubbling, a click on a button inside a div will also trigger any click event listeners attached to that div and its ancestors, unless the event's propagation is explicitly stopped.
Q16

What is the purpose of the JavaScript 'reduce()' array method?

medium
View Answer & Explanation
Correct Answer: To combine all elements of an array into a single accumulated value based on a provided function
Explanation:
reduce() applies a function against an accumulator and each element of the array in turn, which makes it useful for tasks such as summing all the numbers in an array into a single total.
Q17

What is a template literal in JavaScript?

easy
View Answer & Explanation
Correct Answer: A string literal enclosed in backticks that allows embedded expressions and multi-line strings
Explanation:
Template literals let developers embed variables or expressions directly inside a string using the dollar sign and curly brace syntax, avoiding the need for repeated string concatenation with the plus operator.
Q18

What does the 'typeof' operator return when used on a variable that has been declared but never assigned a value?

easy
View Answer & Explanation
Correct Answer: 'undefined'
Explanation:
Since the variable has been declared but has no assigned value, JavaScript automatically treats it as undefined, and the typeof operator returns the string 'undefined' to reflect that state.
Q19

What is prototypal inheritance in JavaScript?

hard
View Answer & Explanation
Correct Answer: A model where objects can inherit properties and methods directly from other objects through a prototype chain
Explanation:
Unlike classical inheritance found in some other languages, JavaScript objects can inherit directly from other objects, with property lookups falling back to an object's prototype whenever a property is not found directly on the object itself.
Q20

What is the purpose of async/await syntax in JavaScript?

hard
View Answer & Explanation
Correct Answer: To write asynchronous code that reads and behaves more like synchronous code, while still being non-blocking
Explanation:
The 'await' keyword pauses execution of an async function until a Promise resolves, without blocking the rest of the browser, which makes asynchronous code involving Promises easier to read compared to chaining multiple then() calls.
Q21

What is the DOM in the context of web development?

medium
View Answer & Explanation
Correct Answer: A programming interface that represents an HTML document as a structured tree of objects that JavaScript can interact with
Explanation:
The Document Object Model gives JavaScript a way to read and modify the structure, content, and styling of a webpage after it has loaded, which is how dynamic interactions like updating text without reloading the page become possible.
Q22

What is the purpose of the JavaScript 'const' keyword?

easy
View Answer & Explanation
Correct Answer: It declares a variable whose binding cannot be reassigned after it is set
Explanation:
While the binding itself cannot change, if a const variable holds an object or array, the contents of that object or array can still be modified.
Q23

What does the JavaScript 'Array.isArray()' method check?

medium
View Answer & Explanation
Correct Answer: Whether a given value is actually an array
Explanation:
Because typeof returns 'object' for arrays just like it does for plain objects, Array.isArray() provides a more reliable way to specifically confirm something is an array.
Q24

What is the purpose of the JavaScript 'Object.keys()' method?

medium
View Answer & Explanation
Correct Answer: It returns an array containing the property names of a given object
Explanation:
This method is often used together with array methods like forEach or map to loop through an object's properties in a convenient array format.
Q25

What is the difference between synchronous and asynchronous code in JavaScript?

medium
View Answer & Explanation
Correct Answer: Synchronous code runs in order, blocking further execution until it finishes, while asynchronous code allows other code to run while it waits
Explanation:
Because JavaScript is single threaded, asynchronous patterns like callbacks, Promises, and async/await are essential for handling tasks like network requests without freezing the rest of the program.
Q26

What does the JavaScript 'localStorage' object allow a web page to do?

medium
View Answer & Explanation
Correct Answer: It stores key value data in the browser that persists even after the page is closed and reopened
Explanation:
Unlike sessionStorage, which clears when the tab is closed, data saved in localStorage remains available across browser restarts until it is explicitly removed.
Q27

What is the purpose of the JavaScript 'Array.prototype.find()' method?

medium
View Answer & Explanation
Correct Answer: It returns the first element in an array that satisfies a given testing function
Explanation:
If no elements pass the test, find() returns undefined, which makes it different from filter(), which always returns an array, even if that array is empty.
Q28

What does the JavaScript 'try...catch' block do?

easy
View Answer & Explanation
Correct Answer: It lets a program attempt to run code and handle any errors that occur without crashing
Explanation:
Code that might throw an error is placed in the try block, and if something goes wrong, control moves to the catch block where the error can be examined and handled gracefully.
Q29

What is the purpose of the JavaScript 'Number.parseInt()' function?

easy
View Answer & Explanation
Correct Answer: It converts a string into an integer, parsing digits until it reaches a non-numeric character
Explanation:
For example, parseInt('42px') returns 42, since parsing stops as soon as a non-digit character is encountered in the string.
Q30

What is the purpose of the JavaScript 'export' and 'import' keywords used with ES modules?

medium
View Answer & Explanation
Correct Answer: They allow code from one file to be shared with and used in another file
Explanation:
This module system lets developers split code into separate files organized by feature, then explicitly choose which pieces to expose and which to bring into other files.
Q31

What does the JavaScript '||' operator do when used between two values?

medium
View Answer & Explanation
Correct Answer: It returns the first truthy value, or the last value if none of them are truthy
Explanation:
This behavior is often used to provide a default value, such as writing 'name || "Guest"' to fall back to 'Guest' if name happens to be empty or undefined.
Q32

What is the purpose of the JavaScript 'Array.prototype.sort()' method by default?

hard
View Answer & Explanation
Correct Answer: It sorts array elements as strings, based on their character order, unless a custom comparison function is provided
Explanation:
This is why sorting an array like [10, 2, 1] without a comparison function can produce an unexpected order, since '10' comes before '2' when compared as text.
Q33

What is a callback function in JavaScript?

medium
View Answer & Explanation
Correct Answer: A function passed as an argument to another function, to be executed at a later point
Explanation:
Callbacks were historically the main way to handle asynchronous operations in JavaScript, before Promises and async/await offered cleaner alternatives for many use cases.
Q34

What does the JavaScript 'Array.prototype.includes()' method check?

easy
View Answer & Explanation
Correct Answer: Whether a specified value exists anywhere within an array
Explanation:
includes() returns a simple true or false, which is often more readable than checking whether indexOf() returns something other than negative one.
Q35

What is the purpose of destructuring assignment in JavaScript?

medium
View Answer & Explanation
Correct Answer: It allows values from arrays or properties from objects to be unpacked into distinct variables in a concise way
Explanation:
For example, writing 'const {name, age} = person;' pulls the name and age properties directly out of the person object into their own variables in a single line.
Q36

What is the purpose of the JavaScript 'Set' object?

medium
View Answer & Explanation
Correct Answer: It stores a collection of unique values, automatically ignoring duplicate entries
Explanation:
A Set is useful when you want to guarantee that a collection contains no repeated values, such as removing duplicates from an existing array.
Q37

What does the JavaScript 'window' object represent in a browser environment?

medium
View Answer & Explanation
Correct Answer: The global object representing the browser window, giving access to things like the DOM, location, and timers
Explanation:
Because window is the global object in a browser, variables declared globally without a module system effectively become properties attached to it.
Q38

What is the purpose of the JavaScript 'Array.prototype.slice()' method?

medium
View Answer & Explanation
Correct Answer: It returns a shallow copy of a portion of an array without changing the original array
Explanation:
Unlike splice(), which modifies the original array, slice() leaves it untouched and simply returns a new array containing the requested range of elements.
Q39

What does the JavaScript 'setTimeout()' function do?

easy
View Answer & Explanation
Correct Answer: It schedules a function to run once after a specified delay, measured in milliseconds
Explanation:
setTimeout() does not block the rest of the program while it waits, since the delayed function is placed on the event queue to run after the specified time has passed.
Q40

What is the purpose of the JavaScript 'Array.prototype.some()' method?

medium
View Answer & Explanation
Correct Answer: It checks whether at least one element in an array passes a given test, returning true or false
Explanation:
some() stops checking as soon as it finds one element that satisfies the condition, which can make it more efficient than manually looping through the entire array.
Q41

What is the purpose of strict mode, enabled with 'use strict' in JavaScript?

hard
View Answer & Explanation
Correct Answer: It enforces stricter parsing and error handling rules, catching common mistakes that would otherwise be silently ignored
Explanation:
For example, strict mode prevents accidentally creating a global variable by forgetting to declare it with let, const, or var, throwing an error instead.
Q42

What is the purpose of the JavaScript 'Promise.all()' method?

hard
View Answer & Explanation
Correct Answer: It takes an array of Promises and resolves once all of them have resolved, or rejects if any one of them fails
Explanation:
Promise.all() is useful when several independent asynchronous operations need to complete before moving forward, such as fetching data from multiple endpoints at once.
Q43

What does the JavaScript 'Array.prototype.join()' method do?

easy
View Answer & Explanation
Correct Answer: It combines all elements of an array into a single string, separated by a specified separator
Explanation:
By default join() uses a comma as the separator if none is specified, but a custom separator, such as a space or hyphen, can be passed in instead.
Q44

What is the difference between 'null' and '0' when used in a boolean context in JavaScript?

hard
View Answer & Explanation
Correct Answer: Both are treated as falsy values, but they represent conceptually different things, an intentional absence of value versus the number zero
Explanation:
Even though both evaluate to false in a boolean check, using strict equality with '===' shows they are not considered equal to each other, since they are different types.
Q45

What is the purpose of the JavaScript 'querySelector()' method?

easy
View Answer & Explanation
Correct Answer: It returns the first element in the document that matches a specified CSS selector
Explanation:
For selecting multiple matching elements at once, developers typically use the related method querySelectorAll(), which returns a list instead of just the first match.
Q46

What is the purpose of the JavaScript 'Array.prototype.flat()' method?

medium
View Answer & Explanation
Correct Answer: It flattens nested arrays into a single array up to a specified depth
Explanation:
For example, calling flat() on an array containing other arrays combines all their elements into one single level array, which can be handy when dealing with deeply nested data.
Q47

What will typeof NaN return in JavaScript?

easy
View Answer & Explanation
Correct Answer: "number"
Explanation:
In JavaScript, NaN (Not-a-Number) is technically a numeric data type value, so typeof NaN evaluates to "number".
Q48

What will typeof NaN return in JavaScript?

easy
View Answer & Explanation
Correct Answer: "number"
Explanation:
In JavaScript, NaN (Not-a-Number) is technically a numeric data type value, so typeof NaN evaluates to "number".