
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the use of Map in JavaScript?
Map
Map holds key value pairs and remembers the actual insertion order of the keys. Map allows to store only a unique value.
syntax
new Map([iterable])
Case-1: Absence Of Map
In the absence of Map, since javascript object endorses only one key object, If we provide multiple keys only the last one will be remembered. In the following example despite providing many keys such as a and b only b is remembered and displayed as output.So to eliminate this drawback "Map" came in to existence in javascript.
Example
<html> <body> <script> const x = {}; const a = {}; const b = { num:3 } x[a] = "a"; x[b] = "b"; document.write(JSON.stringify(x)); </script> </body> </html>
Output
{"[object Object]":"b"}
case-2: Presence Of Map
As we know from the definition that Map is going to remember the actual insertion order of keys it displays all the key and value pair such as '{}' as a key and 'a' has a value etc. as shown in the output.
Example
<html> <body> <script> const a = {}; const b = { num:3 } const map = new Map(); map.set(a, "a").set(b, "b"); for(let[key, value] of map.entries()){ document.write(JSON.stringify(key, value)); // displaying key using Map document.write((key, value)); // displaying value using Map } </script> </body> </html>
Output
{}a {"num":3}b
- Related Articles
- What is the use of map object in javascript?
- What is the use of the map function in Python?
- What is the difference between Map and WeakMap in JavaScript?
- What is the use of OBJECT.assign() in javascript?
- 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 JavaScript cookies?
- What is the use of declaring variables in JavaScript?
- What is the use of Math.hypot() function in JavaScript?
- What is the use of _.size() method in JavaScript?
- What is the use of parsing dates in JavaScript?
- What is the use of weakSet.has() method in JavaScript?
- What is the use of Object.is() method in JavaScript?
- What is the use of proxy() object in JavaScript?

Advertisements