100 JavaScript MCQ Questions and Answers. These 100 Multiple Choice Questions on JavaScript will help you score full marks in your exam and interview.
100 JavaScript MCQ Questions and Answers
I. Introduction to JavaScript – 100 JavaScript MCQ Questions and Answers
What is JavaScript?
1. Which of the following best describes JavaScript?
A. A markup language used to structure web pages
B. A style sheet language used to control the presentation of web pages
C. A scripting language used to add interactivity to web pages
D. A server-side language used to handle database operations
History and evolution of JavaScript
2. Who created JavaScript?
A. Tim Berners-Lee
B. Brendan Eich
C. James Gosling
D. Guido van Rossum
Role of JavaScript in web development
3. Which of the following is NOT a common use of JavaScript in web development?
A. Validating user input in forms
B. Creating animations and visual effects
C. Defining the structure and content of a web page
D. Making web pages dynamic and interactive
Client-side vs. server-side JavaScript (brief overview)
4. Where does client-side JavaScript code execute?
A. On the user’s web browser
B. On a remote web server
C. In a database
D. In a virtual machine
JavaScript and ECMAScript (relationship and versions)
5. What is the relationship between JavaScript and ECMAScript?
A. ECMAScript is a specific implementation of JavaScript.
B. JavaScript is a standardized version of ECMAScript.
C. ECMAScript is the standardized specification that JavaScript is based on.
D. They are completely unrelated languages.
II. Working with Data – 100 JavaScript MCQ Questions and Answers
Variables and Data Types
6. Which of the following is NOT a primitive data type in JavaScript?
A. Number
B. String
C. Object
D. Boolean
7. What is the output of `typeof null` in JavaScript?
A. “null”
B. “undefined”
C. “object”
D. “symbol”
8. What does the `symbol` data type represent in JavaScript?
A. A string literal
B. A unique and immutable value
C. An alias for another variable
D. A floating-point number
9. Which keyword is used to declare a variable with block scope in JavaScript?
A. `var`
B. `let`
C. `const`
D. Both B and C
10. What is the difference between `let` and `const` in JavaScript?
A. `let` can be reassigned, while `const` cannot.
B. `const` can be reassigned, while `let` cannot.
C. `let` is used for strings, while `const` is used for numbers.
D. They are interchangeable keywords.
11. What will happen when you try to reassign a `const` variable?
A. The code will execute without any errors.
B. It will update the value of the variable.
C. It will throw a TypeError.
D. It will create a new variable with the same name.
12. What is type coercion in JavaScript?
A. Explicitly converting one data type to another.
B. Implicitly converting one data type to another.
C. Checking the data type of a variable.
D. Declaring a variable without specifying its type.
13. What is the result of the expression `5 + “5”` in JavaScript?
A. 10
B. 25
C. “55”
D. “10”
14. Which function can be used to convert a string to a number in JavaScript?
A. `toString()`
B. `parseInt()`
C. `parseFloat()`
D. Both B and C
Operators and Expressions
15. What does the modulus operator (%) do in JavaScript?
A. Divides two numbers and returns the quotient.
B. Divides two numbers and returns the remainder.
C. Raises a number to a specified power.
D. Calculates the square root of a number.
16. What is the result of the expression `10 < 5 || 5 > 2`?
A. true
B. false
C. undefined
D. null
17. Which operator is used to assign a value to a variable in JavaScript?
A. `==`
B. `===`
C. `=`
D. `+`
18. What does the strict equality operator (`===`) check for in JavaScript?
A. Only value equality
B. Only type equality
C. Both value and type equality
D. Neither value nor type equality
19. What is the difference between `==` and `===` in JavaScript?
A. `==` performs type coercion, while `===` does not.
B. `===` performs type coercion, while `==` does not.
C. `==` checks for strict equality, while `===` checks for loose equality.
D. They are interchangeable operators.
20. In the expression `5 + 4 * 2`, which operation is performed first?
A. Addition
B. Multiplication
C. They are performed from left to right.
D. It depends on the JavaScript engine.
21. What does operator associativity determine in JavaScript?
A. The order of operations when operators have the same precedence
B. The data type of the result of an expression
C. Whether type coercion will occur
D. The scope of a variable
22. What is the purpose of the conditional (ternary) operator in JavaScript?
A. To create a loop that iterates a specific number of times
B. To define a function that can be called later
C. To provide a shorthand way to write an if-else statement
D. To handle potential errors in code
Strings
23. What is a string literal in JavaScript?
A. A variable that stores a string value
B. A sequence of characters enclosed in single or double quotes
C. A method used to manipulate strings
D. A special character used to escape other characters
24. Which property of a string returns its length in JavaScript?
A. `size`
B. `length`
C. `count`
D. `index`
25. What does the `toUpperCase()` method do to a string in JavaScript?
A. Converts the string to lowercase
B. Converts the string to uppercase
C. Removes whitespace from the beginning and end of the string
D. Reverses the order of characters in the string
26. How do you concatenate two strings in JavaScript?
A. Using the `+` operator
B. Using the `concat()` method
C. Both A and B
D. Using the `join()` method
27. Which method is used to extract a portion of a string in JavaScript?
A. `substring()`
B. `slice()`
C. `substr()`
D. All of the above
Numbers
28. What is the value of `NaN` in JavaScript?
A. A specific number
B. A string representing “Not a Number”
C. A special value indicating an invalid number operation
D. The largest possible number in JavaScript
29. Which method is used to round a number to the nearest integer in JavaScript?
A. `Math.floor()`
B. `Math.ceil()`
C. `Math.round()`
D. `Math.trunc()`
Booleans
30. What are the two possible values of a boolean in JavaScript?
A. 0 and 1
B. “true” and “false”
C. true and false
D. yes and no
31. What is a “truthy” value in JavaScript?
A. A value that is strictly equal to `true`
B. Any value that evaluates to `true` in a boolean context
C. A string that contains the word “true”
D. A non-zero number
32. Which of the following is a “falsy” value in JavaScript?
A. 0
B. An empty string (“”)
C. `null`
D. All of the above
Arrays
33. How do you create an empty array in JavaScript?
A. `var myArray = [];`
B. `var myArray = new Array();`
C. Both A and B
D. `var myArray = {};`
34. Which index represents the first element in a JavaScript array?
A. 1
B. 0
C. -1
D. Any number
35. What does the `push()` method do to an array in JavaScript?
A. Adds an element to the beginning of the array
B. Adds an element to the end of the array
C. Removes an element from the beginning of the array
D. Removes an element from the end of the array
36. What does the `pop()` method do to an array?
A. Adds an element to the beginning of the array.
B. Adds an element to the end of the array.
C. Removes the first element from the array.
D. Removes the last element from the array.
37. Which array method is used to add an element to the beginning of an array?
A. `push()`
B. `unshift()`
C. `shift()`
D. `splice()`
38. What does the `splice()` method allow you to do with an array?
A. Add or remove elements at any position
B. Sort the elements of the array
C. Reverse the order of elements in the array
D. Create a copy of the array
39. Which loop is specifically designed for iterating over the properties of an object in JavaScript?
A. `for` loop
B. `for…in` loop
C. `for…of` loop
D. `while` loop
40. What does the `forEach()` method do in JavaScript?
A. Executes a provided function once for each array element
B. Creates a new array with the results of calling a function on every element
C. Filters the array based on a condition
D. Sorts the array in ascending order
Objects
41. How do you create an empty object in JavaScript?
A. `var myObject = {};`
B. `var myObject = new Object();`
C. Both A and B
D. `var myObject = [];`
42. What is the purpose of the `this` keyword within an object in JavaScript?
A. It refers to the current object.
B. It refers to the parent object.
C. It refers to the global object.
D. It refers to the previous object.
43. What are the two ways to access properties of an object in JavaScript?
A. Dot notation and bracket notation
B. Parentheses and curly braces
C. Single quotes and double quotes
D. Forward slashes and backslashes
III. Control Flow and Logic
Conditional Statements
44. What does an `if` statement do in JavaScript?
A. Executes a block of code only if a specified condition is true
B. Executes a block of code repeatedly
C. Defines a function that can be called later
D. Handles potential errors in code
45. What is the purpose of the `else` clause in an `if-else` statement?
A. To specify an alternative block of code to execute if the `if` condition is true
B. To specify a block of code to execute if the `if` condition is false
C. To define a loop that iterates a specific number of times
D. To handle potential errors in code
46. When is the code inside an `else if` block executed?
A. Always
B. Only if the preceding `if` condition is true
C. Only if the preceding `if` condition is false and the `else if` condition is true
D. Only if the preceding `if` and all preceding `else if` conditions are false
47. What is the purpose of a `switch` statement in JavaScript?
A. To create a loop that iterates a specific number of times
B. To provide an alternative to using multiple `if-else if` statements
C. To define a function that can be called later
D. To handle potential errors in code
48. What does the `break` keyword do within a `switch` statement?
A. Exits the entire `switch` statement
B. Skips to the next case in the `switch` statement
C. Continues to the next iteration of the loop
D. Throws an error
49. What happens if you omit the `break` keyword in a `switch` statement?
A. The code execution stops at the matching case.
B. The code execution continues to the next case, even if it doesn’t match.
C. An error is thrown.
D. The `switch` statement is ignored.
Loops
50. What is the purpose of a `for` loop in JavaScript?
A. To execute a block of code repeatedly for a specific number of times
B. To execute a block of code only if a specified condition is true
C. To define a function that can be called later
D. To handle potential errors in code
51. Which loop in JavaScript executes a block of code as long as a specified condition is true?
A. `for` loop
B. `while` loop
C. `do…while` loop
D. `for…of` loop
52. What is the key difference between a `while` loop and a `do…while` loop?
A. A `while` loop checks the condition before executing the code block, while a `do…while` loop checks it after.
B. A `do…while` loop checks the condition before executing the code block, while a `while` loop checks it after.
C. A `while` loop is used for iterating over arrays, while a `do…while` loop is used for iterating over objects.
D. They are functionally identical.
53. Which loop is best suited for iterating over the elements of an array in JavaScript?
A. `for` loop
B. `for…in` loop
C. `for…of` loop
D. `while` loop
54. What does the `break` statement do within a loop?
A. Exits the current iteration of the loop and continues with the next iteration
B. Exits the entire loop immediately
C. Skips to the next iteration of the loop
D. Throws an error
55. What does the `continue` statement do within a loop?
A. Exits the entire loop immediately
B. Skips the rest of the current iteration and continues with the next iteration
C. Pauses the loop execution
D. Throws an error
Functions
56. What is a function in JavaScript?
A. A block of code that can be reused multiple times
B. A variable that stores a value
C. A data type that represents a true or false value
D. An error that occurs during code execution
57. What keyword is used to define a function in JavaScript?
A. `func`
B. `function`
C. `define`
D. `method`
58. What are function parameters?
A. Values passed to a function when it is called
B. Values returned by a function
C. Variables declared inside a function
D. Errors that can occur within a function
59. What is the purpose of the `return` statement in a function?
A. To specify the parameters of the function
B. To define the code block of the function
C. To specify the value that the function should return
D. To call another function
60. What is function scope in JavaScript?
A. The area of code where a function can be called
B. The accessibility of variables within and outside a function
C. The data type of the value returned by a function
D. The number of parameters a function can accept
61. What is an arrow function in JavaScript?
A. A shorthand way to write functions
B. A function that can only be called once
C. A function that always returns a boolean value
D. A function that throws an error
62. What is one key difference between regular functions and arrow functions in terms of the `this` keyword?
A. Arrow functions have their own `this` binding, while regular functions do not.
B. Regular functions have their own `this` binding, while arrow functions do not.
C. Arrow functions cannot use the `this` keyword.
D. They are identical in how they handle `this`.
Error Handling
63. What is the purpose of a `try…catch` statement in JavaScript?
A. To define a loop that iterates a specific number of times
B. To handle potential errors that may occur in a block of code
C. To define a function that can be called later
D. To create a conditional statement
64. What does the `throw` statement do in JavaScript?
A. Catches an error
B. Generates a custom error
C. Ignores an error
D. Prevents an error from occurring
IV. DOM Manipulation
Understanding the DOM
65. What does DOM stand for in web development?
A. Data Object Model
B. Document Object Model
C. Digital Order Management
D. Dynamic Output Mechanism
66. What are nodes in the DOM?
A. Programming languages used to create web pages
B. Individual elements, attributes, and text within an HTML document
C. Styles applied to elements in a web page
D. Events that can occur on a web page
Selecting Elements
67. Which method is used to select an element by its ID in JavaScript?
A. `getElementById()`
B. `getElementsByTagName()`
C. `getElementsByClassName()`
D. `querySelector()`
68. What does `getElementsByTagName()` return in JavaScript?
A. A single element with the specified tag name
B. A live HTMLCollection of elements with the specified tag name
C. A static NodeList of elements with the specified tag name
D. An array of elements with the specified tag name
69. How does `querySelector()` differ from `querySelectorAll()`?
A. `querySelector()` returns a NodeList, while `querySelectorAll()` returns a single element.
B. `querySelectorAll()` returns a NodeList, while `querySelector()` returns a single element.
C. `querySelector()` only works with IDs, while `querySelectorAll()` works with any CSS selector.
D. They are interchangeable methods.
Modifying Elements
70. Which property is used to change the text content of an element in JavaScript?
A. `innerHTML`
B. `textContent`
C. Both A and B
D. `value`
71. Which method is used to set an attribute of an element in JavaScript?
A. `setAttribute()`
B. `getAttribute()`
C. `removeAttribute()`
D. `hasAttribute()`
72. How can you change the style of an element using JavaScript?
A. By modifying the `style` property of the element
B. By applying a CSS class to the element
C. Both A and B
D. By using the `setStyle()` method
Creating and Deleting Elements
73. Which method is used to create a new HTML element in JavaScript?
A. `createElement()`
B. `addNewElement()`
C. `createNode()`
D. `insertElement()`
74. What does the `appendChild()` method do in JavaScript?
A. Adds a new child node to the beginning of a parent node
B. Adds a new child node to the end of a parent node
C. Removes a child node from a parent node
D. Replaces a child node with another node
Events
75. What is an event listener in JavaScript?
A. A function that waits for a specific event to occur on an element
B. An attribute that defines the type of event an element can handle
C. A method that triggers an event on an element
D. An object that stores information about an event
76. Which method is used to attach an event listener to an element?
A. `addEventListener()`
B. `attachEvent()`
C. `onEvent()`
D. `listenForEvent()`
77. Which of the following is NOT a common event type in JavaScript?
A. `click`
B. `mouseover`
C. `submit`
D. `resize`
78. What is event bubbling in JavaScript?
A. The order in which event handlers are executed when an event occurs on an element with nested child elements
B. The process of preventing an event from being handled by its default handler
C. The act of triggering an event programmatically
D. The way events are passed from child elements to their parent elements
79. What is the purpose of the `event` object in an event handler function?
A. To store information about the event that occurred
B. To prevent the default behavior of the event
C. To stop the event from bubbling up the DOM tree
D. To create a new event
V. Advanced JavaScript Concepts
Object-Oriented Programming (OOP)
80. What is a prototype in JavaScript?
A. A blueprint for creating objects
B. An object that serves as a template for other objects
C. A property that defines the data type of an object
D. A method that is inherited by all objects
81. What is the purpose of the `class` keyword in JavaScript?
A. To declare a variable
B. To define a function
C. To create a blueprint for objects
D. To handle errors
82. What is a constructor in JavaScript?
A. A special method that is called when an object is created from a class
B. A property that defines the data type of an object
C. A loop that iterates over the properties of an object
D. A function that is inherited by all objects
83. What is encapsulation in object-oriented programming?
A. The process of combining data and methods that operate on that data within a single unit (the object)
B. The ability of an object to take on many forms
C. The mechanism by which objects inherit properties and methods from their parent objects
D. The practice of writing code that is easy to read and understand
84. What is inheritance in object-oriented programming?
A. The process of creating new objects from a blueprint
B. The ability of objects to share behaviors and characteristics from other objects
C. The act of hiding data within an object
D. The practice of writing code that can handle errors gracefully
85. What is polymorphism in object-oriented programming?
A. The ability of objects to take on many forms
B. The process of creating new objects from a blueprint
C. The act of hiding data within an object
D. The practice of writing code that can handle errors gracefully
Asynchronous JavaScript
86. What is a callback function in JavaScript?
A. A function that is passed as an argument to another function and is executed after the first function completes
B. A function that is called immediately
C. A function that is defined within another function
D. A function that throws an error
87. What is a Promise in JavaScript?
A. A variable that stores a value
B. An object that represents the eventual result of an asynchronous operation
C. A function that is called repeatedly
D. An error that occurs during code execution
88. What are the three possible states of a Promise?
A. Pending, Fulfilled, Rejected
B. Open, Closed, Error
C. True, False, Null
D. Start, Running, Finished
89. What is the purpose of the `async` and `await` keywords in JavaScript?
A. To define synchronous functions
B. To handle errors in asynchronous code
C. To simplify working with Promises
D. To create loops
JSON (JavaScript Object Notation)
90. What is JSON?
A. A programming language
B. A lightweight data interchange format
C. A type of database
D. A JavaScript framework
91. Which method is used to convert a JavaScript object into a JSON string?
A. `JSON.parse()`
B. `JSON.stringify()`
C. `Object.toJSON()`
D. `String.fromObject()`
Regular Expressions
92. What is a regular expression in JavaScript?
A. A pattern used to match character combinations in strings
B. A variable that stores a string value
C. A method used to manipulate strings
D. A special character used to escape other characters
93. What is the purpose of the `test()` method in a regular expression?
A. To replace all occurrences of a pattern in a string
B. To extract the portion of a string that matches a pattern
C. To check if a string matches a pattern
D. To create a new regular expression
Modules
94. What is a module in JavaScript?
A. A reusable block of code that can be imported into other files
B. A variable that stores a value
C. A data type that represents a true or false value
D. An error that occurs during code execution
VI. Working with the Browser
Browser Object Model (BOM)
95. What is the Browser Object Model (BOM)?
A. A tree-like representation of an HTML document
B. A programming interface for interacting with the web browser
C. A set of standards for web development
D. A JavaScript framework
96. Which object in the BOM represents the current browser window?
A. `document`
B. `window`
C. `navigator`
D. `history`
97. Which object in the BOM provides information about the user’s browser?
A. `document`
B. `window`
C. `navigator`
D. `history`
DOM Events
98. Which event is triggered when a user clicks a mouse button?
A. `mousedown`
B. `mouseup`
C. `click`
D. All of the above
99. Which event is triggered when a user moves the mouse over an element?
A. `mousemove`
B. `mouseover`
C. `mouseout`
D. `mouseenter`
Timers
100. Which function is used to execute a block of code after a specified delay in milliseconds?
A. `setTimeout()`
B. `setInterval()`
C. `delay()`
D. `pause()`
Most Asked Important Questions on JavaScript
1. What is the difference between var
, let
, and const
when declaring variables in JavaScript?
var
has function scope, while let
and const
have block scope.const
declares a variable with a constant value that cannot be re-assigned.let
allows for re-assignment of the variable.
Explain the concept of “hoisting” in JavaScript.
Hoisting is JavaScript’s default behavior of moving declarations to the top of their scope (function or global). This means you can use a variable before it is declared. However, only the declaration is hoisted, not the initialization.
What is the purpose of the “this
” keyword in JavaScript?
this
refers to the object it belongs to. In a function, this
refers to the global object. In a method, this
refers to the owner object.
What is the difference between ==
and ===
in JavaScript?
==
checks for equality after type coercion.===
checks for strict equality, meaning both value and type must be the same.
Explain the concept of closures in JavaScript.
A closure gives a function access to an outer function’s scope, even after the outer function has returned. This is achieved by creating a function inside another function and returning the inner function.