Open In App

JavaScript Set Reference

Last Updated : 07 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Set is a collection of items that are unique i.e. no element can be repeated. Elements of the set can be iterated in the insertion order. A set can store any type of value whether primitive or objects.

Syntax:

Set.function()

Example: This example will show the use of Set() constructor.

Javascript




const mySet = new Set();
  
mySet.add("California");
mySet.add("India");
mySet.add("California");
mySet.add(10);
mySet.add(10);
  
const myObject = { a: 1, a: 5 };
  
mySet.add(myObject);
  
console.log(mySet);


Output:

Set(4) { 'California', 'India', 10, { a: 5 } }

The complete list of JavaScript Set has listed below:

JavaScript Set Constructor: A constructor gets called when an object is created using the new keyword.

Constructor Description Example
Set() Create a set of unique values of any type, whether primitive values or object preferences.
Try

JavaScript Set Properties:  A JavaScript property is a member of an object that associates a key with a value.

  • Instance Property: An instance property is a property that has a new copy for every new instance of the class.
Instance Properties Description Example
size Returns the size of set
Try

constructor Used to create a new instance of set
Try

JavaScript Set Methods: Methods are actions that can be performed on objects.

  • Instance Method: If the method is called on an instance of a Set then it is called an instance method.
Instance Methods Description Example
add() Add an element to the set with the specified value
Try

clear() Removes all the elements from the set
Try

delete() Delete specified element if it exists in the set
Try

entries() Used to iterate over each element of the set
Try

forEach() Applies a function on each element of the set
Try

has() Checks if a particular element exists in the set
Try

values() Returns an iterator that contains all elements of the set
Try

keys() Exactly equivalent to the values method
Try



Previous Article
Next Article

Similar Reads

JavaScript is showing reference error "Prompt is not defined"
In this article, we cover the reference error that is "Prompt is not defined". It's important to understand the tools you are using. I know, this can be very overwhelming in the beginning. but here, we will solve this reference error with not only one solution but also make more ways to done properly. window.prompt() instructs the browser to displa
3 min read
JavaScript Math Reference
JavaScript Math object is used to perform mathematical operations on numbers. Math is an inbuilt object that works with numbers types but it does not work with BigInt. Example: Below example will give you a brief idea of JavaScript math objects. C/C++ Code // Return PI value(3.141592653589793) console.log(Math.PI); Output: This will print the value
4 min read
JavaScript Boolean Reference
JavaScript Boolean is a datatype that returns either true or false In JavaScript, a boolean is used as a function to get the value of a variable, object, conditions, expressions, etc in terms of true or false Syntax: Boolean(variable/expression) Example: If the operands are equal, the equal to operator == returns true C/C++ Code <script> cons
2 min read
JavaScript BigInt Reference
JavaScript BigInt is an inbuilt object in JavaScript that provides a way to represent whole numbers larger than 253-1 which is the largest number that can represent with the Number primitive. It is represented by the MAX_SAFE_INTEGER constant. Example: This example creates a BigInt by appending n at the end of the number. C/C++ Code // Decimal form
2 min read
Primitive and Reference value in JavaScript
In JavaScript, a variable may store two types of values, Primitive values or Reference values. This article will describe and help to compare both these types of values. Primitive value: JavaScript provides six types of primitive values that include Number, String, Boolean, Undefined, Symbol, and BigInt. The size of Primitive values are fixed, ther
2 min read
Reference and Copy Variables in JavaScript
In this article, we will talk about pass-by-value and pass-by-reference in JavaScript. JavaScript always passes by value, but in an array or object, the value is a reference to it, so you can 'change' the data. JavaScript has 5 primitive data types that are passed by value, they are Boolean, NULL, undefined, String, and Number. It has 3 non-primiti
5 min read
JavaScript ReferenceError - Reference to undefined property "x"
This JavaScript warning reference to undefined property occurs if a script tries to access an object property that doesn't exist. Message: ReferenceError: reference to undefined property "x" (Firefox) Error Type: ReferenceError(Only reported by firefox browser) Cause of the error: The script is trying to access an object property that doesn't exist
2 min read
Call by Value Vs Call by Reference in JavaScript
Call by Value: In this method, values of actual parameters are copied to the function’s formal parameters, and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller. Suppose there is a variable named “a”. Now, we store a primitive value(boo
3 min read
Foundation CSS Dropdown Menu JavaScript Reference
Foundation CSS is an open-source and responsive front-end framework built by the ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing and can be accessible to any device. The Dropdown Menu is used for displaying an expandable dropdown menu by using the Foundation CSS plug
6 min read
JavaScript Proxy/handler Reference
JavaScript Proxy Object is used to define the custom behavior of fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc). Syntax: const p = new Proxy(target, { Proxy method: function(target, thisArg, argumentsList) { } }); Example: Below examples illustrate the handler.apply() method in JavaScript: C/C++ Cod
2 min read
Article Tags :