DEV Community

Cover image for Introduction to Javascript Symbol Type
Srijan
Srijan

Posted on • Originally published at hackinbits.com

Introduction to Javascript Symbol Type

The symbol is a primitive datatype introduced in ECMAScript 2015. It is different from other data types in Javascript as its value is always unique and kept private for internal use. Symbols are only accessible from its reference after it is created.

Creating a Symbol

A symbol is created by calling Symbol() global factory function.

const mySym = Symbol();
Enter fullscreen mode Exit fullscreen mode

Symbol() takes one optional parameter, which is a description of the symbol itself. Specifying a description while creating Symbols helps identify it during debugging.

const mySym = Symbol('name of person');
//Symbol(name of person)
Enter fullscreen mode Exit fullscreen mode

The new keyword is not supported by Symbol(). The below code will throw a type error.

let sym = new Symbol()

// Uncaught TypeError: Symbol is not a constructor
// at new Symbol (<anonymous>)
Enter fullscreen mode Exit fullscreen mode

Equality

Symbols are unique identifiers and are never equal to each other.

console.log(Symbol() === Symbol())
//false  
Enter fullscreen mode Exit fullscreen mode

Symbols are useful for creating object properties as they are always unique, never equal to each other, thus avoiding name clashes.

const LESSON = Symbol()
const subject = {
  [LESSON]: 'Symbols on javascript'
}

subject[LESSON];
// "Symbols on javascript"
Enter fullscreen mode Exit fullscreen mode

The symbol also prevents the overwriting of properties, intentionally or unintentionally as values created by Symbol() method is always unique.

Iterating over Symbol

Symbols are not enumerable i.e. they are not accessible by for...of or for...in methods. They will not be listed.

let obj = {} 
obj[Symbol('one')] = 1
obj[Symbol.for('two')] = 2
obj['three'] = 3
for (let i in obj) { 
console.log(i) 
// three
}
Enter fullscreen mode Exit fullscreen mode

Using typeof operator on Symbol

Using typeof operator you can identify symbols.

let sym = Symbol();
typeof sym;
// 'symbol'
Enter fullscreen mode Exit fullscreen mode

Summary

In this article, we learned:

  • The symbol is a primitive datatype.
  • Symbols are always unique.
  • You can create a symbol by using Symbol() factory function.
  • Symbols are not enumerable.
  • You can use typeof operator to identify symbols.

This article is first published on hackinbits.com

Top comments (3)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

Learning something new is one thing, but why exactly did the committee created it? For the internals? Or for people to create new Symbols?

Collapse
 
srijan1709 profile image
Srijan

This may be the one reason, I faced this sometime in my codebase too:


The symbol also prevents the overwriting of properties, intentionally or unintentionally as values created by Symbol() method is always unique.


Please share any other cases where symbol type is more effective.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

When you want something more unique than UUID string, and you would never need to compare for equality. Or, you don't want to use string.

My use case was undefinedProxy in any-serialize.

Otherwise, I only see it IRL for

  • Symbol.iterator
  • Symbol.asyncIterator
  • and, other Symbol.xxx or Symbol.for('xxxString'), e.g. util.inspect

So, I guess, mostly internal stuff. Creating one yourself is very very rare.