From d6bd7c70ea5e1ec355f7135e12658e5f721c6378 Mon Sep 17 00:00:00 2001 From: Stuart Mosquera Date: Thu, 9 Oct 2025 13:47:57 -0300 Subject: [PATCH 1/3] create keys.md --- .../concepts/map/terms/keys/keys.md | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 content/javascript/concepts/map/terms/keys/keys.md diff --git a/content/javascript/concepts/map/terms/keys/keys.md b/content/javascript/concepts/map/terms/keys/keys.md new file mode 100644 index 00000000000..ad56b46022b --- /dev/null +++ b/content/javascript/concepts/map/terms/keys/keys.md @@ -0,0 +1,101 @@ +--- +Title: '.keys()' +Description: 'Returns a new map iterator object that contains the keys of each element in the map.' +Subjects: + - 'Computer Science' + - 'Web Development' +Tags: + - 'Map' + - 'Object' +CatalogContent: + - 'introduction-to-javascript' + - 'paths/front-end-engineer-career-path' +--- + +The **`.keys()`** method returns a new map [iterator](https://www.codecademy.com/resources/docs/javascript/iterators) object containing the keys of each element in the map, respecting the insertion order. This method enables explicit iteration using a `for...of` loop or the iterator's `.next()` method. + +> **Note:** The iterable returned by `.keys()` is _not reusable_. Once it has been fully consumed (i.e., all elements have been iterated over), it becomes exhausted. To iterate again, a new iterator must be created by calling `.keys()` again on the map. + +## Syntax + +```javascript +map.keys(); +``` + +**Parameters:** + +The `.keys()` method does not take any parameters. + +**Return value:** + +Returns a new map iterator object containing the keys of each element in the map, in order. + +## Example 1: Printing Each Key + +This example uses a `for...of` loop to iterate over the iterable object returned by `.keys()` and prints the keys of the `ratings` map: + +```javascript +const ratings = new Map(); + +ratings.set(1, 'Bad'); +ratings.set(5, 'Medium'); +ratings.set(10, 'Excellent'); + +const iterator = ratings.keys(); + +for (const key of iterator) { + console.log(key); +} +``` + +The code will produce this output: + +```shell +1 +5 +10 +``` + +## Example 2: Getting the Values + +In this example, the keys are saved in the `numsKeys` array, and the map's [`.get()`](https://www.codecademy.com/resources/docs/javascript/map/get) method is used to retrieve and print each value. + +```javascript +const nums = new Map([ + [2, 'Two'], + [4, 'Four'], + [6, 'Six'], + [8, 'Eight'], +]); + +const numsKeys = Array.from(nums.keys()); + +numsKeys.forEach((key) => console.log(nums.get(key))); +``` + +The code will produce this output: + +```shell +Two +Four +Six +Eight +``` + +## Codebyte Example + +This example uses the `.next()` method to manually iterate through the keys ​​and print them to the console: + +```codebyte/js +const map = new Map(); + +map.set(1, 'One'); +map.set(true, 'True'); +map.set({ id: 1 }, 'Object'); + +const iterator = map.keys(); + +console.log(iterator.next().value); +console.log(iterator.next().value); +console.log(iterator.next().value); +``` From 1248d2d78b4b8853c3d720d4d94e9de56e868851 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 10 Oct 2025 16:07:07 +0530 Subject: [PATCH 2/3] minor fixes --- content/javascript/concepts/map/terms/keys/keys.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/javascript/concepts/map/terms/keys/keys.md b/content/javascript/concepts/map/terms/keys/keys.md index ad56b46022b..553debe6bcc 100644 --- a/content/javascript/concepts/map/terms/keys/keys.md +++ b/content/javascript/concepts/map/terms/keys/keys.md @@ -18,7 +18,7 @@ The **`.keys()`** method returns a new map [iterator](https://www.codecademy.com ## Syntax -```javascript +```pseudo map.keys(); ``` @@ -34,7 +34,7 @@ Returns a new map iterator object containing the keys of each element in the map This example uses a `for...of` loop to iterate over the iterable object returned by `.keys()` and prints the keys of the `ratings` map: -```javascript +```js const ratings = new Map(); ratings.set(1, 'Bad'); @@ -58,9 +58,9 @@ The code will produce this output: ## Example 2: Getting the Values -In this example, the keys are saved in the `numsKeys` array, and the map's [`.get()`](https://www.codecademy.com/resources/docs/javascript/map/get) method is used to retrieve and print each value. +In this example, the keys are saved in the `numsKeys` array, and the map's [`.get()`](https://www.codecademy.com/resources/docs/javascript/map/get) method is used to retrieve and print each value: -```javascript +```js const nums = new Map([ [2, 'Two'], [4, 'Four'], From 062905a85f9445028f3f41f678ec7a97b58f5b19 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Mon, 20 Oct 2025 14:19:54 +0530 Subject: [PATCH 3/3] Update content/javascript/concepts/map/terms/keys/keys.md --- content/javascript/concepts/map/terms/keys/keys.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/javascript/concepts/map/terms/keys/keys.md b/content/javascript/concepts/map/terms/keys/keys.md index 553debe6bcc..82c3fc63c3d 100644 --- a/content/javascript/concepts/map/terms/keys/keys.md +++ b/content/javascript/concepts/map/terms/keys/keys.md @@ -86,7 +86,7 @@ Eight This example uses the `.next()` method to manually iterate through the keys ​​and print them to the console: -```codebyte/js +```codebyte/javascript const map = new Map(); map.set(1, 'One');