100 JavaScript MCQ Questions and Answers

100 JavaScript MCQ Questions and Answers. These 100 Multiple Choice Questions on JavaScript will help you score full marks in your exam and interview.

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

Answer
Answer: C. JavaScript is a scripting language that enables dynamic and interactive elements on websites. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

History and evolution of JavaScript

2.  Who created JavaScript?

A. Tim Berners-Lee

B. Brendan Eich

C. James Gosling

D. Guido van Rossum

Answer
Answer: B. Brendan Eich created JavaScript while working at Netscape Communications. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. Defining the structure and content of a web page is primarily done with HTML, not JavaScript. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Client-side JavaScript runs within the user’s web browser. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: C. ECMAScript provides the standards that JavaScript implementations adhere to. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. An object is a complex data type, not a primitive one. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

7. What is the output of `typeof null` in JavaScript?

A. “null”

B. “undefined”

C. “object”

D. “symbol”

Answer
Answer: C. This is a quirk of JavaScript; `typeof null` returns “object”. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. Symbols are primarily used as unique property keys in objects. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: D. Both `let` and `const` declare variables with block scope. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: A. `let` allows for reassignment of a variable, whereas `const` creates a read-only variable. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)
100 JavaScript MCQ Questions and Answers

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.

Answer
Answer: C. Reassigning a `const` variable results in a TypeError. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: B. Type coercion is JavaScript’s automatic conversion of values from one data type to another. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

13. What is the result of the expression `5 + “5”` in JavaScript?

A. 10

B. 25

C. “55”

D. “10”

Answer
Answer: C. The number 5 is coerced into a string, resulting in string concatenation. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: D. Both `parseInt()` and `parseFloat()` convert strings to numbers, with the latter handling decimals. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: B. The modulus operator gives the remainder after division. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

16. What is the result of the expression `10 < 5 || 5 > 2`?

A. true

B. false

C. undefined

D. null

Answer
Answer: A. The expression evaluates to true because 5 > 2 is true, and the || operator only needs one condition to be true. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

17. Which operator is used to assign a value to a variable in JavaScript?

A. `==`

B. `===`

C. `=`

D. `+`

Answer
Answer: C. The single equals sign (`=`) is the assignment operator. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. The strict equality operator ensures that both the value and the data type are the same. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: A. The double equals (`==`) allows for type coercion, while the triple equals (`===`) requires the types to be the same. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: B. Multiplication has higher precedence than addition. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Associativity defines the direction of operation when operators have equal precedence. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. The ternary operator offers a concise way to express conditional logic. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. String literals are how you represent text within your code. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

24. Which property of a string returns its length in JavaScript?

A. `size`

B. `length`

C. `count`

D. `index`

Answer
Answer: B. The `length` property provides the number of characters in a string. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `toUpperCase()` transforms all characters in a string to uppercase. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. You can concatenate strings with either the `+` operator or the `concat()` method. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: D. All three methods (`substring()`, `slice()`, and `substr()`) can be used to extract parts of a string, each with slightly different behaviors. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. `NaN` signifies that a mathematical operation did not result in a meaningful number. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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()`

Answer
Answer: C. `Math.round()` rounds a number to the nearest integer. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. Booleans hold either `true` or `false`. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. Truthy values are treated as `true` in conditions. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

32.  Which of the following is a “falsy” value in JavaScript?

A. 0

B. An empty string (“”)

C. `null`

D. All of the above

Answer
Answer: D. All listed values are considered falsy in JavaScript. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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 = {};`

Answer
Answer: C. Both the array literal (`[]`) and the `Array()` constructor create arrays. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

34. Which index represents the first element in a JavaScript array?

A. 1

B. 0

C. -1

D. Any number

Answer
Answer: B. JavaScript arrays are zero-indexed. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `push()` appends an element to the end. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: D. `pop()` removes and returns the last element. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

37. Which array method is used to add an element to the beginning of an array?

A. `push()`

B. `unshift()`

C. `shift()`

D. `splice()`

Answer
Answer: B. `unshift()` adds elements to the front of an array. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. `splice()` is versatile for adding and removing elements at specific indices. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. The `for…in` loop is used to loop through the keys of an object. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. `forEach()` iterates over array elements and applies a function to each. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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 = [];`

Answer
Answer: C. Both the object literal (`{}`) and the `Object()` constructor create objects. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: A. `this` provides a way to access properties and methods within the object itself. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Dot notation (e.g., `object.property`) and bracket notation (e.g., `object[‘property’]`) are used for property access. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. `if` statements control code execution based on a condition. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. The `else` block provides an alternative path when the `if` condition isn’t met. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C.  `else if` provides additional conditional checks when the previous `if` is false. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `switch` statements offer a cleaner way to handle multiple possible values of a variable. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. `break` ensures that only the matching case’s code is executed. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: B.  Without `break`, execution “falls through” to subsequent cases. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. `for` loops are designed for repeated execution based on a counter. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. A `while` loop continues as long as its condition remains true. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: A. A `do…while` loop guarantees at least one execution of its code block. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. The `for…of` loop provides a clean way to iterate through array values. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `break` terminates the loop entirely. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `continue` jumps to the next loop iteration. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Functions encapsulate reusable blocks of code. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

57. What keyword is used to define a function in JavaScript?

A. `func`

B. `function`

C. `define`

D. `method`

Answer
Answer: B. The `function` keyword starts a function declaration. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Parameters act as placeholders for values that a function will receive. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. `return` sends a value back from the function. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. Scope determines which variables a function can access. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Arrow functions provide a more concise syntax for function expressions. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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`.

