ES6 Symbol Types

Symbols are a unique and integral part of the ES6 (ECMAScript 2015) feature set, providing a way to create unique, immutable identifiers for object properties. They play a crucial role in ensuring property keys do not conflict, especially in large codebases and libraries.

Creating Symbols

Creating a symbol is straightforward. You use the Symbol() function, optionally passing a description to help with debugging:

const sym1 = Symbol(); const sym2 = Symbol("description"); const sym3 = Symbol("description");

Each call to Symbol() creates a unique symbol, even if the descriptions are the same:

console.log(Symbol("foo") === Symbol("foo")); // false

Trying to use the new operator with Symbol will throw an error:

const sym = new Symbol(); // TypeError

Symbols can also be explicitly converted to symbol wrapper objects:

const sym = Symbol("foo"); const symObj = Object(sym); console.log(typeof symObj); // "object"

Retrieving Global Symbols

ES6 provides a global symbol registry to share symbols across different parts of an application. You can create or retrieve these symbols using Symbol.for() and Symbol.keyFor():

const globalSym = Symbol.for("globalKey"); const sameGlobalSym = Symbol.for("globalKey"); console.log(globalSym === sameGlobalSym); // true console.log(Symbol.keyFor(globalSym)); // "globalKey"

Well-known Symbols

JavaScript defines several well-known symbols for customizing language behavior:

class Collection { constructor(items) { this.items = items; } *[Symbol.iterator]() { for (const item of this.items) { yield item; } } } const myCollection = new Collection([1, 2, 3]); for (const item of myCollection) { console.log(item); // 1, 2, 3 }

Some other well-known symbols include:

  • Symbol.hasInstance for instanceof checks.

  • Symbol.toStringTag for customizing toString() results.

  • Symbol.isConcatSpreadable for array concatenation.

Symbols as Object Properties

Symbols can be used as object property keys. They are not enumerated in typical object iterations and are ignored by JSON.stringify():

const obj = {}; const privateProp = Symbol("private"); obj[privateProp] = "secret"; obj.publicProp = "visible"; for (const key in obj) { console.log(key); // "publicProp" } console.log(JSON.stringify(obj)); // "{"publicProp":"visible"}"

To access symbol properties, use Object.getOwnPropertySymbols():

const symbols = Object.getOwnPropertySymbols(obj); console.log(symbols); // [Symbol(private)]

Conclusion

Symbols provide a powerful way to ensure uniqueness and encapsulation in JavaScript objects. Whether it's minimizing collisions in object keys or creating internal-only APIs, symbols offer robust capabilities that can be easily integrated into your development toolkit. Understanding how to effectively leverage symbols can lead to more maintainable and reliable codebases.

Start Building in JavaScript Today

Lilbots is the best platform for creating powerful JavaScript bots

  • Built in access to cutting edge AI models
  • Out of the box integration with hundreds of APIs
  • Collaborate with coding workspaces
  • Discover and fork thousands of bots built by the community

Learn JavaScript

JavaScript Objects

JavaScript Arrays

JavaScript Basics