JavaScript Map

Last Updated :
Discuss
Comments

Question 1

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

  • A

    map.keys()

  • B

    map.values()

  • C

    map.forEach()

  • D

    map.getKeys()

Question 2

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

  • A

     Stack

  • B

    Queue

  • C

    Hash Table

  • D

    Binary Search Tree

Question 3

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

  • A

    map.contains(key)

  • B

    map.has(key)

  • C

    map.exists(key)

  • D

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



  • A

    'value1'

  • B

     'value2'

  • C

    undefined

  • D

    Error

Question 5

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

  • A

    map.remove(key)

  • B

    map.delete(key)

  • C

    map.erase(key)

  • D

    map.drop(key)

Question 6

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

  • A

     O(1)

  • B

     O(n)

  • C

    O(log n)

  • D

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


  • A

    boolean

  • B

     number

  • C

    string

  • D

    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'));


  • A

    ONE

  • B

    one

  • C

    undefined

  • D

    Error

Question 9

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

  • A

    map.removeAll()

  • B

    map.clear()

  • C

    map.deleteAll()

  • D

    map = new Map();

Question 10

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

  • A

    map.toArray()

  • B

    [...map]

  • C

     [...map.values()]

  • D

    Array.from(map.values())

There are 10 questions to complete.

Take a part in the ongoing discussion