JavaScript Quiz - Multiple Choice Questions (MCQ)

In this blog post, we present a JavaScript quiz with 50+ multiple-choice questions. Each question is followed by its correct answer and an explanation. 

Let's dive in and see how well you know JavaScript! 

1. What is the correct way to declare a variable in JavaScript? 

a) 

variable x = 10;

b) 

var x = 10; 

c)

 x = 10; 

d) 

int x = 10; 

Answer: 

b) 

var x = 10;

Explanation: 

In JavaScript, variables are declared using the 'var' keyword, followed by the variable name and an optional initial value.

2. What is the correct JavaScript syntax to modify the content of the HTML element shown below?

<p id="demo">Welcome to JavaScript Quiz</p>
a) 
document.getElementById("demo").innerHTML = "Hello World!";
b) 
demo.innerHTML = "Hello World!"; 
c) 
document.getElement("p").innerHTML = "Hello World!";
d)
document.getElementByName("p").innerHTML = "Hello World!"; 

Answer:

a) 
document.getElementById("demo").innerHTML = "Hello World!";

Explanation: 

To change the content of an HTML element using JavaScript, we need to select the element using its unique identifier (ID) and then update its 'innerHTML' property with the new content.

3. What will be the output of the following code snippet?

console.log(2 + "2");
a) 4 
b) 22 
c) "22" 
d) "4" 

Answer: 

c) "22" 

Explanation: 

When the '+' operator is used with a string and a number, the number is converted to a string and concatenated.

4. How do you check if a variable is an array in JavaScript? 

a) isArray(x) 
b) x.isArray() 
c) x instanceof Array 
d) typeof Array 

Answer: 

a) isArray(x) 

Explanation: 

The 'isArray()' method is used to check if a variable is an array in JavaScript.

5. How do you properly comment on a single line in JavaScript? 

a) # This is a comment. 
b) // This is a comment. 
c) /* This is a comment. */ 
d) <!-- This is a comment. --> 

Answer: 

b) // This is a comment. 

Explanation: 

In JavaScript, single-line comments are denoted by //.

6. Where is the correct place to insert JavaScript code in an HTML document?

a) Both the <head> section and the <body> section
b) The <head> section 
c) The <body> section 
d) Anywhere in the HTML file

Answer:

a) Both the <head> section and the <body> section

Explanation: 

JavaScript code can be inserted in both the <head> section and the <body> section of an HTML document. The choice of placement depends on the specific requirements of the script. Placing scripts in the <head> section allows them to load early but may require handling the document's readiness. On the other hand, placing scripts at the end of the <body> section ensures access to all the HTML elements without additional handling for document readiness.

7. What are the main types of loops available in JavaScript? 

a) for loop, do...while loop, while loop
b) for loop, do...until loop, while loop
c) for loop, while loop, repeat...until loop
d) for loop, do...while loop, repeat...until loop 

Answer: 

a) for loop, do...while loop, while loop

Explanation: 

JavaScript provides three main types of loops - for loop, do...while loop, and while loop. Each loop has its use case and syntax, but they all serve the purpose of repeatedly executing a block of code based on a condition.

8. Which loop is guaranteed to execute the block of code at least once? 

a) for loop 
b) do...while loop 
c) while loop 
d) none

Answer: 

b) do...while loop 

Explanation: 

The do...while loop first executes the block of code and then checks the condition. It will keep executing the code as long as the condition is true. Since the code block is executed before checking the condition, it is guaranteed to run at least once.

9. What is the purpose of the break statement in a loop? 

a) It stops the execution of the loop immediately. 
b) It skips the current iteration and moves to the next one. 
c) It restarts the loop from the beginning. 
d) It returns a value from the loop. 

Answer: 

a) It stops the execution of the loop immediately. 

Explanation: 

The break statement is used to prematurely terminate a loop. When the break statement is encountered inside a loop, the loop execution stops immediately, and the program continues with the next statement after the loop.

10. Which loop is ideal when you want to iterate over an array in JavaScript? 

a) for loop 
b) do...while loop 
c) while loop 
d) repeat...until loop 

Answer: 

a) for loop 

Explanation: 

The for loop is ideal for iterating over an array in JavaScript. It provides a concise syntax for initializing a variable, defining a condition, and updating the variable's value. By utilizing the array's length property, you can easily iterate through all its elements.

11. What is the key difference between the while loop and the do...while loop? 

a) The while loop executes the code block first, then checks the condition. The do...while loop checks the condition first, then executes the code block. 
b) The while loop always executes the code block at least once. The do...while loop checks the condition first and may skip the code block entirely. 
c) The while loop is more efficient for larger datasets. The do...while loop is more efficient for smaller datasets. 
d) There is no difference between the two; they can be used interchangeably. 

Answer: 

a) The while loop executes the code block first, then checks the condition. The do...while loop checks the condition first, then executes the code block. 

