

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of map object in javascript?
According to MDN Docs,
The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
The important thing to note here is that objects can also be used as keys in maps. This is not the case with JavaScript objects. JS objects only allow primitives to be used as keys.
Another feature that makes it useful in certain scenarios is that it is iterable. And it is iterable in order of insertion. So in cases where you need to maintain the order of keys as well as have a value associated with it, a map can be used.
Example
Example usage of maps −
const myMap = new Map(); const keyString = 'a string', const keyObj = {}, const keyFunc = function() {}; // setting the values with all kinds of keys myMap.set(keyString, "String Val"); myMap.set(keyObj, 'Object val'); myMap.set(keyFunc, 'function val'); console.log(myMap.size); console.log(myMap.get(keyString));
Output
This will give the output −
3 String Val
- Related Questions & Answers
- What is the use of Map in JavaScript?
- What is the use of proxy() object in JavaScript?
- Map object in JavaScript.
- What is the use of Object Cloning in Java?
- Javascript Map vs Object — What and when?
- Object to Map conversion in JavaScript
- What is the use of JavaScript cookies?
- What is the use of Atomics in JavaScript?
- What is the use of window.location in javascript?
- What is the use of sentry in javascript?
- What is the use of OBJECT.assign() in javascript?
- Convert object to a Map - JavaScript
- What is the role of Navigator object in JavaScript?
- What is the importance of Navigator Object in JavaScript?
- What is the role of the window.history object in JavaScript?
Advertisements