{"id":2457,"date":"2026-11-12T06:14:26","date_gmt":"2026-11-12T06:14:26","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=2457"},"modified":"2026-11-12T06:14:26","modified_gmt":"2026-11-12T06:14:26","slug":"javascript-map-keys","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/javascript-map-keys\/","title":{"rendered":"JavaScript Map keys() Method with Examples"},"content":{"rendered":"<p>Hello friends! When you store data in a JavaScript <code>Map<\/code>, sooner or later you will want a list of just the keys. That is exactly what <code>keys()<\/code> is for. The <code>keys()<\/code> method of a JavaScript <code>Map<\/code> returns an <strong>iterator<\/strong> over all the keys in the map, in the order they were inserted. It takes no arguments and does not change the map.<\/p>\n<pre class=\"wp-block-code\"><code>map.keys()<\/code><\/pre>\n<p>The important word is <em>iterator<\/em>. <code>keys()<\/code> does not return an array, so you cannot call <code>.length<\/code> or <code>.filter()<\/code> on it directly. You loop over it with <code>for...of<\/code>, or convert it to an array first. Let&#8217;s understand both with simple examples.<\/p>\n<h2>Basic Example<\/h2>\n<pre class=\"wp-block-code\"><code>const marks = new Map([[\"Riya\", 91], [\"Aman\", 78], [\"Kabir\", 85]]);\n\nconst keyIter = marks.keys();\nconsole.log(keyIter.next().value);\nconsole.log(keyIter.next().value);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Riya\nAman<\/code><\/pre>\n<p>Each call to <code>next()<\/code> moves one step forward and returns an object like <code>{ value: \"Riya\", done: false }<\/code>. When there are no keys left, <code>done<\/code> becomes <code>true<\/code>.<\/p>\n<h2>Looping Over Keys with for&#8230;of<\/h2>\n<p>Calling <code>next()<\/code> by hand is good for understanding, but in real code you will rarely do it. The most common way to use <code>keys()<\/code> is a <code>for...of<\/code> loop:<\/p>\n<pre class=\"wp-block-code\"><code>for (const name of marks.keys()) {\n  console.log(name);\n}<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>Riya\nAman\nKabir<\/code><\/pre>\n<h2>Converting Keys to an Array<\/h2>\n<p>Use the spread operator or <code>Array.from()<\/code> when you need array methods:<\/p>\n<pre class=\"wp-block-code\"><code>console.log([...marks.keys()]);\nconsole.log(Array.from(marks.keys()));\n\nconst toppers = [...marks.keys()].filter(n =&gt; marks.get(n) &gt; 80);\nconsole.log(toppers);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ 'Riya', 'Aman', 'Kabir' ]\n[ 'Riya', 'Aman', 'Kabir' ]\n[ 'Riya', 'Kabir' ]<\/code><\/pre>\n<h2>Keys Keep Their Insertion Order<\/h2>\n<p>Now here is something that makes Maps really handy. A Map remembers the order in which keys were added, whatever their type:<\/p>\n<pre class=\"wp-block-code\"><code>const m2 = new Map();\nm2.set(\"z\", 1);\nm2.set(\"a\", 2);\nm2.set(10, 3);\nconsole.log([...m2.keys()]);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ 'z', 'a', 10 ]<\/code><\/pre>\n<p>Compare this with a plain object, where number-like keys are moved to the front and every key becomes a string:<\/p>\n<pre class=\"wp-block-code\"><code>const obj = { 2: \"b\", 1: \"a\", x: \"c\" };\nconsole.log(Object.keys(obj));<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ '1', '2', 'x' ]<\/code><\/pre>\n<h2>Keys Can Be Any Type<\/h2>\n<p>Unlike object keys, Map keys keep their real type. The number <code>1<\/code> and the string <code>\"1\"<\/code> are two different keys:<\/p>\n<pre class=\"wp-block-code\"><code>const objKey = { id: 1 };\nconst m3 = new Map([[1, \"number\"], [\"1\", \"string\"], [true, \"boolean\"], [objKey, \"object\"]]);\nfor (const k of m3.keys()) console.log(typeof k, k);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>number 1\nstring 1\nboolean true\nobject { id: 1 }<\/code><\/pre>\n<h2>The Iterator Is &#8220;Live&#8221;<\/h2>\n<p>If you add keys to the map before you finish reading the iterator, the new keys show up too:<\/p>\n<pre class=\"wp-block-code\"><code>const m4 = new Map([[\"a\", 1]]);\nconst it = m4.keys();\nm4.set(\"b\", 2);\nconsole.log([...it]);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ 'a', 'b' ]<\/code><\/pre>\n<p>If you need a fixed snapshot, convert to an array first with <code>[...map.keys()]<\/code>.<\/p>\n<h2>keys() vs values() vs entries()<\/h2>\n<p><code>keys()<\/code> has two close cousins, and it helps to see all three side by side:<\/p>\n<pre class=\"wp-block-code\"><code>console.log([...marks.values()]);\nconsole.log([...marks.entries()]);\nconsole.log(marks.size);<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>[ 91, 78, 85 ]\n[ [ 'Riya', 91 ], [ 'Aman', 78 ], [ 'Kabir', 85 ] ]\n3<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>Method<\/th>\n<th>Gives you<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>keys()<\/code><\/td>\n<td>only the keys<\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/gyanipandit.com\/programming\/javascript-map-values\/\"><code>values()<\/code><\/a><\/td>\n<td>only the values<\/td>\n<\/tr>\n<tr>\n<td><code>entries()<\/code><\/td>\n<td><code>[key, value]<\/code> pairs (also what <code>for...of<\/code> on the map itself gives)<\/td>\n<\/tr>\n<tr>\n<td><code>size<\/code><\/td>\n<td>the number of entries (a property, not a method)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Common Mistakes<\/h2>\n<p>Beginners often trip over these three things, so keep them in mind:<\/p>\n<ul>\n<li><strong>Treating the result as an array.<\/strong> <code>marks.keys().length<\/code> is <code>undefined<\/code>. Use <code>marks.size<\/code>, or <code>[...marks.keys()].length<\/code>.<\/li>\n<li><strong>Reusing a finished iterator.<\/strong> Once you have looped through an iterator, it is empty. Call <code>marks.keys()<\/code> again to start over.<\/li>\n<li><strong>Using <code>Object.keys(map)<\/code>.<\/strong> That returns <code>[]<\/code> for a Map, because Map entries are not object properties.<\/li>\n<\/ul>\n<p>So friends, to sum up: <code>map.keys()<\/code> gives you every key in insertion order as an iterator. Loop over it with <code>for...of<\/code>, and when you need array methods, just spread it into an array with <code>[...map.keys()]<\/code>. Try the examples above in your browser console, and you will get comfortable with it in a few minutes.<\/p>\n<p>Related: <a href=\"https:\/\/gyanipandit.com\/programming\/map-in-javascript\/\">Map in JavaScript<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-map-has\/\">Map has()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/javascript-map-get\/\">Map get()<\/a> \u00b7 <a href=\"https:\/\/gyanipandit.com\/programming\/for-of-loop-javascript\/\">for&#8230;of loop<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends! When you store data in a JavaScript Map, sooner or later you will want a list of just the keys. That is exactly what keys() is for. The keys() method of a JavaScript Map returns an iterator over all the keys in the map, in the order they were inserted. It takes no &#8230; <a title=\"JavaScript Map keys() Method with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/javascript-map-keys\/\" aria-label=\"Read more about JavaScript Map keys() Method with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-2457","post","type-post","status-publish","format-standard","hentry","category-javascript-tutorial","tag-javascript","tag-javascript-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=2457"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2457\/revisions"}],"predecessor-version":[{"id":6132,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/2457\/revisions\/6132"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=2457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=2457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=2457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}