Test Your JavaScript Knowledge with These 32 Quiz Questions

JavaScript QUIZ 

30 Questions

Test your knowledge

How many can you answer?

1. What is the result of the expression 2 + ‘2’?

a) 4

b) ’22’

c) 22

d) 2

Answer: b) ’22’

Explanation: In JavaScript, the + operator with a string and a number results in string concatenation.

2. How do you declare a variable in JavaScript?

a) v

b) let

c) variable

d) var

Answer: b) let

Explanation: The let keyword is used to declare variables in modern JavaScript.

3. What does the “typeof” operator do in JavaScript?

a) Checks if a variable is defined

b) Returns the type of a variable

c) Converts a variable to a boolean

d) Compares two variables

Answer: b) Returns the type of a variable

Explanation: The typeof operator returns a string indicating the type of the operand.

4. How is a single-line comment written in JavaScript?

a) /**/

b) —

c) /* */

d) //

Answer: d) //

Explanation: Single-line comments in JavaScript are written using //.

5. Which keyword is used to prevent variable modification in JavaScript?

a) lock

b) protect

c) freeze

d) const

Answer: d) const

Explanation: The const keyword is used to declare constants in JavaScript, preventing variable reassignment.

6. What does the “parseInt” function do?

a) Parses a string and returns an integer

b) Rounds a number to the nearest integer

c) Returns the decimal part of a number

d) Converts a string to uppercase

Answer: a) Parses a string and returns an integer

Explanation: parseInt is used to parse a string and return an integer.

7. How do you concatenate two strings in JavaScript?

a) str1.join(str2)

b) str1 + str2

c) concat(str1, str2)

d) str1.combine(str2)

Answer: b) str1 + str2

Explanation: The + operator is used for string concatenation in JavaScript.

8. What is the purpose of the “push” method in JavaScript?

a) Adds elements to the beginning of an array

b) Adds elements to the end of an array

c) Removes elements from an array

d) Reverses the order of elements in an array

Answer: b) Adds elements to the end of an array

Explanation: The push method adds elements to the end of an array.

9. How do you declare a function in JavaScript?

a) function myFunction()

b) def myFunction()

c) func myFunction()

d) create myFunction()

Answer: a) function myFunction()

Explanation: Functions are declared using the function keyword followed by the function name.

10. What is the difference between “==” and “===” in JavaScript?

– a) They are the same

– b) “==” compares values for equality with type coercion, and “===” compares values for equality without type coercion

– c) “===” compares values for equality with type coercion, and “==” compares values for equality without type coercion

– d) “==” is used for strict equality, and “===” is used for loose equality

– Answer: b) “==” compares values for equality with type coercion, and “===” compares values for equality without type coercion

– Explanation: == performs type coercion, while === checks both value and type without coercion.

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

– a) isArray()

– b) isTypeOf(‘array’)

– c) typeofArray()

– d) arrayCheck()

– Answer: a) isArray()

– Explanation: The isArray function is used to check if a variable is an array.

12. What is the purpose of the “map” function in JavaScript?

– a) Iterates over the elements of an array and applies a function to each element

– b) Creates a new array with all elements that pass a test

– c) Filters the elements of an array based on a condition

– d) Reduces the array to a single value

– Answer: a) Iterates over the elements of an array and applies a function to each element

– Explanation: The map function applies a given function to each element of an array.

13. Which keyword is used to declare a block-scoped variable in JavaScript?

– a) var

– b) let

– c) const

– d) block

– Answer: b) let

– Explanation: The let keyword declares block-scoped variables.

14. What is a closure in JavaScript?

– a) A variable declared within a function

– b) A function inside another function that has access to the outer function’s variables

– c) A way to close a function

– d) A type of loop

– Answer: b) A function inside another function that has access to the outer function’s variables

– Explanation: A closure is a function defined inside another function, allowing access to the outer function’s variables.

15. What is the purpose of the “return” statement in a function?

– a) To exit the function and return a value

– b) To print a value to the console

– c) To declare a variable

– d) To concatenate strings

– Answer: a) To exit the function and return a value

– Explanation: The return statement is used to exit a function and return a value.

16. What does the “JSON” acronym stand for in JavaScript?

– a) JavaScript Object Notation

– b) Java Syntax Object Notation

– c) Just Simple Object Naming

– d) JavaScript Oriented Notation

– Answer: a) JavaScript Object Notation

– Explanation: JSON stands for JavaScript Object Notation, a lightweight data interchange format.

17. How do you handle errors in JavaScript?

– a) try-catch

– b) error()

– c) catch-throw

– d) handle-error

– Answer: a) try-catch

