Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

JavaScript Sets1

Uploaded by

Maiwand Mangal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JavaScript Sets1

Uploaded by

Maiwand Mangal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

JavaScript Sets
❮ Previous Next ❯

A JavaScript Set is a collection of unique values.

Each value can only occur once in a Set.

The values can be of any type, primitive values or objects.

How to Create a Set


You can create a JavaScript Set by:

Passing an array to new Set()


Create an empty set and use add() to add values

The new Set() Method


Pass an array to the new Set() constructor:

Example

// Create a Set
const letters = new Set(["a","b","c"]);
Try it Yourself »
Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ 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");

Try it Yourself »

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);

Try it Yourself »

The add() Method


 Tutorials 
Example Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
letters.add("d");
letters.add("e");

Try it Yourself »

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");

Try it Yourself »

Listing the Elements


You can list all Set elements (values) with a for..of loop:

Example
// Create a Set
const letters = new Set(["a","b","c"]);

// List all Elements


let text = "";
for (const x of letters) {
text += x;
 } Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Try it Yourself »

Sets are Objects

typeof returns object:

typeof letters; // Returns object

Try it Yourself »

instanceof Set returns true:

letters instanceof Set; // Returns true

Try it Yourself »

Complete Set Reference


For a complete reference, go to our:

Complete JavaScript Set Reference.

The reference contains descriptions and examples of all Set Properties and Methods.

Browser Support
Set is an ES6 feature (JavaScript 2015).

ES6 is fully supported in all modern browsers since June 2017:


 Tutorials 
Chrome 51
Exercises 
Edge 15
Services 
Firefox 54

Safari 10
Sign Up
Opera 38
Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
May 2016 Apr 2017 Jun 2017 Sep 2016 Jun 2016

Set is not supported in Internet Explorer.

❮ Previous Next ❯

W3schools Pathfinder
Track your progress - it's free! Sign Up Log in

COLOR PICKER

 

 SPACES UPGRADE AD-FREE NEWSLETTER


 Tutorials 
GET CERTIFIED
Exercises  Services 
CONTACT US
 Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

Top Tutorials Top References


HTML Tutorial HTML Reference
CSS Tutorial CSS Reference
JavaScript Tutorial JavaScript Reference
How To Tutorial SQL Reference
SQL Tutorial Python Reference
Python Tutorial W3.CSS Reference
W3.CSS Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT CLASSROOM


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy
policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

You might also like