codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › JS maps

JS maps

Key/value pairs with any key type.

A Map keeps insertion order and allows objects as keys. Use set, get, has and iterate with for...of.

Example code

<pre id='o'></pre>
<script>
const m=new Map([['apple',3]]);m.set('pear',5);
let t='';for(const [k,v] of m)t+=k+' = '+v+'\n';
o.textContent=t+'size '+m.size;
</script>

Run this code in the online editor

← JS setsJS iterations →