JavaScript .size

karel.de.smetoutlook.com's avatar
Published Dec 31, 2021Updated Jun 26, 2023
Contribute to Docs

The .size property returns the number of entries inside of a Map object.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

map.size;

Since .size is a property of Map, no parentheses are needed.

Example

Assuming there is a map of fruits, the number of keys can be checked with the .size property:

const fruits = new Map([
['Apples', 5],
['Oranges', 8],
]);
console.log(fruits.size); // Output: 2

Codebyte Example

The following example uses the size property to confirm the number of elements in a map after deleting a key-value pair.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours