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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985

2 Answers2

5

Use description to get value

s.description

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

For more read this.

abby37
  • 597
  • 6
  • 21
3

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

A deeper explanation here.

Andrea
  • 6,032
  • 2
  • 28
  • 55