JavaScript Quiz: Test Your Knowledge with These 45 Questions

JavaScript QUIZ

How much do you know about JavaScript ?

Skills Test quiz 

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

a) 4
b) ’22’
c) 22
d) 2

Answer: b) ’22’

2. How do you declare a variable in JavaScript?

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

Answer: b) let

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

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

a) /**/
b) —
c) /* */
d) //

Answer: d) //

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

a) lock
b) protect
c) freeze
d) const

Answer: d) const

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

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

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

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

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

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

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

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

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

Answer: b) let

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

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

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

17. How do you handle errors in JavaScript?

a) try-catch
b) error()
c) catch-throw
d) handle-error

Answer: a) try-catch

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

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

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

a) parseInt()
b) toNumber()
c) Number()
d) stringToNumber()

Answer: c) Number()

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

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

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

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

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

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

Answer: a) const

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

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

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

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

a) toString()
b) toText()
c) stringify()
d) numberToString()

Answer: a) toString()

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

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

32. What is the purpose of the “toUpperCase” method in JavaScript?

a) Converts a string to lowercase
b) Converts a string to uppercase
c) Removes leading and trailing whitespaces from a string
d) Splits a string into an array of substrings

Answer: b) Converts a string to uppercase

33. What is the purpose of the “toLowerCase” method in JavaScript?

a) Converts a string to lowercase
b) Converts a string to uppercase
c) Removes leading and trailing whitespaces from a string
d) Splits a string into an array of substrings

Answer: a) Converts a string to lowercase

34. What is the output of the following code?

javascript

Copy code

console.log(1 + ‘1’ – 1);

a) 10
b) 11
c) 01
d) “11”

Answer: b) 11

35. What is the purpose of the “slice” method in JavaScript?

a) Returns a section of a string
b) Removes elements from an array and returns the removed elements
c) Adds elements to the end of an array
d) Reverses the elements of an array

Answer: a) Returns a section of a string

36. What is the purpose of the “concat” method in JavaScript?

a) Combines two arrays into one
b) Creates a new array with the results of calling a function for every array element
c) Adds elements to the end of an array
d) Removes elements from an array

Answer: a) Combines two arrays into one

37. What is the purpose of the “slice” method in JavaScript?

a) Adds elements to an array and returns the new array length
b) Returns a section of a string
c) Removes elements from an array and returns the removed elements
d) Concatenates two strings

Answer: b) Returns a section of a string

38. What is the purpose of the “toFixed” method in JavaScript?

a) Converts a number to a fixed-point notation
b) Rounds a number to the nearest integer
c) Converts a string to a number
d) Returns the decimal part of a number

Answer: a) Converts a number to a fixed-point notation

39. What is the purpose of the “split” method in JavaScript?

a) Adds elements to an array and returns the new array length
b) Returns a section of a string
c) Splits a string into an array of substrings
d) Joins two arrays into one

Answer: c) Splits a string into an array of substrings

40. What is the purpose of the “reverse” method in JavaScript?

a) Reverses the order of elements in an array
b) Returns the character at a specified index in a string
c) Converts a string to uppercase
d) Removes elements from an array and returns the removed elements

Answer: a) Reverses the order of elements in an array

41. How do you round a number to a specific number of decimal places in JavaScript?

a) toFixed()
b) round()
c) decimalRound()
d) setPrecision()

Answer: a) toFixed()

42. What does the “splice” method do in JavaScript?

a) Removes elements from an array and returns the removed elements
b) Adds elements to an array and returns the new array length
c) Concatenates two arrays
d) Reverses the elements of an array

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

43. What is the purpose of the “Object.keys()” method in JavaScript?

a) Returns an array of a given object’s property names
b) Returns the keys of an array
c) Returns an array of a given object’s property values
d) Adds a new key to an object

Answer: a) Returns an array of a given object’s property names

44. What does the “Math.random()” function do in JavaScript?

a) Generates a random integer between 1 and 10
b) Generates a random decimal between 0 and 1
c) Rounds a number to the nearest integer
d) Generates a random number with a specified range

Answer: b) Generates a random decimal between 0 and 1

45. What is the purpose of the “Math.floor()” function in JavaScript?

a) Returns the smallest integer less than or equal to a given number
b) Rounds a number to the nearest integer
c) Returns the largest integer less than or equal to a given number
d) Returns the closest integer greater than or equal to a given number

Answer: a) Returns the smallest integer less than or equal to a given number

