Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Map entries() Method
Next article icon

JavaScript Map delete() Method

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaScript Map.delete() method is used to delete the specified element among all the elements that are present in the map.

The Map.delete() method takes the key that needs to be removed from the map, thus removing the element associated with that key and returning true. If the key is not present then it returns false. 

Syntax:

my_map.delete(key);

Parameters:

  • key: The element associated with this key is to be removed from the map

Return value:

  • The Map. delete() method returns true if the key is present whose associated element is to be removed which is passed as an argument, otherwise returns false.

Example 1: The key '3' is present in the map hence element associated with it is removed and returns true.

javascript
// creating a map object let my_map = new Map();  // Adding [key, value] pair to the map my_map.set(1, 'first'); my_map.set(2, 'second'); my_map.set(3, 'third'); my_map.set(4, 'fourth');  // will display true as key '3' // is present and its associated // element is removed as well console.log(my_map.delete(3));   // elements left in the map after deletion console.log("key-value pair of the map",     " after deletion-");  my_map.forEach(function (item, key, mapObj) {     console.log(key.toString(), ":",         " ", item.toString()); }); 

Output:

true
key-value pair of the map after deletion-
1: first
2: second
4: fourth

Example 2: The key '5' is not present in the map hence it returns false. 

javascript
// creating a map object let my_map = new Map();  // Adding [key, value] pair to the map my_map.set(1, 'first'); my_map.set(2, 'second'); my_map.set(3, 'third'); my_map.set(4, 'fourth');  // will display false as key '5' // is not present and its associated // element is removed as well console.log(my_map.delete(5))  // elements left in the map after deletion console.log("key-value pair of the map",     " after deletion-");  my_map.forEach(function (item, key, mapObj) {     console.log(key.toString(), ":", " ",         item.toString()); }); 

Output:

false
key-value pair of the map after deletion-
1: first
2: second
3: third
4: fourth

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • safari

Next Article
JavaScript Map entries() Method

A

akash1295
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-map
  • JavaScript-Methods

Similar Reads

    JavaScript Map() Constructor
    The Map() constructor is used to create Map objects in JavaScript. The map is a data structure that stores elements as a key, value pair.Syntax:new Map()new Map(iterable)Parameters:iterable: An iterable object used for iterating through elements, stored as a key, value pair.Return value:A new Map ob
    3 min read
    JavaScript Map constructor Property
    The map constructor property in JavaScript is used to return the map constructor function for the object. The function which is returned by this property is just the reference to this function, not a map containing the function's name. The JavaScript map constructor, string constructor, and boolean
    1 min read
    JavaScript Map size Property
    Map is a collection of elements where each element is stored as a Key, value pair. The Map.size property returns the number of key, and value pairs stored in a map.SyntaxmapName.sizeReturn ValueAn integer representing the number of key, value pairs stored in a map.The below examples illustrate the M
    1 min read
    JavaScript Map clear() Method
    JavaScript Map.clear() method is used for the removal of all the elements from a map and making it empty. It removes all the [key, value] from the map. No arguments are required to be sent as parameters to the Map.clear() method and it returns an undefined return value.Syntax:mapObj.clear()Parameter
    3 min read
    JavaScript Map delete() Method
    JavaScript Map.delete() method is used to delete the specified element among all the elements that are present in the map. The Map.delete() method takes the key that needs to be removed from the map, thus removing the element associated with that key and returning true. If the key is not present the
    3 min read
    JavaScript Map entries() Method
    JavaScript Map.entries() method is used for returning an iterator object which contains all the [key, value] pairs of each element of the map. It returns the [key, value] pairs of all the elements of a map in the order of their insertion. The Map.entries() method does not require any argument to be
    4 min read
    JavaScript Map forEach() Method
    JavaScript Map.forEach method is used to loop over the map with the given function and executes the given function over each key-value pair.Syntax:myMap.forEach(callback, value, key, thisArg)Parameters:This method accepts four parameters as mentioned above and described below:callback: This is the f
    3 min read
    JavaScript Map get() Method
    The Map.get() method in JavaScript is a convenient way to retrieve the value associated with a specific key in a Map object. A Map in JavaScript allows you to store key-value pairs where keys can be of any data type, making it more useful compared to objects, which only allow strings and symbols as
    3 min read
    JavaScript Map has() Method
    The has() method in JavaScript Map returns a boolean indicating whether a specified element is present in the Map object or not. It returns true if the element is found, otherwise false.Syntax:mapObj.has(key)Parameters:key: It is the key of the element of the map which has to be searched.Return Valu
    3 min read
    JavaScript Map keys() Method
    The Map.keys() method is used to extract the keys from a given map object and return the iterator object of keys. The keys are returned in the order they were inserted.Syntax:Map.keys()Parameters:This method does not accept any parameters.Return Value:This returns the iterator object that contains k
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences