-1

When we say that object property keys may be either of string type, or of symbol type. Not numbers, not booleans, only strings or symbols. What it really means.

noman tufail
  • 341
  • 2
  • 8

1 Answers1

2

Symbol is a new primitive type introduced in ES2015. They are distinct from other types (strings, numbers, objects). Every Symbol is unique. They can optionally have a string description, but again, they are not strings. Their primary benefit is that they are guaranteed unique, unlike using a string key which could conflict with other code using the same string key on the same object.

So for instance:

const s = Symbol();
const o = {[s]: "foo"};
console.log(o[s]); // "foo"

More on MDN.

Note: You'll find a lot of resources and indeed answers here on SO saying that Symbols provide privacy. They don't. At all. Not even a little bit. Anyone with a reference to the object can read all of its properties, including Symbol-named properties.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I should have realized this was a duplicate. I have to say, I don't think much of the answer on the dupetarget, or indeed the answers on *its* dupetargets, many or indeed most of which erroneously claim symbols are private (even ones written after the spec was completed, which was at least a year if not two after Symbol privacy was abandoned). So I'll leave this here. – T.J. Crowder Feb 28 '18 at 07:41