JS Coding Questions Logo
JS Coding Questions
#208💼 Interview

What are the differences between WeakMap and Map

Advertisement

728x90

The main difference is that references to key objects in Map are strong while references to key objects in WeakMap are weak. i.e, A key object in WeakMap can be garbage collected if there is no other reference to it.

Other differences are,

1. Map can store any key type whereas WeakMap can store only collections of key objects

2. WeakMap does not have size property unlike Map

3. WeakMap does not have methods such as clear, keys, values, entries, forEach.

4. WeakMap is not iterable.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 38

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
208of476