JavaScript Map

Last Updated :
Discuss
Comments

Question 1

How can you iterate over all the keys in a Map?

  • map.keys()

  • map.values()

  • map.forEach()

  • map.getKeys()

Question 2

Which data structure is best implemented using a Map in JavaScript?

  •  Stack

  • Queue

  • Hash Table

  • Binary Search Tree

Question 3

How can you efficiently check if a key exists in a Map?

  • map.contains(key)

  • map.has(key)

  • map.exists(key)

  • map.includes(key)

Question 4

What will be the output of the following code?

JavaScript
const map = new Map();
map.set(42, 'value1');
map.set('42', 'value2');
console.log(map.get(42));



  • 'value1'

  •  'value2'

  • undefined

  • Error

Question 5

Which of the following methods is used to delete an entry from a Map?

  • map.remove(key)

  • map.delete(key)

  • map.erase(key)

  • map.drop(key)

Question 6

What is the time complexity of retrieving a value from a JavaScript Map using map.get(key)?

  •  O(1)

  •  O(n)

  • O(log n)

  •  O(n log n)

Question 7

What will be the output of the following code?

JavaScript
const map = new Map();
map.set(true, "boolean");
map.set(1, "number");
map.set("1", "string");
console.log(map.get(1));


  • boolean

  •  number

  • string

  • undefined

Question 8

What will be the output of the following code?

JavaScript
const map = new Map();
map.set('1', 'one');
map.set(1, 'ONE');
console.log(map.get('1'));


  • ONE

  • one

  • undefined

  • Error

Question 9

What is the most efficient way to clear all elements from a Map?

  • map.removeAll()

  • map.clear()

  • map.deleteAll()

  • map = new Map();

Question 10

How can you convert a Map into an array of its values?

  • map.toArray()

  • [...map]

  •  [...map.values()]

  • Array.from(map.values())

There are 10 questions to complete.

Take a part in the ongoing discussion