– Explanation: The try-catch statement is used to handle errors in JavaScript.

18. What is the purpose of the “isNaN” function in JavaScript?

– a) Checks if a value is not a number

– b) Checks if a value is a number

– c) Converts a string to a number

– d) Rounds a number to the nearest integer

– Answer: a) Checks if a value is not a number

– Explanation: The isNaN function checks if a value is not a number.

19. What is the purpose of the “splice” method in JavaScript?

– a) Adds elements to an array and returns the new array length

– b) Removes elements from an array and returns the removed elements

– c) Concatenates two arrays

– d) Reverses the elements of an array

– Answer: b) Removes elements from an array and returns the removed elements

– Explanation: The splice method modifies an array by removing or replacing existing elements.

20. How do you convert a string to a number in JavaScript?

– a) parseInt()

– b) toNumber()

– c) Number()

– d) stringToNumber()

– Answer: c) Number()

– Explanation: The Number function is used to convert a value to a number in JavaScript.

21. What does the “bind” method do in JavaScript?

– a) Creates a new array with the results of calling a function for every array element

– b) Binds a function to a particular object, ensuring that “this” refers to the specified object

– c) Combines two arrays into one

– d) Creates a copy of an array with the elements reversed

– Answer: b) Binds a function to a particular object, ensuring that “this” refers to the specified object

– Explanation: The bind method creates a new function with a specified this value and initial arguments.

22. What is an IIFE (Immediately Invoked Function Expression) in JavaScript?

– a) A function that is automatically invoked when the script loads

– b) A function that is executed only when called explicitly

– c) A function that is defined inside another function

– d) A function that runs forever

– Answer: a) A function that is automatically invoked when the script loads

– Explanation: An IIFE is a function that is immediately invoked as soon as it’s defined.

23. What is the purpose of the “Promise” object in JavaScript?

– a) To handle asynchronous operations and represent a value that might be available now, or in the future, or never

– b) To create a new object

– c) To check if a variable is defined

– d) To concatenate strings

– Answer: a) To handle asynchronous operations and represent a value that might be available now, or in the future, or never

– Explanation: Promises are used to handle asynchronous operations and provide a way to work with asynchronous code.

24. What is the purpose of the “filter” method in JavaScript?

– a) Iterates over the elements of an array and applies a function to each element

– b) Creates a new array with all elements that pass a test

– c) Combines two arrays into one

– d) Reduces the array to a single value

– Answer: b) Creates a new array with all elements that pass a test

– Explanation: The filter method creates a new array with elements that pass a specified condition.

25. How do you declare a constant variable in JavaScript?

– a) const

– b) let

– c) var

– d) constant

– Answer: a) const

– Explanation: The const keyword is used to declare constant variables in JavaScript.

26. What does the “addEventListener” method do in JavaScript?

– a) Adds an event listener to a DOM element

– b) Removes an event listener from a DOM element

– c) Appends a new child element to a parent element

– d) Adds a new event to the event queue

– Answer: a) Adds an event listener to a DOM element

– Explanation: The addEventListener method attaches an event handler to a specified element.

27. What is the purpose of the “charAt” method in JavaScript?

– a) Returns the character at a specified index in a string

– b) Concatenates two strings

– c) Converts a string to uppercase

– d) Returns the length of a string

– Answer: a) Returns the character at a specified index in a string

– Explanation: The charAt method returns the character at a specified index in a string.

28. What is the purpose of the “shift” method in JavaScript?

– a) Adds elements to the end of an array

– b) Removes elements from the beginning of an array

– c) Adds elements to the beginning of an array

– d) Removes elements from the end of an array

– Answer: b) Removes elements from the beginning of an array

– Explanation: The shift method removes the first element from an array.

29. How do you convert a number to a string in JavaScript?

– a) toString()

– b) toText()

– c) stringify()

– d) numberToString()

– Answer: a) toString()

– Explanation: The toString method converts a number to a string in JavaScript.

30. What is the purpose of the “find” method in JavaScript?

– a) Iterates over the elements of an array and applies a function to each element

– b) Creates a new array with all elements that pass a test

– c) Returns the first element in an array that satisfies a provided testing function

– d) Reduces the array to a single value

– Answer: c) Returns the first element in an array that satisfies a provided testing function

– Explanation: The find method returns the first element in an array that satisfies a provided testing function.

31. What is the purpose of the “includes” method in JavaScript?

– a) Checks if a value is not a number

– b) Checks if a value is a number

– c) Checks if an array includes a certain element

– d) Checks if a string includes a certain substring

– Answer: c) Checks if an array includes a certain element

– Explanation: The includes method checks if an array includes a specific element.