GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Hashing
  • set(key, val)

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Map in JS - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

Map in JS

Last Updated : 1 Jul, 2026

A JavaScript Map holds key-value pairs and similar to Hash Map or Dictionary in other languages. Preserves the original insertion order. Supports any type, including objects and primitives, as keys or values. This feature allows for efficient data retrieval and manipulation.

  • Internally use Hashing that makes time complexities of operations like search, insert and delete constant or O(1) on average.
  • Only Unique Keys are allowed, if we insert the same key with a different value, it overwrites the previous one.
  • It is always recommended to use Maps over Objects when we have frequent insert and delete operations.
javascript_map_initialization_logging
JavaScript
let myMap = new Map();
let anotherMap = new Map([
    ['name', 'GFG'],
    ['age', 30],
    ['city', 'Noida']
]);
console.log(anotherMap);

Output
Map(3) { 'name' => 'GFG', 'age' => 30, 'city' => 'Noida' }

Creating a Map

Map() constructor allows two ways to create a Map in JavaScript.

  • Passing an Array to new Map().
  • Create a Map and use Map.set().

Properties of JavaScript Map

  • set(key, val) : set(key, val) adds or updates an element with a specified key and value.
  • get(key) : get(key) returns the value associated with the specified key.
  • has(key) : has(key) returns a boolean indicating whether an element with the specified key exists.
  • delete(key) : delete(key) removes the element with the specified key.
  • clear() : clear() removes all elements from the Map.
  • size : size returns the number of key-value pairs in the 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);

Output
Map(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.

Also Check:

DSA Problems On Map

  • Most Frequent Element
  • Count distinct elements in every window of size K
  • Check if two arrays are equal or not
  • 2 Sum – Count Pairs with target sum
  • Count all pairs with absolute difference equal to K
  • Check If Array Pair Sums Divisible by K
  • Max distance between two occurrences in array
  • Subarray with Given Sum – Handles Negative Numbers
  • Remove minimum elements such that no common elements exist in two arrays
  • 3 Sum – Count all triplets with target sum
  • Longest Subarray with Sum Divisible by K
  • Longest Subarray having Majority Elements Greater than K
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.