JavaScript Sets
A JavaScript Set is a collection of unique values.
Each value can only occur once in a Set.
Essential Set Methods
Method | Description |
---|---|
new Set() | Creates a new Set |
add() | Adds a new element to the Set |
delete() | Removes an element from a Set |
has() | Returns true if a value exists in the Set |
forEach() | Invokes a callback for each element in the Set |
values() | Returns an iterator with all the values in a Set |
Property | Description |
size | Returns the number of elements in a Set |
How to Create a Set
You can create a JavaScript Set by:
- Passing an Array to
new Set()
- Create a new Set and use
add()
to add values - Create a new Set and use
add()
to add variables
The new Set() Method
Pass an Array to the new Set()
constructor:
Example
// Create a Set
const letters = new Set(["a","b","c"]);
Create a Set and add values:
Example
// Create a Set
const letters = new Set();
// Add Values to the Set
letters.add("a");
letters.add("b");
letters.add("c");
Create a Set and add variables:
Example
// Create a Set
const letters = new Set();
// Create Variables
const a = "a";
const b = "b";
const c = "c";
// Add Variables to the Set
letters.add(a);
letters.add(b);
letters.add(c);
The add() Method
Example
letters.add("d");
letters.add("e");
If you add equal elements, only the first will be saved:
Example
letters.add("a");
letters.add("b");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
The forEach() Method
The forEach()
method invokes (calls) a function for each Set element:
Example
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Elements
let text = "";
letters.forEach (function(value) {
text += value;
})
The values() Method
The values()
method returns a new iterator object containing all the values in a Set:
Example
letters.values() // Returns [object Set Iterator]
Now you can use the Iterator object to access the elements:
Example
// List all Elements
let text = "";
for (const x of letters.values()) {
text += x;
}
42 Responses
levaquin for sale buy levaquin generic
avodart order order dutasteride generic order zofran 8mg without prescription
spironolactone 100mg oral buy finasteride 1mg buy fluconazole 200mg online cheap
order ampicillin 250mg sale cost cephalexin 500mg buy erythromycin 500mg pills
buy fildena 50mg sale brand sildenafil 100mg robaxin order online
cost suhagra 100mg sildenafil online buy order estradiol 2mg without prescription
purchase lamotrigine sale buy vermox generic retin medication
brand tadalafil 20mg cheap avana 100mg buy voltaren 100mg without prescription
buy isotretinoin pill zithromax drug buy zithromax 500mg pills
indocin without prescription suprax without prescription order trimox 500mg online cheap
buy cialis 10mg for sale ed medications online viagra 100mg pills for men
buy arimidex online cheap Us pharmacy viagra viagra canada
buy prednisone 5mg pill canada pharmacy sildenafil online
cialis für männer cialis kaufen viagra 200mg generika
buy generic isotretinoin 10mg buy amoxil 500mg pill ivermectin 12 mg
purchase provigil for sale Buy cialis once daily acetazolamide 250mg pill
order doxycycline doxycycline price buy furosemide 100mg sale
altace brand altace online order azelastine 10 ml canada
catapres 0.1mg tablet minocin order generic spiriva 9 mcg
terazosin ca buy generic actos 15mg order sulfasalazine 500 mg for sale
purchase fosamax pills famotidine 20mg ca famotidine 20mg for sale
order benicar 10mg sale acetazolamide 250mg pills order acetazolamide 250 mg for sale
prograf uk cheap trandate 100 mg urso us
imdur 40mg pills cost isosorbide 20mg buy telmisartan 80mg online cheap
zyban buy online oral quetiapine seroquel over the counter
brand molnupiravir generic omnicef 300mg lansoprazole 15mg canada
order sertraline 50mg online sildenafil 200mg us sildenafil over the counter
buy salbutamol 100mcg generic protonix 40mg generic viagra pill
cialis drug cialis cost oral viagra 100mg
buy cialis 40mg without prescription order phenazopyridine 200mg online cheap order amantadine 100mg generic
buy revia 50mg pill buy albenza 400 mg sale aripiprazole uk
dapsone 100 mg ca buy adalat 30mg for sale oral aceon 8mg
provera sale order periactin for sale cyproheptadine online order
provigil pill buy stromectol sale ivermectin 12 mg tablets for humans
luvox 50mg tablet order duloxetine generic order generic glipizide 10mg
accutane 40mg cheap brand amoxicillin 250mg prednisone 20mg drug
purchase piracetam pills buy sildenafil 100mg for sale viagra 50 mg
cost zithromax 250mg prednisolone 5mg us buy gabapentin 800mg sale
buying cialis cheap brand tadalafil 40mg buy viagra 50mg online
furosemide pills order doxycycline 200mg online buy plaquenil
buy cialis 40mg pill buy anafranil 50mg generic buy anafranil sale
tips for getting pregnant on clomid As expected, dogs with severe heart failure, particularly left heart failure, have a worse prognosis than those with milder signs or signs of right heart failure at presentation