Menu Home

Symbol

Hi everyone, and welcome to another exciting edition of Boring JavaScript! Today, we tackle the primitive data type Symbol – what it is and how it is used.

Don’t like to read? Then watch the video.

It’s all Symbolism

Symbols create unique values that can then be used where variables are required to be unique – such as keys to a record set.

And what do we mean by unique? Let’s take this example:

const myCat = 'Fluffy';
const myOtherCat = 'Fluffy';
console.log(`Is myCat the same as myOtherCat? ${myCat === myOtherCat ? 'Yes!' : 'No!'}`);

/* output
Is myCat the same as myOtherCat? Yes!
*/

In the code above, we have two variables that are strings, and we’ve set them to the same value. And JavaScript says they are equal. Of course, that’s expected. But now let’s look at the same code, but instead of using strings, let’s use Symbols:

const myCat = Symbol('Fluffy');
const myOtherCat = Symbol('Fluffy');
console.log(`Is myCat the same as myOtherCat? ${myCat === myOtherCat ? 'Yes!' : 'No!'}`);

/* output
Is myCat the same as myOtherCat? No!
*/

Now the two variables are not equal to each other. Why? JavaScript guarantees that each Symbol is different from every other symbol, even if the argument passed to the Symbol is the same. That is important to remember – and that is why Symbols are great for using as keys to record sets.

Your Arguments are Baseless

Notice we passed a string as the argument to the Symbol. This is standard with a Symbol. JavaScript will do it’s best to convert whatever you pass to the Symbol as a string and use that as the argument. That means all of the below are valid:

const myBlank = Symbol();
const myNumber = Symbol(1);
const myBoolean = Symbol(true);
const myObject = Symbol({ cat: "Fluffy" });
const myFunction = Symbol( () => { console.log('Inside myFunction') });

However, this is not valid:

const mySymbol = Symbol(Symbol());

But can be made valid by converting it to a string:

const mySymbol = Symbol(Symbol().toString());

Symbols! Huh! What is it Really Good For?

So what’s a good use for symbols? How about unique keys within objects! Remember – whenever you create a symbol, it is guaranteed to be unique as long as your application is running.

For example, let’s take a look at this database:

[
	{ "type": "Cat", "name": "Fluffy", "class": "Mammalia"},
	{ "type": "Dog", "name": "Fido", "class": "Mammalia"},
	{ "type": "Horse", "name": "Mr. Ed", "class": "Mammalia"},
	{ "type": "Cow", "name": "Betsy", "class": "Mammalia"},
	{ "type": "Coyote", "name": "Wile E.", "class": "Mammalia"},
	{ "type": "Road Runner", "name": "Beep Beep", "class": "Aves"},
	{ "type": "Dolphin", "name": "Flipper",  "class": "Mammalia"},
	{ "type": "Whale", "name": "Moby Dick", "class": "Mammalia"},
	{ "type": "Cat", "name": "Fluffy", "class": "Mammalia"},
	{ "type": "Lizard", "name": "Larry", "class": "Reptilia"}
]

Notice that the first entry and the ninth entry are exactly the same (seriously – you can have two cats named Fluffy). Your problem is now that you need to insure that you give unique identifiers to each record so that even the duplicates can be accessed.

Symbols are a perfect and easy way to do that.

animals.forEach( (animal) => {
	animal.key = Symbol();
});

// Now each animal has a unique key!

No more UUIDs! No More GUIDs!

Well … no. You still need them.

Symbols are NOT substitutes for UUIDs or GUIDs. Why? The Symbol is guaranteed to be unique as long as your application is running. Once it stops – that’s it. They are gone. If you run your application again, the process starts all over.

Bottom line: Symbols are great within your application, but not outside of it. If you need uniqueness in a database, for example, use UUIDs or GUIDs.

The Video

We have produced a video that covers Symbols. Check it out!

Shameless Plug

Check us out everywhere!

Check out all our videos at: https://www.boringjavascript.com

Check out everything at: https://www.thevirtuoid.com

Facebook: https://www.facebook.com/TheVirtuoid

Twitter: https://twitter.com/TheVirtuoid

YouTube: https://www.youtube.com/channel/UCKZ7CV6fI7xlh7zIE9TWqgw

Categories: Boring JavaScript Javascript

Tagged as:

thevirtuoid

Web Tinkerer. No, not like Tinkerbell.

Creator of the game Virtuoid. Boring JavaScript. Visit us at thevirtuoid.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: