1. What is a Set in JavaScript?
Answer:
Explanation:
In JavaScript, a Set is an object that allows you to store unique values of any type, whether primitive values or object references.
2. How do you create a Set in JavaScript?
Answer:
Explanation:
A Set is created using the new Set() constructor.
3. Can a JavaScript Set contain duplicate values?
Answer:
Explanation:
Sets in JavaScript are collections of unique values, so they cannot contain duplicates.
4. How can you add a value to a Set in JavaScript?
Answer:
Explanation:
The add method is used to add a new element with a specified value to a Set in JavaScript.
5. How can you remove a value from a Set in JavaScript?
Answer:
Explanation:
The delete method is used to remove a specified value from a Set.
6. What method is used to clear all values from a Set in JavaScript?
Answer:
Explanation:
The clear method removes all elements from a Set, effectively clearing it.
7. How can you check if a value is present in a Set in JavaScript?
Answer:
Explanation:
The has method is used to check if a Set contains a specified element.
8. What does the size property of a Set in JavaScript represent?
Answer:
Explanation:
The size property returns the number of values in a Set.
9. How can you iterate over the values in a Set in JavaScript?
Answer:
Explanation:
You can iterate over a Set using a for loop or the forEach method.
10. What type of values can you store in a Set in JavaScript?
Answer:
Explanation:
JavaScript Sets can store any type of values, whether they are primitive values or object references.
11. Can a Set in JavaScript have a length property?
Answer:
Explanation:
JavaScript Sets do not have a length property; instead, they have a size property.
12. What happens when you try to add a duplicate value to a Set in JavaScript?
Answer:
Explanation:
If you try to add a duplicate value to a Set, the Set simply ignores it since all values in a Set must be unique.
13. Is the order of elements preserved in a JavaScript Set?
Answer:
Explanation:
Elements in a Set are iterated in the order of their insertion.
14. How do you convert a Set to an Array in JavaScript?
Answer:
Explanation:
A Set can be converted to an Array using the Array.from method or the spread operator […mySet].
15. Can a JavaScript Set be nested within another Set?
Answer:
Explanation:
Sets in JavaScript can contain any type of values, including other Sets, allowing them to be nested.