Explanation: 

The key difference lies in the order of execution. In a while loop, the condition is evaluated first, and if it's true, the code block is executed. In contrast, the do...while loop executes the code block first and then checks the condition. This ensures that the code block is executed at least once, even if the condition is initially false.

12. What is the purpose of the continue statement in a loop? 

a) To exit the loop and stop execution immediately. 
b) To skip the current iteration and proceed to the next one. 
c) To restart the loop from the beginning. 
d) To return a value from the loop. 

Answer: 

b) To skip the current iteration and proceed to the next one. 

Explanation: 

The continue statement is used to skip the current iteration in a loop and proceed with the next iteration without executing the remaining code in the loop's block.

13. Which operator is used to combine two or more strings in JavaScript? 

a) && 
b) || 
c) + 
d) - 

Answer: 

c) + 

Explanation: 

The plus (+) operator is used for string concatenation in JavaScript. It combines two or more strings into a single string.

14. Which comparison operator checks for both value and type equality in JavaScript? 

a) == 
b) === 
c) = 
d) <=> 

Answer: 

b) === 

Explanation: 

The triple equals (===) is the strict equality operator in JavaScript. It checks both the value and the data type of the operands. It returns true only if both the value and type are the same.

15. How do you create an empty array in JavaScript? 

a) 
let arr = new Array(); 
b) 
let arr = []; 
c) 
let arr = {}; 
d) 
let arr = null; 

Answer: 

b) let arr = []; 

Explanation: 

The correct way to create an empty array in JavaScript is by using square brackets [].

16. What is the index of the first element in an array? 

a) 0 
b) 1 
c) -1 
d) 10 

Answer: 

a) 0 

Explanation: 

In JavaScript, arrays are zero-indexed, which means the first element is accessed using index 0.

17. Which method do you use to add elements to the end of an array? 

a) add() 
b) push() 
c) append() 
d) concat() 

Answer: 

b) push() 

Explanation: 

The push() method is used to add elements to the end of an array in JavaScript.

18. How do you remove the last element from an array? 

a) removeLast() 
b) deleteLast() 
c) pop() 
d) splice() 

Answer: 

c) pop() 

Explanation: 

The pop() method removes the last element from an array and returns that element.

19. What will be the output of the following code snippet?

let fruits = ['apple', 'banana', 'orange'];
fruits.pop();
console.log(fruits.length);
a) 0 
b) 1 
c) 2 
d) 3 

Answer: 

c) 2 

Explanation: 

The pop() method removes the last element from the array. After executing fruits.pop(), the array will have two elements left, 'apple' and 'banana'. The length property returns the number of elements in the array after the 'orange' element has been popped off which is 2.

20. How do you check if an element exists in an array? 

a) Using the check() method 
b) Using the search() method 
c) Using the includes() method 
d) Using the exists() method 

Answer: 

c) Using the includes() method 

Explanation: 

The includes() method is used to check if an element exists in an array. It returns true if the element is found and false otherwise.

21. What method do you use to join all elements of an array into a single string? 

a) join() 
b) concat() 
c) merge() 
d) combine() 

Answer: 

a) join() 

Explanation: 

The join() method is used to concatenate all the elements of an array into a single string using a specified separator.

22. How do you remove elements from an array, starting from a specific index? 

a) removeFromIndex() 
b) splice() 
c) cut() 
d) deleteFromIndex() 

Answer: 

b) splice() 

Explanation: 

The splice() method is used to remove elements from an array, starting from a specific index.

23. How do you find the index of a specific element in an array? 

a) findIndex() 
b) indexOf() 
c) searchIndex() 
d) getElementIndex() 

Answer: 

b) indexOf() 

Explanation: 

The indexOf() method is used to find the index of a specific element in an array. It returns the first index at which the element is found or -1 if it's not present.

24. What method do you use to create a new array by applying a function to each element in the existing array?

a) apply() 
b) map() 
c) transform() 
d) forEach()

Answer: 

b) map() 

Explanation: 

The map() method creates a new array by applying a callback function to each element of the original array.

25. How do you define a function in JavaScript? 

a) 
function myFunction() {} 
b) 
let myFunction = function() {};
c) 
const myFunction = () => {};
d) All of the above 

Answer: 

d) All of the above 

Explanation: 

All three options are valid ways to define functions in JavaScript. Option a) is the traditional function declaration, b) is a function expression, and c) is an arrow function (introduced in ES6).

26. What keyword is used to return a value from a JavaScript function? 

a) value 
b) result 
c) return 
d) output 

Answer: 

c) return 

Explanation: 

The return keyword is used to send a value back from the function to the caller. When the return statement is encountered, the function's execution stops, and the value following the return is returned as the result of the function.

27. How do you call a JavaScript function named myFunction? 

