JavaScript 50 Quiz Questions

1. What is the output of the following code?

console.log(typeof null);

A) “object”
B) “null”
C) “undefined”
D) “boolean”

Answer: A) “object”

2. How can you create an immutable object in JavaScript?

A) Object.freeze(obj)
B) Object.seal(obj)
C) Object.preventExtensions(obj)
D) Object.immutable(obj)

Answer: A) Object.freeze(obj)

3. What does the reduce method do?

A) Iterates over an array and returns a new array
B) Iterates over an array and returns a single value
C) Iterates over an object and returns an array
D) Iterates over an array and returns a boolean

Answer: B) Iterates over an array and returns a single value

4. Which of the following is not a valid way to create a new array?

A) new Array()
B) Array.of()
C) Array.create()
D) []

Answer: C) Array.create()

5. What will the following code output?

console.log([1, 2, 3] + [4, 5, 6]);

A) “1,2,34,5,6”
B) “1,2,3,4,5,6”
C) [1, 2, 3, 4, 5, 6]
D) “1,2,34,5,6”

Answer: A) “1,2,34,5,6”

6. Which method is used to merge two or more arrays?

A) Array.concat()
B) Array.merge()
C) Array.join()
D) Array.add()

Answer: A) Array.concat()

7. What will the following code output?

let a = 5;

let b = a++;

console.log(a, b);

A) 5, 5
B) 6, 5
C) 5, 6
D) 6, 6

Answer: B) 6, 5

8. Which of the following is a JavaScript data type?

A) Integer
B) Float
C) Number
D) Double

Answer: C) Number

9. What does the splice method do?

A) Adds elements to the end of an array
B) Removes elements from an array and optionally inserts new elements in their place
C) Sorts the elements of an array
D) Removes the first element from an array

Answer: B) Removes elements from an array and optionally inserts new elements in their place

10. What will the following code output?

console.log(“Hello” instanceof String);

A) true
B) false
C) TypeError
D) undefined

Answer: B) false

11. Which of the following will NOT create an empty object?

A) {}
B) new Object()
C) Object.create(null)
D) Object.empty()

Answer: D) Object.empty()

12. How can you determine if a variable is an array?

A) typeof variable === ‘array’
B) variable instanceof Array
C) Array.isArray(variable)
D) B and C

Answer: D) B and C

13. What is the output of the following code?

console.log(0.1 + 0.2 === 0.3);

A) true
B) false
C) TypeError
D) undefined

Answer: B) false

14. Which statement is used to handle exceptions in JavaScript?

A) try…catch
B) try…except
C) catch…try
D) try…finally

Answer: A) try…catch

15. What does the map method return?

A) A single value
B) A new array with the results of calling a provided function on every element
C) The first element that matches a condition
D) A boolean value

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

16. How can you create a new promise?

A) new Promise((resolve, reject) => {})
B) Promise.create((resolve, reject) => {})
C) new Promise.create((resolve, reject) => {})
D) Promise((resolve, reject) => {})

Answer: A) new Promise((resolve, reject) => {})

17. What is the purpose of the super keyword in JavaScript?

A) To refer to the parent class constructor
B) To call a superclass method
C) To create a new object
D) To define a new class

Answer: A) To refer to the parent class constructor

18. Which method is used to return the keys of an object?

A) Object.values()
B) Object.keys()
C) Object.entries()
D) Object.getKeys()

Answer: B) Object.keys()

19. What does the finally block do?

A) It executes if no error occurs in the try block
B) It executes only if an error occurs in the try block
C) It executes regardless of whether an error occurs or not
D) It catches errors

Answer: C) It executes regardless of whether an error occurs or not

20. What is the output of the following code?

console.log(typeof NaN);

A) “number”
B) “NaN”
C) “undefined”
D) “object”

Answer: A) “number”

21. Which statement is true about the for…in loop?

A) It iterates over all enumerable properties of an array
B) It iterates over all enumerable properties of an object
C) It iterates over the values of an array
D) It iterates over the values of an object

Answer: B) It iterates over all enumerable properties of an object

22. What is the output of the following code?

let x = 10;

if (x) {

  console.log(“x is true”);

}

A) “x is true”
B) Nothing
C) “x is false”
D) TypeError

Answer: A) “x is true”

23. Which of the following is not a primitive data type in JavaScript?

A) String
B) Number
C) Boolean
D) Object

Answer: D) Object

24. What will the following code output?

console.log(2 ** 3);

A) 6
B) 8
C) 9
D) 12

Answer: B) 8

25. Which method removes the last element from an array?

A) shift()
B) unshift()
C) pop()
D) push()

Answer: C) pop()

26. What is the output of the following code?

console.log(‘2’ + 2);

A) 4
B) “22”
C) “4”
D) TypeError

