From 40e564f35b1465f1aeb61b41951504d40f5d97f2 Mon Sep 17 00:00:00 2001 From: Anudhyan Datta <127120520+Anudhyan@users.noreply.github.com> Date: Thu, 2 Oct 2025 15:54:25 +0530 Subject: [PATCH 1/2] Add documentation for Map.clear() method Added documentation for the Map.clear() method, including syntax, examples, and a Codebyte example demonstrating its usage. --- content/javascript/concepts/map/clear.md | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 content/javascript/concepts/map/clear.md diff --git a/content/javascript/concepts/map/clear.md b/content/javascript/concepts/map/clear.md new file mode 100644 index 00000000000..955128ff136 --- /dev/null +++ b/content/javascript/concepts/map/clear.md @@ -0,0 +1,53 @@ +--- +Title: "Map.clear()" +Description: "Removes all key-value pairs from a Map object." +Tags: ["JavaScript", "Map", "clear"] +--- + +## Overview + +The `Map.clear()` method removes all key-value pairs from a `Map` object. +After calling `clear()`, the map will be empty. + +## Syntax + +```javascript +map.clear() +``` +## Example + +To demonstrate `Map.clear()`, first create a `Map` object with some key-value pairs: +```javascript +const myMap = new Map(); +myMap.set('a', 1); +myMap.set('b', 2); +console.log(myMap.size); +``` +The output will be +```pseudo +2 +``` +## Codebyte Example + +In the example below, a `fruitInventory` map tracks the quantity of different fruits in stock. By the end of the program: +- The inventory is checked before clearing with the [`.size` property](https://www.codecademy.com/resources/docs/javascript/map/size). +- All entries are removed using the `.clear()` method. +- The inventory is verified to be empty after clearing. +```codebyte/js +const fruitInventory = new Map([ + ['apples', 50], + ['bananas', 30], + ['oranges', 25] +]); + +console.log('Initial inventory size:', fruitInventory.size); +console.log('Apples in stock:', fruitInventory.get('apples')); + +// Clear all inventory +fruitInventory.clear(); + +console.log('Inventory after clearing:', fruitInventory.size); +console.log('Apples in stock after clear:', fruitInventory.get('apples')); + +``` + From 5145b6b16eaeacbe06ee896a45024e7a3d25244b Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 3 Oct 2025 20:04:24 +0530 Subject: [PATCH 2/2] content fixes --- content/javascript/concepts/map/clear.md | 61 ++++++++++++++++-------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/content/javascript/concepts/map/clear.md b/content/javascript/concepts/map/clear.md index 955128ff136..72b376a8074 100644 --- a/content/javascript/concepts/map/clear.md +++ b/content/javascript/concepts/map/clear.md @@ -1,39 +1,64 @@ --- -Title: "Map.clear()" -Description: "Removes all key-value pairs from a Map object." -Tags: ["JavaScript", "Map", "clear"] +Title: '.clear()' +Description: 'Removes all key-value pairs from a Map object.' +Subjects: + - 'Computer Science' + - 'Web Development' +Tags: + - 'JavaScript' + - 'Map' + - 'Methods' + - 'Objects' +CatalogContent: + - 'introduction-to-javascript' + - 'paths/front-end-engineer-career-path' --- -## Overview - -The `Map.clear()` method removes all key-value pairs from a `Map` object. -After calling `clear()`, the map will be empty. +The **`Map.clear()`** method in JavaScript removes all entries (key-value pairs) from a `Map` object. After invoking it, the map becomes empty and its [`.size`](https://www.codecademy.com/resources/docs/javascript/map/size) property becomes 0. ## Syntax -```javascript +```pseudo map.clear() ``` + +**Parameters:** + +It does not take any parameters. + +**Return value:** + +The method returns `undefined` after clearing the map. + ## Example -To demonstrate `Map.clear()`, first create a `Map` object with some key-value pairs: -```javascript +In this example, a map is created with two entries, then cleared, and the size and values are checked: + +```js const myMap = new Map(); myMap.set('a', 1); myMap.set('b', 2); console.log(myMap.size); + +myMap.clear(); + +console.log(myMap.size); +console.log(myMap.get('a')); ``` -The output will be -```pseudo + +The output of this code will be: + +```shell 2 +0 +undefined ``` + ## Codebyte Example -In the example below, a `fruitInventory` map tracks the quantity of different fruits in stock. By the end of the program: -- The inventory is checked before clearing with the [`.size` property](https://www.codecademy.com/resources/docs/javascript/map/size). -- All entries are removed using the `.clear()` method. -- The inventory is verified to be empty after clearing. -```codebyte/js +In this example, a fruit inventory map is initialized with values, then cleared to show the map becomes empty: + +```codebyte/javascript const fruitInventory = new Map([ ['apples', 50], ['bananas', 30], @@ -48,6 +73,4 @@ fruitInventory.clear(); console.log('Inventory after clearing:', fruitInventory.size); console.log('Apples in stock after clear:', fruitInventory.get('apples')); - ``` -