Skip to content
JS Fundamentals: Objects, Arrays, and Functions
Share
Explore
Objects & Arrays

icon picker
Bonus: Map and Set

Map

A Map object is really similar to an object except that its keys can be any type, while an object only has strings as keys. It also has a nice API for working with them.

Set

A Set object is a collection of unique values. The API is really similar to the Map
Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection.

Example of an Object vs Array vs a Map vs a Set :


// Instantiating an instance
const myObj = {}; // object literal
const myArr = [];
const myMap = new Map(); // w/ keyword new
const mySet = new Set();

// Setting a key-value pair
myObj.key = 'value';
myObj['key'] = 'value';

myArr.key = 'value';
myArr['key'] = 'value';

myMap.set('key', 'value');
mySet.add('value');

// Check if it has a key
if (myObj.key) { console.log(true); }
myMap.has('key');
mySet.has('value');

// Getting a value
myObj.key;
myArr.includes('value');
myMap.get('key');
// Sets don't have keys, just values

// Deleting
delete myObj.key;
myMap.delete('key');
mySet.delete('value');
Read more in the docs:

Next 👉


©2021 for

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.