100 JavaScript MCQ Questions and Answers. 100 objective questions on JavaScript basics. Test understanding of syntax, data types, functions, loops & DOM interaction. Answers provided.
100 JavaScript MCQ Questions and Answers – Mock Online Test
Question 1: Which of the following best describes JavaScript?
Show Explanation
Correct Answer: C. A scripting language used to add interactivity to web pages. JavaScript is a scripting language that enables dynamic and interactive elements on websites.
Question 2: Who created JavaScript?
Show Explanation
Correct Answer: B. Brendan Eich. Brendan Eich created JavaScript while working at Netscape Communications.
Question 3: Which of the following is NOT a common use of JavaScript in web development?
Show Explanation
Correct Answer: C. Defining the structure and content of a web page. Defining the structure and content of a web page is primarily done with HTML, not JavaScript.
Question 4: Where does client-side JavaScript code execute?
Show Explanation
Correct Answer: A. On the user’s web browser. Client-side JavaScript runs within the user’s web browser.
Question 5: What is the relationship between JavaScript and ECMAScript?
Show Explanation
Correct Answer: C. ECMAScript is the standardized specification that JavaScript is based on. ECMAScript provides the standards that JavaScript implementations adhere to.
Question 6: Which of the following is NOT a primitive data type in JavaScript?
Show Explanation
Correct Answer: C. Object. An object is a complex data type, not a primitive one.
Question 7: What is the output of typeof null
in JavaScript?
Show Explanation
Correct Answer: C. “object”. This is a quirk of JavaScript; typeof null
returns “object”.
Question 8: What does the symbol
data type represent in JavaScript?
Show Explanation
Correct Answer: B. A unique and immutable value. Symbols are primarily used as unique property keys in objects.
Question 9: Which keyword is used to declare a variable with block scope in JavaScript?
Show Explanation
Correct Answer: D. Both B and C. Both let
and const
declare variables with block scope.
Question 10: What is the difference between let
and const
in JavaScript?
Show Explanation
Correct Answer: A. let
can be reassigned, while const
cannot. let
allows for reassignment of a variable, whereas const
creates a read-only variable.
Question 11: What will happen when you try to reassign a const
variable?
Show Explanation
Correct Answer: C. It will throw a TypeError. Reassigning a const
variable results in a TypeError.
Question 12: What is type coercion in JavaScript?
Show Explanation
Correct Answer: B. Implicitly converting one data type to another. Type coercion is JavaScript’s automatic conversion of values from one data type to another.
Question 13: What is the result of the expression 5 + "5"
in JavaScript?
Show Explanation
Correct Answer: C. “55”. The number 5 is coerced into a string, resulting in string concatenation.
Question 14: Which function can be used to convert a string to a number in JavaScript?
Show Explanation
Correct Answer: D. Both B and C. Both parseInt()
and parseFloat()
convert strings to numbers, with the latter handling decimals.
Question 15: What does the modulus operator (%) do in JavaScript?
Show Explanation
Correct Answer: B. Divides two numbers and returns the remainder. The modulus operator gives the remainder after division.
Question 16: What is the result of the expression 10 < 5 || 5 > 2
?
Show Explanation
Correct Answer: A. true. The expression evaluates to true because 5 > 2 is true, and the || operator only needs one condition to be true.
Question 17: Which operator is used to assign a value to a variable in JavaScript?
Show Explanation
Correct Answer: C. =
. The single equals sign (=
) is the assignment operator.
Question 18: What does the strict equality operator (===
) check for in JavaScript?
Show Explanation
Correct Answer: C. Both value and type equality. The strict equality operator ensures that both the value and the data type are the same.
Question 19: What is the difference between ==
and ===
in JavaScript?
Show Explanation
Correct Answer: A. ==
performs type coercion, while ===
does not. The double equals (==
) allows for type coercion, while the triple equals (===
) requires the types to be the same.
Question 20: In the expression 5 + 4 * 2
, which operation is performed first?
Show Explanation
Correct Answer: B. Multiplication. Multiplication has higher precedence than addition.
Question 21: What does operator associativity determine in JavaScript?
Show Explanation
Correct Answer: A. The order of operations when operators have the same precedence. Associativity defines the direction of operation when operators have equal precedence.
Question 22: What is the purpose of the conditional (ternary) operator in JavaScript?
Show Explanation
Correct Answer: C. To provide a shorthand way to write an if-else statement. The ternary operator offers a concise way to express conditional logic.
Question 23: What is a string literal in JavaScript?
Show Explanation
Correct Answer: B. A sequence of characters enclosed in single or double quotes. String literals are how you represent text within your code.
Question 24: Which property of a string returns its length in JavaScript?
Show Explanation
Correct Answer: B. length
. The length
property provides the number of characters in a string.
Question 25: What does the toUpperCase()
method do to a string in JavaScript?
Show Explanation
Correct Answer: B. Converts the string to uppercase. toUpperCase()
transforms all characters in a string to uppercase.
Question 26: How do you concatenate two strings in JavaScript?
Show Explanation
Correct Answer: C. Both A and B. You can concatenate strings with either the +
operator or the concat()
method.
Question 27: Which method is used to extract a portion of a string in JavaScript?
Show Explanation
Correct Answer: D. All of the above. All three methods (substring()
, slice()
, and substr()
) can be used to extract parts of a string, each with slightly different behaviors.
Question 28: What is the value of NaN
in JavaScript?
Show Explanation
Correct Answer: C. A special value indicating an invalid number operation. NaN
signifies that a mathematical operation did not result in a meaningful number.
Question 29: Which method is used to round a number to the nearest integer in JavaScript?
Show Explanation
Correct Answer: C. Math.round()
. Math.round()
rounds a number to the nearest integer.
Question 30: What are the two possible values of a boolean in JavaScript?
Show Explanation
Correct Answer: C. true and false. Booleans hold either true
or false
.
Question 31: What is a “truthy” value in JavaScript?
Show Explanation
Correct Answer: B. Any value that evaluates to true
in a boolean context. Truthy values are treated as true
in conditions.
Question 32: Which of the following is a “falsy” value in JavaScript?
Show Explanation
Correct Answer: D. All of the above. All listed values are considered falsy in JavaScript.
Question 33: How do you create an empty array in JavaScript?
Show Explanation
Correct Answer: C. Both A and B.
Question 34: Which index represents the first element in a JavaScript array?
Show Explanation
Correct Answer: B. 0. JavaScript arrays are zero-indexed.
Question 35: What does the push()
method do to an array in JavaScript?
Show Explanation
Correct Answer: B. Adds an element to the end of the array. push()
appends an element to the end.
Question 36: What does the pop()
method do to an array?
Show Explanation
Correct Answer: D. Removes the last element from the array. pop()
removes and returns the last element.
Question 37: Which array method is used to add an element to the beginning of an array?
Show Explanation
Correct Answer: B. unshift()
. unshift()
adds elements to the front of an array.
Question 38: What does the splice()
method allow you to do with an array?
Show Explanation
Correct Answer: A. Add or remove elements at any position. splice()
is versatile for adding and removing elements at specific indices.
Question 39: Which loop is specifically designed for iterating over the properties of an object in JavaScript?
Show Explanation
Correct Answer: B. for...in
loop. The for...in
loop is used to loop through the keys of an object.
Question 40: What does the forEach()
method do in JavaScript?
Show Explanation
Correct Answer: A. Executes a provided function once for each array element. forEach()
iterates over array elements and applies a function to each.
Question 41: How do you create an empty object in JavaScript?
Show Explanation
Correct Answer: C. Both A and B. Both the object literal ({}
) and the Object()
constructor create objects.
Question 42: What is the purpose of the this
keyword within an object in JavaScript?
Show Explanation
Correct Answer: A. It refers to the current object. this
provides a way to access properties and methods within the object itself.
Question 43: What are the two ways to access properties of an object in JavaScript?
Show Explanation
Correct Answer: A. Dot notation and bracket notation.
Question 44: What does an if
statement do in JavaScript?
Show Explanation
Correct Answer: A. Executes a block of code only if a specified condition is true. if
statements control code execution based on a condition.
Question 45: What is the purpose of the else
clause in an if-else
statement?
Show Explanation
Correct Answer: B. To specify a block of code to execute if the if
condition is false. The else
block provides an alternative path when the if
condition isn’t met.
Question 46: When is the code inside an else if
block executed?
Show Explanation
Correct Answer: C. Only if the preceding if
condition is false and the else if
condition is true. else if
provides additional conditional checks when the previous if
is false.
Question 47: What is the purpose of a switch
statement in JavaScript?
Show Explanation
Correct Answer: B. To provide an alternative to using multiple if-else if
statements. switch
statements offer a cleaner way to handle multiple possible values of a variable.
Question 48: What does the break
keyword do within a switch
statement?
Show Explanation
Correct Answer: A. Exits the entire switch
statement. break
ensures that only the matching case’s code is executed.
Question 49: What happens if you omit the break
keyword in a switch
statement?
Show Explanation
Correct Answer: B. The code execution continues to the next case, even if it doesn’t match. Without break
, execution “falls through” to subsequent cases.
Question 50: What is the purpose of a for
loop in JavaScript?
Show Explanation
Correct Answer: A. To execute a block of code repeatedly for a specific number of times. for
loops are designed for repeated execution based on a counter.
Question 51: Which loop in JavaScript executes a block of code as long as a specified condition is true?
Show Explanation
Correct Answer: B. while
loop. A while
loop continues as long as its condition remains true.
Question 52: What is the key difference between a while
loop and a do...while
loop?
Show Explanation
Correct Answer: A. A while
loop checks the condition before executing the code block, while a do...while
loop checks it after. A do...while
loop guarantees at least one execution of its code block.
Question 53: Which loop is best suited for iterating over the elements of an array in JavaScript?
Show Explanation
Correct Answer: C. for...of
loop. The for...of
loop provides a clean way to iterate through array values.
Question 54: What does the break
statement do within a loop?
Show Explanation
Correct Answer: B. Exits the entire loop immediately. break
terminates the loop entirely.
Question 55: What does the continue
statement do within a loop?
Show Explanation
Correct Answer: B. Skips the rest of the current iteration and continues with the next iteration. continue
jumps to the next loop iteration.
Question 56: What is a function in JavaScript?
Show Explanation
Correct Answer: A. A block of code that can be reused multiple times. Functions encapsulate reusable blocks of code.
Question 57: What keyword is used to define a function in JavaScript?
Show Explanation
Correct Answer: B. function
. The function
keyword starts a function declaration.
Question 58: What are function parameters?
Show Explanation
Correct Answer: A. Values passed to a function when it is called. Parameters act as placeholders for values that a function will receive.
Question 59: What is the purpose of the return
statement in a function?
Show Explanation
Correct Answer: C. To specify the value that the function should return. return
sends a value back from the function.
Question 60: What is function scope in JavaScript?
Show Explanation
Correct Answer: B. The accessibility of variables within and outside a function. Scope determines which variables a function can access.
Question 61: What is an arrow function in JavaScript?
Show Explanation
Correct Answer: A. A shorthand way to write functions. Arrow functions provide a more concise syntax for function expressions.
Question 62: What is one key difference between regular functions and arrow functions in terms of the this
keyword?
Show Explanation
Correct Answer: B. Regular functions have their own this
binding, while arrow functions do not. Arrow functions inherit this
from their surrounding context.
Question 63: What is the purpose of a try...catch
statement in JavaScript?
Show Explanation
Correct Answer: B. To handle potential errors that may occur in a block of code. try...catch
allows you to gracefully manage runtime errors.
Question 64: What does the throw
statement do in JavaScript?
Show Explanation
Correct Answer: B. Generates a custom error. throw
creates an exception that can be caught.
Question 65: What does DOM stand for in web development?
Show Explanation
Correct Answer: B. Document Object Model. The DOM is a tree-like representation of an HTML document.
Question 66: What are nodes in the DOM?
Show Explanation
Correct Answer: B. Individual elements, attributes, and text within an HTML document. Nodes are the fundamental building blocks of the DOM tree.
Question 67: Which method is used to select an element by its ID in JavaScript?
Show Explanation
Correct Answer: A. getElementById()
. getElementById()
retrieves a single element with a specific ID.
Question 68: What does getElementsByTagName()
return in JavaScript?
Show Explanation
Correct Answer: B. A live HTMLCollection of elements with the specified tag name. getElementsByTagName()
gives you a live collection of elements.
Question 69: How does querySelector()
differ from querySelectorAll()
?
Show Explanation
Correct Answer: B. querySelectorAll()
returns a NodeList, while querySelector()
returns a single element. querySelector()
gets the first matching element, while querySelectorAll()
gets all of them.
Question 70: Which property is used to change the text content of an element in JavaScript?
Show Explanation
Correct Answer: C. Both A and B. Both innerHTML
(which can include HTML tags) and textContent
(plain text only) can modify element content.
Question 71: Which method is used to set an attribute of an element in JavaScript?
Show Explanation
Correct Answer: A. setAttribute()
. setAttribute()
updates or adds an attribute to an element.
Question 72: How can you change the style of an element using JavaScript?
Show Explanation
Correct Answer: C. Both A and B. You can directly manipulate the style
object or add/remove CSS classes.
Question 73: Which method is used to create a new HTML element in JavaScript?
Show Explanation
Correct Answer: A. createElement()
. createElement()
generates a DOM element that you can then insert.
Question 74: What does the appendChild()
method do in JavaScript?
Show Explanation
Correct Answer: B. Adds a new child node to the end of a parent node. appendChild()
inserts a node as the last child of an element.
Question 75: What is an event listener in JavaScript?
Show Explanation
Correct Answer: A. A function that waits for a specific event to occur on an element. Event listeners are set up to react to user interactions or other occurrences.
Question 76: Which method is used to attach an event listener to an element?
Show Explanation
Correct Answer: A. addEventListener()
. addEventListener()
is the standard way to register event handlers.
Question 77: Which of the following is NOT a common event type in JavaScript?
Show Explanation
Correct Answer: D. resize
. While resize
is an event, it’s related to the window, not a typical element event like the others.
Question 78: What is event bubbling in JavaScript?
Show Explanation
Correct Answer: D. The way events are passed from child elements to their parent elements. Event bubbling describes how an event propagates up the DOM tree from the target element to its ancestors.
Question 79: What is the purpose of the event
object in an event handler function?
Show Explanation
Correct Answer: A. To store information about the event that occurred. The event
object provides details like the event type, target element, and mouse coordinates.
Question 80: What is a prototype in JavaScript?
Show Explanation
Correct Answer: B. An object that serves as a template for other objects. Prototypes provide a mechanism for inheritance in JavaScript.
Question 81: What is the purpose of the class
keyword in JavaScript?
Show Explanation
Correct Answer: C. To create a blueprint for objects. The class
keyword provides a more structured way to define object blueprints (constructors) and their associated methods.
Question 82: What is a constructor in JavaScript?
Show Explanation
Correct Answer: A. A special method that is called when an object is created from a class. Constructors initialize the properties of newly created objects.
Question 83: What is encapsulation in object-oriented programming?
Show Explanation
Correct Answer: A. The process of combining data and methods that operate on that data within a single unit (the object). Encapsulation bundles data and methods, promoting organization and modularity.
Question 84: What is inheritance in object-oriented programming?
Show Explanation
Correct Answer: B. The ability of objects to share behaviors and characteristics from other objects. Inheritance allows objects to inherit properties and methods, reducing code duplication.
Question 85: What is polymorphism in object-oriented programming?
Show Explanation
Correct Answer: A. The ability of objects to take on many forms. Polymorphism allows objects of different classes to be treated as objects of a common type.
Question 86: What is a callback function in JavaScript?
Show Explanation
Correct Answer: A. A function that is passed as an argument to another function and is executed after the first function completes. Callbacks are a way to handle operations that take time to complete, like network requests.
Question 87: What is a Promise in JavaScript?
Show Explanation
Correct Answer: B. An object that represents the eventual result of an asynchronous operation. Promises provide a more structured way to handle asynchronous tasks than callbacks.
Question 88: What are the three possible states of a Promise?
Show Explanation
Correct Answer: A. Pending, Fulfilled, Rejected. A Promise can be pending (ongoing), fulfilled (successful), or rejected (failed).
Question 89: What is the purpose of the async
and await
keywords in JavaScript?
Show Explanation
Correct Answer: C. To simplify working with Promises. async/await
makes asynchronous code look and behave a bit more like synchronous code.
Question 90: What is JSON?
Show Explanation
Correct Answer: B. A lightweight data interchange format. JSON is a text-based format for representing data as key-value pairs.
Question 91: Which method is used to convert a JavaScript object into a JSON string?
Show Explanation
Correct Answer: B. JSON.stringify()
. JSON.stringify()
serializes a JavaScript object into JSON format.
Question 92: What is a regular expression in JavaScript?
Show Explanation
Correct Answer: A. A pattern used to match character combinations in strings. Regular expressions are powerful tools for text processing and validation.
Question 93: What is the purpose of the test()
method in a regular expression?
Show Explanation
Correct Answer: C. To check if a string matches a pattern. The test()
method checks for matches between a regular expression and a string.
Question 94: What is a module in JavaScript?
Show Explanation
Correct Answer: A. A reusable block of code that can be imported into other files. Modules help organize code and promote reusability.
Question 95: What is the Browser Object Model (BOM)?
Show Explanation
Correct Answer: B. A programming interface for interacting with the web browser. The BOM provides objects for controlling browser features like the history and window.
Question 96: Which object in the BOM represents the current browser window?
Show Explanation
Correct Answer: B. window
. The window
object is the top-level object in the BOM.
Question 97: Which object in the BOM provides information about the user’s browser?
Show Explanation
Correct Answer: C. navigator
. The navigator
object gives details about the browser itself.
Question 98: Which event is triggered when a user clicks a mouse button?
Show Explanation
Correct Answer: D. All of the above. All three events are related to mouse clicks, but click
represents a complete click action.
Question 99: Which event is triggered when a user moves the mouse over an element?
Show Explanation
Correct Answer: B. mouseover
. mouseover
fires when the mouse enters the area of an element.
Question 100: Which function is used to execute a block of code after a specified delay in milliseconds?
Show Explanation
Correct Answer: A. setTimeout()
. setTimeout()
schedules a function to run once after a given delay.