So please forgive me if this question is super stupid, but can I think of the “size” in JS as like the length property, but for Sets?
Hey there, i have not, until now, ever heard of the .size() method. So i looked it up and Your question isnt stupid coz you are right, It is like the length method however the docs insist that it returns the number of unique elements in a set object. therefore the value might be lower than the actual number of sets. You can refer to the link below for more.
First of all, they’re properties, not methods - they are not called like a function.
I’m not sure what is meant by “returns the number of unique elements in a set object.” By definition, there are no non-unique elements in a set.
const set = new Set([1, 2, 3]);
console.log(...set)
// 1 2 3
console.log(set.size)
// 3
set.add(4);
console.log(...set)
// 1 2 3 4
console.log(set.size)
// 4
set.add(1);
console.log(...set)
// 1 2 3 4
console.log(set.size)
// 4
But yes, size and length are “mostly” equivalent properties. But there are some slight differences. Of course arrays can have duplicate elements and empty slots, which sets cannot. And sets are a more “managed” experience (I guess a little more OOP-ish) than arrays. One oddity is that you can manipulate length directly:
const arr = [1, 2, 3];
console.log(arr.length);
// 3
arr.length = 10;
console.log(arr.length);
// 10
console.log(arr);
// [1, 2, 3, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
const set = new Set([1, 2, 3]);
console.log(set.size);
// 3
set.size = 10;
console.log(set.size);
// 3
console.log(...set);
// 1 2 3
The info i displayed was from the mozila docs, kindly take a look at what i mean by unique elements in a set i.e elements that are not repeated, you however explain it rather well
Thanks I’ll change my question to refer to them as properties. I appreciate the reply.
Thanks I appreciate it, I think I’ve been slapped hard when I asked my first question on slackoverflow. They were brutal on there. I’m new and learning, so I’m trying my best to ask questions properly.
Don’t worry about that, i am a learner too and i have realised the best step is ask and also try and figure it out on your own until an answer comes along but most of the time, you have the power. I haven’t tried stackoverflow though but i know how it feels to be looked down upon
Yes, I wasn’t criticizing you, but the docs. I understand that you were paraphrasing them - I was trying to understand them.
From the docs, the definition of Set “lets you store unique values of any type”. So, it is redundant for the docs to say that size gives “the number of (unique) elements”. Perhaps the parentheses are a nod to this, but I think it is redundant. Does that mean that there are non-unique elements?
It would be like if someone said to me, “My wallet only contains Canadian dollars. Now ask me how many Canadian dollars are in my wallet.” That would be confusing to me as the specificity of the second sentence seems to imply the possibility that there are other kinds of dollars. It would be like saying, “I’ve never robbed a bank on a Monday, Tuesday, or Wednesday”. Uhhhh… Well, why so specific if it’s a general denial?
In short, to talk about “unique” elements in a Set is redundant. The docs are not perfect. As a former proofreader, those kind of things bother me.
I didn’t mean that you had asked the question incorrectly or anything. I was just clarifying some terminology.
Its okay, on the issue of unique, i believe something that is equal or similar to something else isn’t unique, right? And it seems from the docs the repeated values are counted as one unique value, does this present the working of size on a set or its lacking?
None taken, I appreciate the time you took to share. It was very thorough and I have a better understanding now!
I’m sorry, I don’t really understand. “unique” has nothing to do with “similar”. By definition, “unique” means that there is only one - there are no others that are exactly the same. This is how it works in English (beyond casual usage) and this is how it works for Sets. There are no repeated values in a Set. They don’t exist. That’s one of the defining qualities of a set. From my example above:
const set = new Set([1, 2, 3]);
console.log(...set)
// 1 2 3
console.log(set.size)
// 3
set.add(4);
console.log(...set)
// 1 2 3 4
console.log(set.size)
// 4
set.add(1);
console.log(...set)
// 1 2 3 4
console.log(set.size)
// 4
In a Set, all values will be unique, there will be no duplicate values, no two values that are exactly the same - for primitives this means by value and for reference types this means by reference. You cannot add non-unique elements to a Set - it won’t let you.
Thats exactly, what i was trying to say earlier by unique… We are in the same page now.