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!'}


Some other functions you can call on your Map include:


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

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


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


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.