Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Map Example
- const a = new Map();
- const b = {};
- //Adding key-value pairs
- console.log("Set Key");
- //Map
- a.set('test', 3);
- console.log(...a.entries()); //[ 'test', 3 ]
- //Object
- b['test'] = 3;
- console.log(b); //{ test: 3 }
- //Get key
- console.log("\nGet Key");
- //Map
- const c = a.get('test');
- console.log("Map:", c);//Map: 3
- //Object
- const d = b['test'];
- console.log("Object:", d); //Object: 3
- //Has key
- console.log("\nHas Test");
- //Map
- console.log("Map:", a.has('test')); //true
- //Object
- console.log("Object:", b['test'] !== undefined); //true (But could be set to null or undefined by developer)
- //Remove key
- console.log("\nDelete Key");
- //Map
- console.log("Map:", a.delete('test')); //true if there is a key to remove
- console.log("Map:", a.get('test')); //Map: undefined
- //Object
- delete b['test'];
- console.log("Object:",b['test']); //Object: undefined
Add Comment
Please, Sign In to add comment