a) call myFunction(); 
b) run myFunction(); 
c) myFunction(); 
d) execute myFunction(); 

Answer: 

c) myFunction(); 

Explanation: 

To call a function in JavaScript, you simply use the function's name followed by parentheses ().

28. What is a callback function in JavaScript? 

a) A function that performs asynchronous tasks. 
b) A function that is called at the end of the program's execution. 
c) A function that is passed as an argument to another function and is executed inside that function. 
d) A function that is used for error handling. 

Answer: 

c) A function that is passed as an argument to another function and is executed inside that function.

Explanation: 

A callback function is a function that is passed as an argument to another function and is executed inside that function. It allows for better control of the flow of asynchronous code in JavaScript.

29. What is the primary goal of Object-Oriented Programming (OOP) in JavaScript? 

a) To make the code shorter and more concise. 
b) Organize code into classes and objects. 
c) To execute code faster than procedural programming. 
d) To eliminate the need for functions. 

Answer: 

b) Organize code into classes and objects. 

Explanation: 

The primary goal of OOP in JavaScript is to organize code into reusable and modular structures using classes and objects.

30. What is a class in JavaScript? 

a) A built-in object provided by the JavaScript runtime. 
b) A blueprint or template for creating objects with shared properties and methods. 
c) A single function used to define the behavior of an object. 
d) A reserved keyword used to declare variables. 

Answer: 

b) A blueprint or template for creating objects with shared properties and methods. 

Explanation: 

A class in JavaScript is a blueprint or template that defines the structure and behavior of objects. It serves as a prototype for creating instances (objects) with shared properties and methods.

31. How do you create an object from a class in JavaScript? 

a) 
new object(myClass);
b) 
create object myClass;
c)
let obj = new MyClass();  
d) 
let obj = create(myClass);

Answer: 

c)
let obj = new MyClass(); 

Explanation: 

To create an object from a class in JavaScript, you use the new keyword followed by the class name and parentheses.

32. Which keyword is used to refer to the current instance of a class inside its methods? 

a) self 
b) this 
c) it 
d) current 

Answer: 

b) this 

Explanation: 

The this keyword is used to refer to the current instance of a class inside its methods. It allows access to the object's properties and methods.

33. Which keyword is used to call a method defined in the parent class from a child class in JavaScript? 

a) parent 
b) this 
c) base 
d) super 

Answer: 

d) super 

Explanation: 

The super keyword is used to call a method defined in the parent class from a child class in JavaScript. It allows accessing and invoking the parent class's methods.

34. How do you implement inheritance in JavaScript classes? 

a) Using the extends keyword and specifying the parent class. 
b) Using the inherits keyword and specifying the parent class. 
c) Using the super() method to inherit properties from the parent class. 
d) Using the inheritFrom keyword and specifying the parent class. 

Answer: 

a) Using the extends keyword and specifying the parent class. 

Explanation: 

In JavaScript, you implement inheritance by using the extends keyword in the class declaration of the subclass, followed by the parent class name.

35. How do you declare a string variable in JavaScript? 

a)
let str = 'Hello';
b) 
var str = "Hello";
c) 
const str = 'Hello'; 
d) All of the above 

Answer: 

d) All of the above 

Explanation: 

All three options are valid ways to declare a string variable in JavaScript. You can use single quotes (''), double quotes (""), or backticks (``) for string declaration.

36. What will be the length of the following string?

let myString = "Hello, World!";
a) 11 
b) 12 
c) 13 
d) 14 

Answer: 

c) 13 

Explanation: 

The length of a string in JavaScript is determined by the number of characters it contains, including spaces and punctuation. In this case, the string "Hello, World!" contains 13 characters.

37. What method do you use to convert a string to all lowercase letters? 

a) toLowerCase() 
b) toLower() 
c) lowerCase() 
d) convertToLower() 

Answer: 

a) toLowerCase() 

Explanation: 

The toLowerCase() method is used to convert all the characters in a string to lowercase. It returns a new string with the converted characters.

38. How do you remove leading and trailing whitespace from a string? 

a) trim() 
b) removeWhitespace() 
c) strip() 
d) deleteWhitespace() 

Answer: 

a) trim() 

Explanation: 

The trim() method is used to remove leading and trailing whitespace (spaces, tabs, and line breaks) from a string.

39. Which keyword is used to declare block-scoped variables in ES6? 

a) var 
b) let 
c) const 
d) variable 

Answer: 

b) let 

Explanation: 

In ES6, the let keyword is used to declare block-scoped variables that are limited to the block (enclosing curly braces) in which they are defined.

40. What is the difference between let and const in JavaScript? 

a) let is used for constant values, while const is used for variables. 
b) let can be reassigned, while const is read-only and cannot be reassigned. 
c) let is block-scoped, while const is function-scoped. 
d) There is no difference; let and const are interchangeable. 

