Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output all values of a Set of strings

Tags:

javascript

set

In JavaScript, what is the shortest code to output, for debugging purposes, all elements of a Set of strings? It doesn't matter if the strings are on one line or individual lines.

const set = new Set();
set.add('dog');
set.add('cat');

console.log(???);
like image 493
Dan Dascalescu Avatar asked Dec 23 '22 19:12

Dan Dascalescu


1 Answers

You can use Spread syntax:

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

const set = new Set();
set.add('dog');
set.add('cat');
console.log(...set);
like image 167
Mamun Avatar answered Jan 04 '23 04:01

Mamun