Symbol type in JavaScript - Part 2

July 19, 2020 - 2 min read

We looked at the very basics of Symbol datatype in Javascript in our previous blog post.

There we have discussed how to add the Symbol datatype to an object and reading the value of the Symbol.

Symbol Literal

Just like we added a Symbol using the bracket notation to an object we can add Symbol directly to an object.

We need to use the [] (bracket) notation inside the object like this.

// create a symbol
const id = Symbol("id");

// an object
const obj = {
  id: 23,
  [id]: 23,
};

NOTE: Don't put the symbol id inside quotes, pass the reference of the created symbol.

Not shown by for...in loop

The Symbols on an object will not be shown in a for...in loop.

// create a symbol
const id = Symbol("id");

// an object
const obj = {
  id: 23,
  [id]: 23,
};

// for...in loop
for (let prop in obj) {
  console.log(prop, obj[prop]); // Symbol types are not shown
}

Now, if you want to know if any Symbol types are present you can use the Object.getOwnPropertySymbols() method.

// create a symbol
const id = Symbol("id");

// an object
const obj = {
  id: 23,
  [id]: 23,
};

// get all symbols present in an object
const symbols = Object.getOwnPropertySymbols(obj);

console.log(symbols);

Feel free to share if you found this useful 😃.