JavaScript Quiz 25 Questions

JavaScript Multiple Choice Quiz

Question 1: What does JavaScript primarily add to a web page?

A) Style

B) Interactivity

C) Structure

D) Images

Answer: B) Interactivity

Question 2: What is the correct way to declare a variable in JavaScript?

A) let myVar = 10;

B) var myVar = 10;

C) const myVar = 10;

D) all of the above

Answer: D) all of the above

Question 3: Which of the following is a falsy value in JavaScript?

A) 0

B) “false”

C) undefined

D) all of the above

Answer: D) all of the above

Question 4: What is the purpose of the document.getElementById() method in JavaScript?

A) To get the value of an input element

B) To change the page’s title

C) To get an element by its ID

D) To add a new HTML element

Answer: C) To get an element by its ID

Question 5: Which operator is used for equality without type coercion in JavaScript?

A) ===

B) ==

C) =

D) !==

Answer: A) ===

Question 6: What is the correct way to comment 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: A) // This is a comment

Question 7: Which function is used to add a new element at the end of an array in JavaScript?

A) push()

B) pop()

C) shift()

D) unshift()

Answer: A) push()

Question 8: What is the purpose of the setTimeout() function in JavaScript?

A) To set the text of an element

B) To execute a function after a specified delay

C) To change the color of an element

D) To create a new object

Answer: B) To execute a function after a specified delay

Question 9: Which of the following is not a valid JavaScript data type?

A) String

B) Object

C) Function

D) Character

Answer: D) Character

Question 10: What does the addEventListener() method do in JavaScript?

A) Adds a new element to the page

B) Adds a click event to an element

C) Changes the page’s background color

D) Removes an element from the page

Answer: B) Adds a click event to an element

Question 11: How do you declare a function in JavaScript?

A) function myFunction() {}

B) var myFunction = function() {}

C) let myFunction = () => {}

D) All of the above

Answer: D) All of the above

Question 12: What is the purpose of the localStorage object in JavaScript?

A) To store session data on the server

B) To store data in the client’s browser

C) To make AJAX requests

D) To create a new JavaScript file

Answer: B) To store data in the client’s browser

Question 13: What is the result of the expression 5 + “5” in JavaScript?

A) 55

B) 10

C) “10”

D) 15

Answer: C) “10”

Question 14: Which keyword is used to prevent variable hoisting in JavaScript?

A) let

B) var

C) const

D) hoist

Answer: A) let

Question 15: What is the purpose of the return statement in a JavaScript function?

A) To stop the function’s execution

B) To return a value from the function

C) To print a message to the console

D) To declare a new variable

Answer: B) To return a value from the function

Question 16: Which method is used to remove the last element from an array in JavaScript?

A) slice()

B) remove()

C) pop()

D) delete()

Answer: C) pop()

Question 17: How can you convert a string to an integer in JavaScript?

A) parseInt()

B) toInt()

C) convertToInt()

D) strToNum()

Answer: A) parseInt()

Question 18: What is the purpose of the typeof operator in JavaScript?

A) To check if a variable is undefined

B) To check the type of a value or variable

C) To convert a value to a different type

D) To check if a variable is null

Answer: B) To check the type of a value or variable

Question 19: Which loop is used to iterate over the properties of an object in JavaScript?

A) for loop

B) while loop

C) for…in loop

D) do…while loop

Answer: C) for…in loop

Question 20: What is the purpose of the JSON.parse() function in JavaScript?

A) To convert a string to a JSON object

B) To stringify an object

C) To change the value of a variable

D) To add a new element to an array

Answer: A) To convert a string to a JSON object

Question 21: What is the correct way to add a comment that spans multiple lines in JavaScript?

A) /* This is a comment */

B) // This is a comment

C) <!– This is a comment –>

D) # This is a comment #

Answer: A) /* This is a comment */

Question 22: What is the purpose of the Array.isArray() method in JavaScript?

A) To check if a variable is an array

B) To add an element to an array

C) To remove the first element from an array

D) To convert an object to an array

Answer: A) To check if a variable is an array

Question 23: Which JavaScript operator is used to combine two or more strings?

A) +

B) &

C) /

D) –

Answer: A) +

Question 24: What does the Math.random() function return in JavaScript?

A) A random integer

B) A random boolean value

C) A random floating-point number between 0 (inclusive) and 1 (exclusive)

D) A random string

Answer: C) A random floating-point number between 0 (inclusive) and 1 (exclusive)

Question 25: What is the purpose of the try…catch statement in JavaScript?

A) To define a new function

B) To declare a variable

C) To handle exceptions and errors

D) To create a loop

Answer: C) To handle exceptions and errors