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

4 comments

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

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

REF: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/description

@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. [developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…‌​. 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]; }

@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);
console.log(Symbol.keyFor(s));

Related Questions

Sponsored Content

48 Answered Questions

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

13 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
  • 740085 View
  • 1466 Score
  • 13 Answer
  • Tags:   javascript arrays

8 Answered Questions

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

73 Answered Questions

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

21 Answered Questions

[SOLVED] Get the current URL with JavaScript?

  • 2009-06-23 19:26:45
  • dougoftheabaci
  • 2707982 View
  • 2872 Score
  • 21 Answer
  • Tags:   javascript url

18 Answered Questions

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

17 Answered Questions

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

38 Answered Questions

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

6 Answered Questions

16 Answered Questions

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

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

Sponsored Content