In JavaScript, you can use a Set to create a collection of unique strings.
In this example,
stringSet
to store strings.add
method.// Create a Set of strings
const stringSet = new Set();
// Add strings to the set
stringSet.add('Apple');
stringSet.add('Banana');
stringSet.add('Cherry');
// Print the set
console.log(stringSet);
Set(3) { 'Apple', 'Banana', 'Cherry' }
In this example,
has
method of the Set object to check if it contains a specific string.has
method returns true if the string is in the set, otherwise false.// Check if the set contains a string
const containsBanana = stringSet.has('Banana');
console.log('Set contains Banana:', containsBanana);
Set contains Banana: true
In this tutorial, we learned How to create a Set of Strings in JavaScript language with well detailed examples.