2

For example, if I use toString():

let s = Symbol('abc')
console.log(s.toString())

I get:

'Symbol(abc)'

How to get just the:

'abc'

part instead?

I know how to do this with string manipulation, but I would hope for a potentially more efficient solution that directly obtains the value.

I am using Symbol to implement an Enum: What is the preferred syntax for defining enums in JavaScript? and want to serialize it with a toJSON() on the containing class.

Tested in Node.js v10.15.1.

2 Answers2

4

Use description to get value

s.description

As when we create Symbol we pass description of that symbol.

For more read this.

abby37
  • 595
  • 4
  • 19
3

I would use s.description. It will return the description of the Symbol.

A deeper explanation here.

Andrea
  • 5,893
  • 1
  • 29
  • 53