By ppoliani

2015-05-18 11:14:45 8 Comments

I wonder if there is a nice way to get the descriptions of a Symbol.

For example,

var s = Symbol(5);

The default implementation of the toString will simply print Symbol(5)

I would like to find a way to extract the actual description i.e. 5


@Rushi patel 2019-04-11 17:23:38

Symbol.description returns description of the symbol. Simply do s.description in your case.


@Tim Perry 2019-05-14 19:41:33

This is the right answer, and it works great in very modern environments, but note that this is still very new, and isn't supported in even the current node LTS release yet (10.15.3).

@Rushi patel 2019-05-15 01:02:25

@TimPerry is right, Node still doesn't have support for Symbol.description. […‌​. However, as a work around you write a helper function like this. private resolveSymbol(symbol: Symbol): string { const regExp = /\(([^)]+)\)/; const names = regExp.exec(symbol.toString()) || []; return names[1]; }

@user4642212 2019-11-16 12:31:20

@Rushipatel This doesn’t work for Symbol(")") and Symbol(""). Please don’t use regex to get the description. Use String(symbol).slice(7, -1) instead, but note that only the real Symbol.prototype.description can distinguish Symbol() from Symbol("").

@Guest 2016-03-06 20:23:05

 String(symbol).slice(7, -1) || null

This works because String(symbol) explicitly coerce the symbol into a string e.g. Symbol('test') gets coerced into "Symbol(test)".

Doing a splicing on the from 7 to -1 does a splicing between the two brackets, thus capturing the description test

Note: that this may not work for object descriptions (e.g. Symbol({test})) as objects gets coerced into "[object Object]"

@Ryan Gates 2016-03-08 03:33:13

Hi! Welcome to Stack Overflow. Can you please add some explanation as to how/why this answers the question?

@Bergi 2017-07-19 06:53:08

There are no symbols with object descriptions, the description is always cast to a string.

@mightyiam 2016-11-30 18:56:38

symbol-description package on npm

Returns description of provided symbol:

const symDesc = require('symbol-description')
symDesc(Symbol('foo')) // foo

It was published by me just now after reading this answer here.

@zfrisch 2017-01-30 02:36:36

So you created an npm package for one line of code?

@mightyiam 2017-01-30 12:35:40

I did it for you.

@lyschoening 2015-05-18 11:33:08

There is no neat way to do this. However, if you have registered a symbol in the global registry, you can use Symbol.keyFor():

var s = Symbol.for(5);

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

11 Answered Questions

[SOLVED] How can I add new array elements at the beginning of an array in Javascript?

  • 2011-11-10 00:35:22
  • Moon
  • 807336 View
  • 1585 Score
  • 11 Answer
  • Tags:   javascript arrays

16 Answered Questions

[SOLVED] How do I find out which DOM element has the focus?

  • 2009-01-30 20:21:31
  • Tony Peterson
  • 660625 View
  • 1309 Score
  • 16 Answer
  • Tags:   javascript dom

18 Answered Questions

[SOLVED] How to get the children of the $(this) selector?

39 Answered Questions

[SOLVED] How do you get a timestamp in JavaScript?

23 Answered Questions

[SOLVED] Get the current URL with JavaScript?

  • 2009-06-23 19:26:45
  • dougoftheabaci
  • 2821414 View
  • 3004 Score
  • 23 Answer
  • Tags:   javascript url

51 Answered Questions

[SOLVED] How to print a number with commas as thousands separators in JavaScript

73 Answered Questions

[SOLVED] How can I get query string values in JavaScript?

7 Answered Questions

10 Answered Questions

[SOLVED] Using Node.js require vs. ES6 import/export

Sponsored Content