- Object only allows using String or Symbol as key.
- The map allows any data type (String, Number, Boolean, NaN, Object,…) can be key.
To initialize an empty Map, you must pass no parameters to the constructor.
Use Map. size method
Here we will create an empty Map and check if it is empty or not by comparing its length with the value 0. If true, then returns true, and if false, returns false.
So we can check whether any map is empty or not with a basic code. If you want to learn more ways to do it, follow the next part of the article.
Use Object.keys(Map).length method
Object. keys() is a method used to create an array with all an object’s keys.
- Obj: an object whose properties are to be returned by the enumerable.
However, here we will only take a single key, the exampleMap declared above. And with the Object. Keys (Map).length method, we can get the length of the map we need to find and check if it is empty.
With this approach, we also achieve the same result as the one above, but we must be careful not to declare more than one variable with the same key as the map being checked. Hope it will help you.
Maybe you are interested:
Name of the university: PTIT