<!-- Please press ctrl-shift-i to see output in browser console window. -->
JavaScript
//A Map is an unordered list of key-values pairs where the key AND the value can be of any type.
var mapDemo = new Map();
mapDemo.set('name', 'Rahul'); //set function to store data as key/value pair
mapDemo.set(5, 50);
var obj = {};
mapDemo.set(obj, 'JavaScript Object'); //storing JavaScript object as a key
console.log(mapDemo.get('name')); //get function to retrieve value from collection
console.log(mapDemo.get(5));
console.log(mapDemo.get(obj)); //Passed 'obj' (above created) as a key to get value
console.log(mapDemo.has('name')); //'has' function to check if key exists or not, returns true
console.log(mapDemo.has(obj)); //returns true
console.log(mapDemo.size); //'size' property gives number of stored key/values pairs
console.log(mapDemo.delete(obj)); //'delete' function to delete data from collection using key. It returns true, if item deleted. otherwise false.
//After deleting one item, lets check if it exists or not
console.log(mapDemo.has(obj)); //returns false
console.log(mapDemo.clear()); //'clear' function removes all key/value pairs from map
console.log(mapDemo.size); //return 0
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.