1console.log("hello-world);
javascript
console.log("hello-world);
hidden codeblock for CSS
css
.container {
  width: 80%;
}
hidden codeblock for CSS
html
<pre><code class="language-css">
.container {
  width: 80%;
}
</code></pre>
hidden codeblock for CSS
1console.log("hello-world);
javascript
console.log("hello-world);
hidden codeblock for CSS
css
.container {
  width: 80%;
}
hidden codeblock for CSS
html
<pre><code class="language-css">
.container {
  width: 80%;
}
</code></pre>
hidden codeblock for CSS

Checking If A Map Or Set Is Empty In JavaScript

ReadTime: 4 minutes

When working with JavaScript Map and Set objects, it's common to need to check if they are empty. Lets explore different ways to achieve this in JS.


Using the Size Property

Both Map and Set objects have a size property, which represents the number of elements in the collection. Checking if this property is equal to zero is a straightforward way to determine if the Map or Set is empty.

javascript
// Map

const emptyMap = new Map();
const nonEmptyMap = new Map([[1, 'one'], [2, 'two']]);

console.log(emptyMap.size === 0);       // Output: true
console.log(nonEmptyMap.size === 0);    // Output: false

// Set
const emptySet = new Set();
const nonEmptySet = new Set([1, 2, 3]);

console.log(emptySet.size === 0);       // Output: true
console.log(nonEmptySet.size === 0);    // Output: false
Solution Code

Using the forEach Method

Another approach is to use the forEach method to iterate through the elements. If the callback function is never called, it means the collection is empty. Weird, but yeah, can do.

javascript
function isEmpty(collection) {
  let empty = true;
  collection.forEach(() => (empty = false));
  return empty;
}

const emptyMap = new Map();
const nonEmptyMap = new Map([[1, 'one'], [2, 'two']]);

console.log(isEmpty(emptyMap));       // Output: true
console.log(isEmpty(nonEmptyMap));    // Output: false

const emptySet = new Set();
const nonEmptySet = new Set([1, 2, 3]);

console.log(isEmpty(emptySet));       // Output: true
console.log(isEmpty(nonEmptySet));    // Output: false
forEach to check empty set or map

🧪Practice Coding Problem: Task and Feedback Manager

In the spirit of Test Driven Development ( 😁), lets test our understanding by solving a problem.

You are building a small application to manage tasks and user feedback. Using JavaScript’s Map and Set, tasks are stored in a Map, and feedback is tracked with a Set.

Write a function hasFeedback that takes the tasks (Map) and feedback (Set) as parameters, along with a task ID. The function should return true if the specified task has received feedback and false otherwise.

Problem (JavaScript)

javascript
function hasFeedback(tasks, feedback, taskId) {
  // > > > 👉 Write code here 👈 < < <
}

const tasks = new Map([
  [1, { description: 'Complete assignment' }],
  [2, { description: 'Prepare presentation' }],
]);

const feedback = new Set([1]);

console.log(hasFeedback(tasks, feedback, 1)); // Output: true
console.log(hasFeedback(tasks, feedback, 2)); // Output: false
Problem Code

Constraints:

  • Task IDs are positive integers.
  • The tasks Map will have at most 100 entries.
  • The feedback Set will have at most 100 entries.

Please attempt before seeing the Answer:

Solution
javascript
function hasFeedback(tasks, feedback, taskId) {
  // Check if the task ID exists in the tasks Map
  if (tasks.has(taskId)) {
      // Check if the task ID is in the feedback Set
      return feedback.has(taskId);
  } else {
      // Task with the specified ID doesn't exist
      return false;
  }
}

// Example Usage:
const tasks = new Map([
  [1, { description: 'Complete assignment' }],
  [2, { description: 'Prepare presentation' }],
]);

const feedback = new Set([1]);

console.log(hasFeedback(tasks, feedback, 1)); // Output: true
console.log(hasFeedback(tasks, feedback, 2)); // Output: false
Solution Code

This solution defines a function hasFeedback that takes a tasks Map, a feedback Set, and a taskId as parameters. It checks if the task ID exists in the tasks Map and then checks if the task ID is in the feedback Set. The function returns true if the task has feedback and false otherwise.


OK, so if you are checking if a collection is empty, then just use the size property or loop through elements. The only thing you can’t check is if the coffee pot in the office is empty—no JS magic for that! ☕ But I digressed, you keep coding🚀!

Contact

Feel free to reach out!