Answer: B) “22”

27. How can you make a shallow copy of an array?

A) Array.copy()
B) Array.slice()
C) Array.clone()
D) Array.duplicate()

Answer: B) Array.slice()

28. What will the following code output?

let obj = { a: 1, b: 2 };

console.log(‘a’ in obj);

A) true
B) false
C) “a”
D) TypeError

Answer: A) true

29. What is the result of the following expression?

null == undefined;

A) true
B) false
C) TypeError
D) undefined

Answer: A) true

30. Which of the following methods returns a promise that resolves after all of the promises in the iterable have resolved?

A) Promise.all()
B) Promise.race()
C) Promise.resolve()
D) Promise.any()

Answer: A) Promise.all()

31. What will the following code output?

console.log(typeof function() {});

A) “object”
B) “function”
C) “undefined”
D) “string”

Answer: B) “function”

32. What does the Object.assign() method do?

A) Copies the values of all enumerable own properties from one or more source objects to a target object
B) Creates a new object with the specified prototype object and properties
C) Seals an object, preventing new properties from being added and marking all existing properties as non-configurable
D) Freezes an object, making it immutable

Answer: A) Copies the values of all enumerable own properties from one or more source objects to a target object

33. What is the purpose of the Symbol type?

A) To create unique identifiers for object properties
B) To create new primitive data types
C) To create large integers
D) To create non-enumerable properties

Answer: A) To create unique identifiers for object properties

34. What will the following code output?

let [a, b] = [1, 2];

[a, b] = [b, a];

console.log(a, b);

A) 1, 2
B) 2, 1
C) undefined, undefined
D) 1, undefined

Answer: B) 2, 1

35. What does the Array.prototype.flat() method do?

A) Flattens an array of arrays into a single array
B) Maps and flattens an array
C) Filters and flattens an array
D) Reduces and flattens an array

Answer: A) Flattens an array of arrays into a single array

36. What is the result of the following code?

console.log(1 == ‘1’);

console.log(1 === ‘1’);

A) true, true
B) false, false
C) true, false
D) false, true

Answer: C) true, false

37. How can you make an object immutable?

A) Object.preventExtensions(obj)
B) Object.freeze(obj)
C) Object.seal(obj)
D) Object.lock(obj)

Answer: B) Object.freeze(obj)

38. What is the output of the following code?

let arr = [1, 2, 3];

console.log(arr.includes(2));

A) true
B) false
C) TypeError
D) undefined

Answer: A) true

39. What is the output of the following code?

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

A) 22
B) 2
C) 20
D) “22”

Answer: C) 20

40. What is the purpose of the Proxy object?

A) To intercept and redefine fundamental operations for an object
B) To create new objects with dynamic behavior
C) To clone objects
D) To create immutable objects

Answer: A) To intercept and redefine fundamental operations for an object

41. What will the following code output?

console.log([…’hello’]);

A) [“h”, “e”, “l”, “l”, “o”]
B) [“hello”]
C) [“h”, “e”, “l”, “l”]
D) [“h”, “e”, “l”, “o”]

Answer: A) [“h”, “e”, “l”, “l”, “o”]

42. Which method creates a new, shallow-copied array instance from an array-like or iterable object?

A) Array.of()
B) Array.from()
C) Array.create()
D) Array.new()

Answer: B) Array.from()

43. What will the following code output?

console.log(typeof NaN === ‘number’);

A) true
B) false
C) undefined
D) TypeError

Answer: A) true

44. What does the Object.create() method do?

A) Creates a new object with the specified prototype object and properties
B) Creates a new empty object
C) Copies all properties from one object to another
D) Freezes an object

Answer: A) Creates a new object with the specified prototype object and properties

45. What is the output of the following code?

console.log(‘5’ – 3);

A) “2”
B) 2
C) “53”
D) TypeError

Answer: B) 2

46. Which method adds one or more elements to the end of an array?

A) push()
B) pop()
C) shift()
D) unshift()

Answer: A) push()

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

A) To define an asynchronous function that returns a promise
B) To pause the execution of a function
C) To handle synchronous code
D) To create a callback function

Answer: A) To define an asynchronous function that returns a promise

48. What will the following code output?

console.log([1, 2, 3].map(x => x * 2));

A) [1, 2, 3]
B) [2, 4, 6]
C) [3, 6, 9]
D) TypeError

Answer: B) [2, 4, 6]

49. What is the purpose of the finally block in a try…catch statement?

A) To execute code after the try block, regardless of the result
B) To handle errors that are not caught
C) To throw new errors
D) To define asynchronous code

Answer: A) To execute code after the try block, regardless of the result

50. What does the reduce method return when applied to an empty array with no initial value?

A) undefined
B) null
C) 0
D) TypeError

Answer: D) TypeError