Map in JS Last Updated : 11 Feb, 2025 Comments Improve Suggest changes Like Article Like Report A Map is a data structure that stores key-value pairs, where each key is unique. It is similar to an object but has some advantages:Inserts keys in the order they were added.Allows keys of any type, not just strings and symbols.Provides better performance when dealing with large datasets.Creating a MapMap() constructor allows two ways to create a Map in JavaScript.Passing an Array to new Map().Create a Map and use Map.set(). JavaScript let myMap = new Map(); let anotherMap = new Map([ ['name', 'GFG'], ['age', 30], ['city', 'Noida'] ]); console.log(anotherMap); OutputMap(3) { 'name' => 'GFG', 'age' => 30, 'city' => 'Noida' } Properties of JavaScript Mapset(key, val) : Adds or updates an element with a specified key and value.get(key) : Returns the value associated with the specified key.has(key) : Returns a boolean indicating whether an element with the specified key exists.delete(key) : Removes the element with the specified key.clear(): Removes all elements from the Map.size: Returns the number of key-value pairs in the Map. Example demonstrating all the common operations on a Map JavaScript // Create a new Map const myMap = new Map(); // 1. set(key, value) myMap.set('name', 'GFG'); myMap.set('age', 25); myMap.set(1, 'One'); console.log(myMap); // 2. get(key) console.log(myMap.get('name')); console.log(myMap.get('age')); console.log(myMap.get(1)); console.log(myMap.get('invalidKey')); // 3. has(key) console.log(myMap.has('name')); console.log(myMap.has('address')); console.log(myMap.has(1)); // 4. delete(key) myMap.delete('age'); console.log(myMap.has('age')); console.log(myMap); // 5. clear() myMap.clear(); console.log(myMap); // Output: Map {} console.log(myMap.size); myMap.set('a', 1); myMap.set('b', 2); console.log(myMap.size); console.log(myMap); OutputMap(3) { 'name' => 'GFG', 'age' => 25, 1 => 'One' } GFG 25 One undefined true false true false Map(2) { 'name' => 'GFG', 1 => 'One' } Map(0) {} 0 2 Map(2) { 'a' => 1, 'b' => 2 } Advantages of Using Maps:Key order: Maps remember the insertion order of the keys.Performance: Inserting and retrieving elements from a Map are generally faster than objects, especially when the number of elements is large.Any type of key: Unlike objects, Map keys can be of any data type, including functions, objects, and primitive types.DSA Problems On MapMost Frequent ElementCount distinct elements in every window of size KCheck if two arrays are equal or not2 Sum – Count Pairs with target sumCount all pairs with absolute difference equal to KCheck If Array Pair Sums Divisible by KMax distance between two occurrences in arraySubarray with Given Sum – Handles Negative NumbersRemove minimum elements such that no common elements exist in two arrays3 Sum – Count all triplets with target sumLongest Subarray with Sum Divisible by KLongest Subarray having Majority Elements Greater than K Map in JavaScript Visit Course Map in JavaScript Map in JS Comment More info S Sumit Ghosh Follow Improve Article Tags : Misc JavaScript Web Technologies javascript-map JavaScript-ES +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like