Answer
Answer: B. Arrow functions inherit `this` from their surrounding context. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `try…catch` allows you to gracefully manage runtime errors. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `throw` creates an exception that can be caught. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. The DOM is a tree-like representation of an HTML document. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. Nodes are the fundamental building blocks of the DOM tree. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

Selecting Elements

67. Which method is used to select an element by its ID in JavaScript?

A. `getElementById()`

B. `getElementsByTagName()`

C. `getElementsByClassName()`

D. `querySelector()`

Answer
Answer: A. `getElementById()` retrieves a single element with a specific ID. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `getElementsByTagName()` gives you a live collection of elements. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Answer
Answer: B. `querySelector()` gets the first matching element, while `querySelectorAll()` gets all of them. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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`

Answer
Answer: C. Both `innerHTML` (which can include HTML tags) and `textContent` (plain text only) can modify element content. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

71. Which method is used to set an attribute of an element in JavaScript?

A. `setAttribute()`

B. `getAttribute()`

C. `removeAttribute()`

D. `hasAttribute()`

Answer
Answer: A.  `setAttribute()` updates or adds an attribute to an element. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. You can directly manipulate the `style` object or add/remove CSS classes. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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()`

Answer
Answer: A.  `createElement()` generates a DOM element that you can then insert. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. `appendChild()` inserts a node as the last child of an element. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Event listeners are set up to react to user interactions or other occurrences. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

76. Which method is used to attach an event listener to an element?

A. `addEventListener()`

B. `attachEvent()`

C. `onEvent()`

D. `listenForEvent()`

Answer
Answer: A. `addEventListener()` is the standard way to register event handlers. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

77. Which of the following is NOT a common event type in JavaScript?

A. `click`

B. `mouseover`

C. `submit`

D. `resize`

Answer
Answer: D. While `resize` is an event, it’s related to the window, not a typical element event like the others. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: D. Event bubbling describes how an event propagates up the DOM tree from the target element to its ancestors. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. The `event` object provides details like the event type, target element, and mouse coordinates. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. Prototypes provide a mechanism for inheritance in JavaScript. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. The `class` keyword provides a more structured way to define object blueprints (constructors) and their associated methods. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Constructors initialize the properties of newly created objects. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Encapsulation bundles data and methods, promoting organization and modularity. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. Inheritance allows objects to inherit properties and methods, reducing code duplication. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Polymorphism allows objects of different classes to be treated as objects of a common type. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Callbacks are a way to handle operations that take time to complete, like network requests. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. Promises provide a more structured way to handle asynchronous tasks than callbacks. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. A Promise can be pending (ongoing), fulfilled (successful), or rejected (failed). (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. `async/await` makes asynchronous code look and behave a bit more like synchronous code. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. JSON is a text-based format for representing data as key-value pairs. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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()`

Answer
Answer: B.  `JSON.stringify()` serializes a JavaScript object into JSON format. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Regular expressions are powerful tools for text processing and validation. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: C. The `test()` method checks for matches between a regular expression and a string. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: A. Modules help organize code and promote reusability. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: B. The BOM provides objects for controlling browser features like the history and window. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

96. Which object in the BOM represents the current browser window?

A. `document`

B. `window`

C. `navigator`

D. `history`

Answer
Answer: B. The `window` object is the top-level object in the BOM. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

97. Which object in the BOM provides information about the user’s browser?

A. `document`

B. `window`

C. `navigator`

D. `history`

Answer
Answer: C. The `navigator` object gives details about the browser itself. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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

Answer
Answer: D. All three events are related to mouse clicks, but `click` represents a complete click action. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

99. Which event is triggered when a user moves the mouse over an element?

A. `mousemove`

B. `mouseover`

C. `mouseout`

D. `mouseenter`

Answer
Answer: B. `mouseover` fires when the mouse enters the area of an element. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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()`

Answer
Answer: A. `setTimeout()` schedules a function to run once after a given delay. (100 JavaScript MCQ Questions and Answers by Top100MCQ.com)

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top