Answer: 

b) let can be reassigned, while const is read-only and cannot be reassigned. 

Explanation: 

In JavaScript ES6, let allows variables to be reassigned with new values, while const declares read-only variables that cannot be reassigned once they are initialized.

41. How do arrow functions differ from regular functions in ES6? 

a) Arrow functions have shorter syntax than regular functions. 
b) Arrow functions do not have their own this value. 
c) Arrow functions cannot be used as methods in objects. 
d) All of the above 

Answer: 

d) All of the above 

Explanation: 

Arrow functions in ES6 have shorter syntax, do not have their own this value (they inherit it from the surrounding scope), and cannot be used as constructors or methods in objects.

42. What is the purpose of the ... operator (spread operator) in ES6? 

a) To concatenate arrays. 
b) To destructure objects. 
c) To access the properties of an object. 
d) To spread elements in arrays or objects. 

Answer: 

d) To spread elements in arrays or objects. 

Explanation: 

The ... operator, also known as the spread operator, is used to spread elements of an array or object into another array or object or to pass multiple arguments to a function.

43. What is the purpose of destructuring assignment in ES6? 

a) To assign a value to a variable. 
b) To split an array or object into individual variables. 
c) To access the properties of an object. 
d) To define default values for variables. 

Answer: 

b) To split an array or object into individual variables. 

Explanation: 

Destructuring assignment in ES6 allows extracting values from arrays or properties from objects and assigning them to individual variables, making it easier to work with complex data structures.

44. How do you add an event listener to an HTML element in JavaScript? 

a) 
element.addListener("click", myFunction); 
b) 
element.event("click", myFunction); 
c) 
element.addEventListener("click", myFunction); 
d) 
element.click(myFunction); 

Answer: 

c) 
element.addEventListener("click", myFunction); 

Explanation: 

The addEventListener() method is used to attach an event listener to an HTML element, specifying the event type and the function to be executed when the event occurs. 

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

a) To parse a JSON string and convert it into a JavaScript object. 
b) To convert a JavaScript object or value into a JSON string. 
c) To check if a value is a valid JSON object. 
d) To merge two JSON objects into one. 

Answer: 

b) To convert a JavaScript object or value into a JSON string. 

Explanation: 

The JSON.stringify() method is used to convert a JavaScript object or value into a JSON-formatted string.

46. What is the difference between let and const in ES6? 

a) let is used for constant values, while const is used for variables. 
b) let can be reassigned, while const is read-only and cannot be reassigned. 
c) let is block-scoped, while const is function-scoped. 
d) There is no difference; let and const are interchangeable. 

Answer: 

b) let can be reassigned, while const is read-only and cannot be reassigned. 

Explanation: 

In ES6, let allows variables to be reassigned with new values, while const declares read-only variables that cannot be reassigned once they are initialized.

47. What is the purpose of the const keyword in JavaScript? 

a) To declare a constant value that cannot be changed after initialization. 
b) To declare a variable with block scope. 
c) To define a function in JavaScript. 
d) To store multiple values in an array. 

Answer: 

a) To declare a constant value that cannot be changed after initialization. 

Explanation: 

The const keyword is used to declare a constant variable in JavaScript. Once a value is assigned to a const variable, it cannot be reassigned or changed throughout the program's execution.

48. What is the difference between declaring a variable with var and let? 

a) var is block-scoped, while let is function-scoped. 
b) var variables can be reassigned, while let variables cannot. 
c) var variables are limited to the block in which they are defined, while let variables are accessible throughout the entire function. 
d) There is no difference; var and let can be used interchangeably. 

Answer: 

c) var variables are limited to the block in which they are defined, while let variables are accessible throughout the entire function. 

Explanation: 

The difference lies in their scope. var variables are function-scoped, whereas let variables are block-scoped. var variables are accessible throughout the entire function in which they are defined, while let variables are limited to the block in which they are defined.

49. What is the purpose of the async keyword in JavaScript? 

a) To declare a function as asynchronous. 
b) To define an array of values. 
c) To create a new object. 
d) To fetch data from a remote server. 

Answer: 

a) To declare a function as asynchronous. 

Explanation: 

The async keyword is used to declare a function as asynchronous. An asynchronous function can use await to pause execution until an asynchronous operation is completed.

50. What is the purpose of the localStorage object in JavaScript? 

a) To store sensitive user information on the client side. 
b) To fetch data from an external server. 
c) To manage cookies and sessions. 
d) To store data locally on the user's browser. 

Answer: 

d) To store data locally on the user's browser. 

Explanation: 

The localStorage object allows you to store key-value pairs in a web browser. The data is persisted even after the browser is closed, providing a way to store small amounts of data on the client side.

I hope you found this quiz helpful and educational. Keep practicing to solidify your understanding of JavaScript programming concepts. Happy coding!

Comments