codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

38 / 65 topics
35JavaScript Sets36JavaScript Maps37JavaScript Dates38JavaScript Symbols
Tutorials/JavaScript/JavaScript Symbols
🌐JavaScript

JavaScript Symbols

Updated 2026-05-12
30 min read

JavaScript Symbols

In the world of programming, uniqueness is a powerful concept. Just like how every snowflake is unique, JavaScript introduces a primitive type called Symbol that guarantees uniqueness among all values in your application. This tutorial will explore what Symbols are, why they matter, and how to use them effectively.

Introduction

JavaScript's Symbol is a new primitive data type introduced in ECMAScript 6 (ES6). Unlike other primitives like strings or numbers, each Symbol value created is unique, even if they have the same description. This makes Symbols ideal for creating hidden properties on objects and ensuring that property names do not clash with existing ones.

Understanding Symbols is crucial for advanced JavaScript programming, especially when working with complex systems where uniqueness of identifiers is paramount. In this tutorial, we'll dive deep into how to create, use, and manipulate Symbols.

Core Content

What are Symbols?

A Symbol is a primitive data type that represents a unique identifier. Each Symbol value is unique and immutable. You can think of symbols as a way to create private or hidden properties on objects.

Creating Symbols

To create a new Symbol, you use the Symbol() function. Optionally, you can pass a description (also known as a name) that helps with debugging but does not affect the uniqueness of the symbol.

createSymbols.js
1const sym1 = Symbol('description');
2const sym2 = Symbol('description');
3
4console.log(sym1 === sym2); // false
Output
false

Explanation: Even though sym1 and sym2 have the same description, they are unique instances of Symbol.

Using Symbols as Object Properties

One of the most common uses of Symbols is to define object properties that should not be easily accessible or enumerable. This is particularly useful for creating private fields in classes.

symbolProperties.js
1const sym = Symbol('secret');
2
3let obj = {};
4obj[sym] = 'This is a secret';
5
6console.log(obj[sym]); // Accessing the symbol property
7console.log(Object.keys(obj)); // Enumerable properties
Output
This is a secret
[]

Explanation: The sym property is not included in the list of enumerable properties, making it effectively hidden from normal iteration.

Global Symbols

Sometimes, you might want to create symbols that are globally accessible. You can use Symbol.for() for this purpose. This function searches for existing symbols with the given key and returns them if found; otherwise, it creates a new one.

globalSymbols.js
1const sym1 = Symbol.for('global');
2const sym2 = Symbol.for('global');
3
4console.log(sym1 === sym2); // true
Output
true

Explanation: Both sym1 and sym2 are the same symbol because they were created using the same key.

Built-in Symbols

JavaScript has several built-in symbols that you can use to define well-known behaviors for your objects. These symbols include methods like Symbol.iterator, Symbol.toStringTag, etc.

builtInSymbols.js
1const obj = {
2[Symbol.toStringTag]: 'CustomObject'
3};
4
5console.log(obj.toString()); // Output will include the custom tag
Output
[object CustomObject]

Explanation: By defining Symbol.toStringTag, you customize how objects are converted to strings, which can be helpful for debugging and logging.

Practical Example

Let's create a simple application that demonstrates the use of symbols in managing private data within an object.

practicalExample.js
1const id = Symbol('id');
2
3class User {
4constructor(name) {
5 this[id] = Math.random();
6 this.name = name;
7}
8
9getId() {
10 return this[id];
11}
12}
13
14const user = new User('Alice');
15console.log(user.getId()); // Accessing the private ID
16console.log(user.id); // undefined, because 'id' is a symbol and not enumerable
Output
0.123456789 (random value)
undefined

Explanation: The id property is hidden from normal enumeration and can only be accessed through the getId() method, ensuring encapsulation.

Summary

ConceptDescription
SymbolA unique primitive data type in JavaScript.
UniquenessEach symbol created is unique, even if they have the same description.
Hidden PropertiesSymbols can be used to create hidden properties on objects that are not enumerable.
Global SymbolsSymbol.for() creates symbols that are globally accessible.
Built-in SymbolsSpecial symbols that define well-known behaviors for objects.

What's Next?

Now that you have a solid understanding of JavaScript Symbols, the next step is to explore how to handle errors in your code using the try...catch...finally statement. This will help you build more robust and reliable applications.

In the upcoming tutorial, we'll cover:

  • How to use try...catch...finally blocks.
  • Handling different types of exceptions.
  • Best practices for error handling in JavaScript.

Stay tuned!


PreviousJavaScript DatesNext JavaScript try...catch...finally

Recommended Gear

JavaScript DatesJavaScript try...catch...finally