JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – does set object really store duplicate object

JavaScript Quiz – does set object really store duplicate object

JavaScript Set Object

In javascript, set object not only let you only store the primitive date type value but also non-primitive data type value. But javascript array can do the same thing. Then why need a set object.

Javascript set object does not allow duplicate values to be pushed it contains only unique values. For example, if I have to push the javascript string “js startup” then in an array you have length 2 if push twice.

But in set object it’s size is 1 because it neglect the second one.

It’s syntax looks like –

new Set()

For more detail about the javascript set object click here.

JavaScript Question

In this javascript quiz, find out does set objects really store duplicate objects as we know set objects only allows unique values.

First, we declare a variable set using const in which we assign a set object with a new keyword.

const set = new Set();

In the second step, we create another variable student using const keyword and assign an javascript object with name key in it.

const student = { name : "Ajay" };

Now, we add student object to set variable using set object add() method.

set.add(student);
set.add(student);

Finally, we are checking the size of the set object using size property.

console.log(set.size);

Any idea, what should be the idea of above statement ?

JS Quiz Code

const set = new Set();

const student = { name : "Ajay" };

set.add(student);
set.add(student);

console.log(set.size); // output => ?

Answer – 1

Click To Run Code

Explanation

Now, you know the answer is 1 but why because it’s an object not any string or number. How does it compare. Let me explain

Javascript set objects only store unique values either it’s for string or number or object. So, the set will neglect the student add method when he found out the same object again.

Note: set have size property not a length property.

At last, That’s why we get an answer “1“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – what is the math random function range
  2. JavaScript Quiz – numbers array can be sort
  3. JavaScript Quiz – switch case can change a variable value
  4. JS Quiz – zero date can give get full year method

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

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

Back to top
0 Shares
Share via
Copy link