• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Get the Size of a Set in JavaScript

Get the Size of a Set in JavaScript

June 21, 2022 Leave a Comment

We can use JavaScript to get the set size by simply using the size property of the set. The size property will simply return the number of items in the set.

var someSet = new Set(["this","is","a","set","of","size","7"]);
var length_Of_Set = someSet.size;

console.log(length_Of_Set);

#Output
7

In JavaScript, it can be useful to be able to easily calculate the size of a set.

To get the size of a set in JavaScript, we can use the size property.

Let’s use our code above and create a simple function that takes a set, and returns the size of the set.

function getSetSize(someSet){
  return someSet.size;
};

And now let’s just see some examples of using this function to get the size of some sets.

Remember that sets differ from arrays in that they can only contain unique values.

function getSetSize(someSet){
  return someSet.size;
};

var someSet1 = new Set(["this","is","a","set"]);
var someSet2 = new Set(["4","5","4","5"]);
var someSet3 = new Set(["red","green","blue","red","yellow","red","orange","red"]);
var someSet4 = new Set(["false","","null","undefined",null,0,"4"]);

console.log(getSetSize(someSet1));
console.log(getSetSize(someSet2));
console.log(getSetSize(someSet3));
console.log(getSetSize(someSet4));

#Output
4
2
5
7

Hopefully this article has been useful for you to learn how to use JavaScript to get set size.

Other Articles You'll Also Like:

  • 1.  JavaScript Trunc with Math.trunc() Method
  • 2.  Using JavaScript to Get Date Format in yyyy-mm-dd hh mm ss
  • 3.  Using JavaScript to Get a Random Number Between Range of Numbers
  • 4.  JavaScript substring – How to Get a Substring From a String
  • 5.  Creating a JavaScript Function to Subtract Two Numbers
  • 6.  Convert String to Array in JavaScript
  • 7.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript
  • 8.  Using JavaScript to Redirect After 5 Seconds
  • 9.  Using JavaScript to get Textarea Value
  • 10.  JavaScript onkeydown – How to Use onkeydown Event with JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy