MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Symbols & Well-Known Symbols0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
10.1TypedArrays & ArrayBuffers10.2WeakMap & WeakSet for Memory Management10.3Object.freeze, Object.seal & Immutability10.4Symbols & Well-Known Symbols
HomeTutorialsJavaScript for AdvancedSymbols & Well-Known Symbols
WebSockets for Real-Time Bidirectional Communication

Symbols & Well-Known Symbols

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

Introduced in ES6, Symbol is a primitive data type that represents a unique, immutable identifier. Symbols are used to define private-like object properties that never collide with other keys, and to hook into JavaScript's internal engine mechanics via Well-Known Symbols.


1. Creating and Using Symbols

Every call to Symbol() produces a globally unique value, even with the same description:

1const sym1 = Symbol('apiKey');
2const sym2 = Symbol('apiKey');
3 
4console.log(sym1 === sym2); // FALSE! Each Symbol is guaranteed unique!
5console.log(typeof sym1); // "symbol"

Using Symbols as Object Keys (Collision-Proof Properties):

1const USER_ID = Symbol('unique_id');
2 
3const user = {
4 name: 'Alex',
5 [USER_ID]: 'USR_98214' // Symbol-keyed property
6};
7 
8console.log(user[USER_ID]); // "USR_98214"
9 
10// Symbols are hidden from standard object iteration!
11console.log(Object.keys(user)); // ['name'] (USER_ID is omitted!)
12console.log(JSON.stringify(user)); // '{"name":"Alex"}' (Omitted from JSON!)
13 
14// Explicit retrieval via reflection:
15console.log(Object.getOwnPropertySymbols(user)); // [ Symbol(unique_id) ]

2. The Global Symbol Registry: Symbol.for() & Symbol.keyFor()

When multiple scripts or iframes need to share the same Symbol instance:

1// Registers or retrieves 'app.version' from global cross-realm registry
2const globalSym1 = Symbol.for('app.version');
3const globalSym2 = Symbol.for('app.version');
4 
5console.log(globalSym1 === globalSym2); // TRUE! Same shared symbol!
6 
7// Retrieve string key from symbol:
8console.log(Symbol.keyFor(globalSym1)); // "app.version"

3. Well-Known Symbols (Engine Hooks)

Well-Known Symbols are built-in symbols that expose internal engine algorithms:

1. Symbol.toPrimitive: Custom Type Coercion

Customizes how an object converts to a string or number:

1class Money {
2 constructor(amount, currency) {
3 this.amount = amount;
4 this.currency = currency;
5 }
6 
7 [Symbol.toPrimitive](hint) {
8 if (hint === 'number') return this.amount;
9 if (hint === 'string') return `${this.currency} ${this.amount.toFixed(2)}`;
10 return this.amount; // 'default' hint
11 }
12}
13 
14const wallet = new Money(50, 'USD');
15console.log(+wallet + 10); // 60 (Number coercion)
16console.log(`${wallet}`); // "USD 50.00" (String coercion)

2. Symbol.hasInstance: Customizing instanceof

1class EvenNumber {
2 static [Symbol.hasInstance](instance) {
3 return typeof instance === 'number' && instance % 2 === 0;
4 }
5}
6 
7console.log(4 instanceof EvenNumber); // true!
8console.log(7 instanceof EvenNumber); // false!

3. Symbol.species: Subclass Derivative Constructor

Allows derived classes (like custom Array subclasses) to dictate what constructor built-in methods (like .map() or .filter()) return.


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary characteristic of a Symbol primitive in JavaScript?
Question 2
How do Symbol-keyed properties behave during Object.keys() and JSON.stringify()?
Question 3
What method creates or retrieves a shared symbol from the runtime's Global Symbol Registry?
Question 4
Which Well-Known Symbol customizes how an object is coerced into a string, number, or default primitive?
Question 5
How can you retrieve an array of all Symbol properties defined on an object?
Next Lesson

WebSockets for Real-Time Bidirectional Communication

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Object.freeze, Object.seal & ImmutabilityWebSockets for Real-Time Bidirectional Communication

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext