DevLog.at

Fun with JavaScript

Today, I discovered Maps in JS.

Not to be confused with the good ol JS Object {}, Maps provide a way to enumerate in the order of insertion key value pairs.

See below!

const map = new Map();

map.set('cat', 'tofu');

map.set('dog', 'kobe');

console.log(map); //{'cat'-> 'tofu', 'dog' -> 'kobe'}

Objects have all their keys as strings, but Maps don't have the same restriction. So adding different types as keys can be useful in certain cases.

map.set(1, 'number key!');

console.log(map); //{'cat'-> 'tofu', 'dog' -> 'kobe', 1 -> 'number key!'}

2m

Some other functions you can call on your Map include:

map.set('cat','tofu');

map.has('cat'); //true

map.get('cat'); //tofu

map.delete('dog');

map.has('dog'); //false

2m

Welcome to DevLog

The open thought platform for developers.

Share your work as you work on it. Easier than a blog, handier than Twitter.

Sign in and write down valuable thoughts that would otherwise be forgotten.