What is the purpose of the WeakMap in JavaScript?
a) To store key-value pairs where the keys are objects and the values can be garbage collected if no other references exist
b) To store primitive values and prevent memory leaks
c) To create a map with weak references to keys
d) To store values that are automatically removed when their key is no longer referenced
Answer:
a) To store key-value pairs where the keys are objects and the values can be garbage collected if no other references exist
Explanation:
A WeakMap is a collection of key-value pairs where the keys are objects, and the values can be garbage collected if there are no other references to the keys. This makes WeakMap useful for managing memory when working with objects.