Maps, just like object,
can be used to store data
as key-value pairs.
But they have a few differences:
-
The keys of an object can only be
of the type string, number or symbol.
Whereas, the keys of a Map can be
of any type.
-
Map is an iterable data type.
Object is not iterable.
-
Map remembers the order in which
each key-value pair was inserted.
Objects do not.
In the example given above,
we created a new map using
the new Map()
constructor.
We pass an array of key-value pairs
as argument to new Map()
.
In the example given above,
we only pass one key-value pair,
i.e., ["name", "Sam Smith"]
.
The "name"
is the key
and "Sam Smith"
is the value.