Learn more about the answers

  1. Result of 2 + '2'
    • Answer: b) ’22’
    • Explanation: In JavaScript, when you use the + operator with a string and a number, the number is implicitly converted to a string, resulting in string concatenation.
  2. How to declare a variable in JavaScript?
    • Answer: b) let
    • Explanation: The let keyword is used to declare variables in modern JavaScript. It allows you to declare a variable with block scope.
  3. Purpose of the “typeof” operator in JavaScript?
    • 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?
    • Answer: d) //
    • Explanation: Single-line comments in JavaScript are written using //.
  5. Keyword to prevent variable modification in JavaScript?
    • Answer: d) const
    • Explanation: The const keyword is used to declare constants in JavaScript, preventing variable reassignment.
  6. Purpose of the “parseInt” function?
    • Answer: a) Parses a string and returns an integer
    • Explanation: parseInt is used to parse a string and return an integer.
  7. How to concatenate two strings in JavaScript?
    • Answer: b) str1 + str2
    • Explanation: The + operator is used for string concatenation in JavaScript.
  8. Purpose of the “push” method in JavaScript?
    • Answer: b) Adds elements to the end of an array
    • Explanation: The push method adds elements to the end of an array.
  9. How to declare a function in JavaScript?
    • Answer: a) function myFunction()
    • Explanation: Functions are declared using the function keyword followed by the function name.
  10. Difference between “==” and “===” in JavaScript?
  • 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.
  1. How to check if a variable is an array in JavaScript?
  • Answer: a) isArray()
  • Explanation: The isArray function is used to check if a variable is an array.
  1. Purpose of the “map” function in JavaScript?
  • 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.
  1. Keyword to declare a block-scoped variable in JavaScript?
  • Answer: b) let
  • Explanation: The let keyword declares block-scoped variables.
  1. What is a closure in JavaScript?
  • 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.
  1. Purpose of the “return” statement in a function?
  • Answer: a) To exit the function and return a value
  • Explanation: The return statement is used to exit a function and return a value.
  1. What does the “JSON” acronym stand for in JavaScript?
  • Answer: a) JavaScript Object Notation
  • Explanation: JSON stands for JavaScript Object Notation, a lightweight data interchange format.
  1. How to handle errors in JavaScript?
  • Answer: a) try-catch
  • Explanation: The try-catch statement is used to handle errors in JavaScript.
  1. Purpose of the “isNaN” function in JavaScript?
  • Answer: a) Checks if a value is not a number
  • Explanation: isNaN checks if a value is not a number.
  1. Purpose of the “splice” method in JavaScript?
  • Answer: b) Removes elements from an array and returns the removed elements
  • Explanation: The splice method is used to modify an array by adding or removing elements.
  1. How to convert a string to a number in JavaScript?
  • Answer: c) Number()
  • Explanation: The Number function is used to convert a value to a number.
  1. Purpose of the “bind” method in JavaScript?
  • 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.
  1. What is an IIFE in JavaScript?
  • Answer: a) A function that is automatically invoked when the script loads
  • Explanation: IIFE (Immediately Invoked Function Expression) is a function that is executed immediately after it’s created.
  1. Purpose of the “Promise” object in JavaScript?
  • 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 for asynchronous programming in JavaScript.
  1. Purpose of the “filter” method in JavaScript?
  • 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 test.
  1. How to declare a constant variable in JavaScript?
  • Answer: a) const
  • Explanation: The const keyword is used to declare constant variables.
  1. Purpose of the “addEventListener” method in JavaScript?
  • Answer: a) Adds an event listener to a DOM element
  • Explanation: addEventListener is used to attach an event handler to an element.
  1. Purpose of the “charAt” method in JavaScript?
  • Answer: a) Returns the character at a specified index in a string
  • Explanation: The charAt method returns the character at the specified index in a string.
  1. Purpose of the “shift” method in JavaScript?
  • Answer: b) Removes elements from the beginning of an array
  • Explanation: The shift method removes the first element from an array.
  1. How to convert a number to a string in JavaScript?
  • Answer: a) toString()
  • Explanation: The toString method converts a number to a string.
  1. Purpose of the “find” method in JavaScript?
  • Answer: c) Returns the first element in an array that satisfies a provided testing function
  • Explanation: The find method returns the first element that satisfies the specified condition.
  1. Purpose of the “includes” method in JavaScript?
  • Answer: c) Checks if an array includes a certain element
  • Explanation: The includes method checks if an array includes a specific element.
  1. Purpose of the “toUpperCase” method in JavaScript?
  • Answer: b) Converts a string to uppercase
  • Explanation: The toUpperCase method converts a string to uppercase.
  1. Purpose of the “toLowerCase” method in JavaScript?
  • Answer: a) Converts a string to lowercase
  • Explanation: The toLowerCase method converts a string to lowercase.
  1. Output of console.log(1 + '1' - 1)
  • Answer: b) 11
  • Explanation: JavaScript performs implicit type coercion, resulting in the string concatenation of ‘1’ + 1, which is then subtracted.
  1. Purpose of the “slice” method in JavaScript?
  • Answer: a) Returns a section of a string
  • Explanation: The slice method returns a portion of a string.
  1. Purpose of the “concat” method in JavaScript?
  • Answer: a) Combines two arrays into one
  • Explanation: The concat method combines two arrays into a new one.
  1. Purpose of the “slice” method in JavaScript?
  • Answer: b) Returns a section of a string
  • Explanation: The slice method returns a specified section of a string.
  1. Purpose of the “toFixed” method in JavaScript?
  • Answer: a) Converts a number to a fixed-point notation
  • Explanation: The toFixed method formats a number using fixed-point notation.
  1. Purpose of the “split” method in JavaScript?
  • Answer: c) Splits a string into an array of substrings
  • Explanation: The split method divides a string into an array of substrings based on a specified delimiter.
  1. Purpose of the “reverse” method in JavaScript?
  • Answer: a) Reverses the order of elements in an array
  • Explanation: The reverse method changes the order of elements in an array to the opposite.
  1. How to round a number to a specific decimal place in JavaScript?
  • Answer: a) toFixed()
  • Explanation: The toFixed method rounds a number to a specified number of decimal places.
  1. Purpose of the “splice” method in JavaScript?
  • Answer: a) Removes elements from an array and returns the removed elements
  • Explanation: The splice method modifies an array by removing or replacing existing elements.
  1. Purpose of the “Object.keys()” method in JavaScript?
  • Answer: a) Returns an array of a given object’s property names
  • Explanation: Object.keys() returns an array of a given object’s property names.
  1. What does the “Math.random()” function do in JavaScript?
  • Answer: b) Generates a random decimal between 0 and 1
  • Explanation: Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive).
  1. Purpose of the “Math.floor()” function in JavaScript?
  • Answer: a) Returns the smallest integer less than or equal to a given number
  • Explanation: Math.floor() rounds a number down to the nearest integer.