Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Set Methods in JavaScript

Js Set Methods in JavaScript

?? JavaScript Set Methods

A Set is a collection of unique values — no duplicates allowed.


Creating a Set

const mySet = new Set([1, 2, 3, 4]);

Common Set Methods

MethodDescriptionExample
add(value)Adds a new element to the setmySet.add(5);
delete(value)Removes an element if it existsmySet.delete(2);
has(value)Checks if an element exists, returns true or falsemySet.has(3); // true
clear()Removes all elementsmySet.clear();
size (property)Returns number of elementsmySet.size; // 4
values()Returns an iterator for the set valuesfor (let val of mySet.values())

Example Usage

const mySet = new Set();mySet.add(1);mySet.add(2);mySet.add(2);  // Duplicate ignoredconsole.log(mySet.size);    // 2console.log(mySet.has(1));  // truemySet.delete(1);console.log(mySet.has(1));  // falsemySet.clear();console.log(mySet.size);    // 0

Iterating Over a Set

const mySet = new Set(['a', 'b', 'c']);for (let item of mySet) {  console.log(item);}// Output:// a// b// c

If you want, I can explain more about Set vs Array or advanced methods like forEach() on Sets!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql