JavaScript Test your knowledge Quiz

JavaScript Test your knowledge Quiz

Question 1: What is the primary purpose of the this keyword in JavaScript?

A) To refer to the previous line of code

B) To reference the current object or context

C) To access the parent element in the DOM

D) To declare a new variable

Answer: B) To reference the current object or context

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

A) splice()

B) slice()

C) shift()

D) pop()

Answer: D) pop()

Question 3: How do you declare a constant variable in JavaScript?

A) let

B) var

C) const

D) constant

Answer: C) const

Question 4: What is the purpose of the map() function in JavaScript?

A) To create a new array with the results of calling a provided function on every element in the array

B) To remove elements from an array

C) To concatenate two arrays

D) To sort an array in ascending order

Answer: A) To create a new array with the results of calling a provided function on every element in the array

Question 5: Which operator is used to combine two or more conditions in an if statement in JavaScript?

A) and

B) &

C) &&

D) ||

Answer: C) &&

Question 6: What is the purpose of the break statement in JavaScript?

A) To exit a loop or switch statement prematurely

B) To skip the current iteration of a loop

C) To continue to the next iteration of a loop

D) To define a new function

Answer: A) To exit a loop or switch statement prematurely

Question 7: What is the result of the expression 2 + “2” in JavaScript?

A) 22

B) 4

C) 2 + 2

D) TypeError

Answer: A) 22

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

A) To convert a string to a floating-point number

B) To convert a floating-point number to a string

C) To convert a string to an integer

D) To convert an integer to a string

Answer: C) To convert a string to an integer

Question 9: What is the JavaScript operator for checking if two values are not equal, regardless of their data types?

A) !=

B) !==

C) =/=

D) ==

Answer: B) !==

Question 10: Which JavaScript function is used to find the index of the first occurrence of a specified value in an array?

A) find()

B) indexOf()

C) search()

D) contains()

Answer: B) indexOf()

Question 11: 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 12: How do you declare a multiline string in JavaScript?

A) Enclose the string in double quotes

B) Use the backslash character at the end of each line

C) Enclose the string in single quotes

D) Use template literals with backticks

Answer: D) Use template literals with backticks

Question 13: What does the querySelector() method do in JavaScript?

A) Selects the first element that matches a specified CSS selector

B) Adds an event listener to an element

C) Changes the background color of the page

D) Removes an element from the DOM

Answer: A) Selects the first element that matches a specified CSS selector

Question 14: 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 15: How can you add a comment in your JavaScript code that won’t be executed?

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 16: What is the result of typeof null in JavaScript?

A) “undefined”

B) “object”

C) “null”

D) “string”

Answer: B) “object”

Question 17: Which JavaScript function is used to add elements to the end of an array?

A) push()

B) append()

C) add()

D) concat()

Answer: A) push()

Question 18: 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

Question 19: Which method is used to convert an object to a JSON string in JavaScript?

A) objectToString()

B) toJSON()

C) JSON.stringify()

D) stringifyObject()

Answer: C) JSON.stringify()

Question 20: What does the Math.floor() function do in JavaScript?

A) Rounds a number up to the nearest integer

B) Rounds a number down to the nearest integer

C) Returns the absolute value of a number

D) Computes the square root of a number

Answer: B) Rounds a number down to the nearest integer

Question 21: Which JavaScript function is used to remove the first element from an array?

A) shift()

B) removeFirst()

C) splice()

D) delete()

Answer: A) shift()

Question 22: What is the purpose of the event.preventDefault() method in JavaScript?

A) To execute the default action for an event

B) To stop the event from propagating to parent elements

C) To prevent the default behavior of an event

D) To trigger an event

Answer: C) To prevent the default behavior of an event

Question 23: What is the result of the expression “5” – 2 in JavaScript?

A) 7

B) “52”

C) 3

D) NaN

Answer: C) 3

Question 24: How can you check the length of an array in JavaScript?

A) Using the length() method

B) Using the count property

C) Using the size() method

D) Using the length property

Answer: D) Using the length property