Iterating over an object with symbols in JavaScript

The advantage of using a Symbol as an object key is that such keys will not be looped through.

Let's look at an example. Let's have the following object:

let obj = {a: 1, b: 2, c: 3};

Let's add a new element to this object with a symbol key:

let sym = Symbol(); obj[sym] = 'text';

Let's loop over this object. As a result, we will see all elements except ours with a symbol key:

for (let key in obj) { console.log(obj[key]); // 1, 2, 3 }

Make an object with a key from a symbol. Loop through it. Make sure that the symbol doesn't participate in the looping.