CodesJava

Easy learning with example program codes

JavaScript Set

To store the elements with unique primitive values or object references, javascript set object is used.

Syntax:

new Set([iterable])

Where: iterable is the object whose elements will be added to the new Set.

Note: JavaScript set cannot contain duplicate elements i.e. it keeps unique elements only.

JavaScript Set Methods:

add()
Use: To add the specified values to the Set object.

clear()
Use: To remove all the elements from the Set object.

delete()
Use: To delete the specified element from the Set object.

entries()
To return an object of Set iterator that contains an array of [value, value] for each element.

forEach()
Use: To execute the specified function once for each value.

has()
Use: To indicate whether the Set object contains the specified value element.

values()
Use: To return an object of Set iterator that contains the values for each element.

Example:

<!DOCTYPE html>
<html>
<body>
 
<script>
const set1 = new Set([11, 22, 31, 45, 57]);
 
console.log(set1.has(17));
//output: false
 
console.log(set1.has(57));
//output: true
 
console.log(set1.has(61));
//output: false
</script>
 
</body>
</html>
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status