avatarRajdeep Singh

Summary

The web content provides an overview of JavaScript Symbols, their unique identity feature, and practical usage in programming, particularly in React.js.

Abstract

JavaScript Symbols are a primitive data type introduced in ES6 that guarantee a unique value every time they are created. Symbols can be used to create properties with unique identifiers, ensuring no collisions with other property names. The content includes a demonstration of Symbols in action, explains their syntax, and discusses their properties such as Symbol.length and Symbol.prototype. It also emphasizes that Symbols are primitive data types, confirmed by the typeof operator, and that attempting to use the new operator with Symbol results in a TypeError. Additionally, the content encourages readers to explore more about Symbols through provided references and offers the author's contact information for further discussion or queries.

Opinions

  • The author, Rajdeep Singh, suggests that JavaScript Symbols are particularly useful when working with unique identities in programming.
  • Symbols are recommended for use as keys or key props in React.js applications.
  • The author admits to not using Symbols frequently in real-life applications but acknowledges their potential utility for others.
  • Readers are invited to engage with the author by commenting on any mistakes, queries, or suggestions they might have regarding the topic.
  • The author endorses an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), indicating a preference for this tool based on its performance and affordability.

Javascript

What are Javascript Symbols?

The symbol returns a unique value every time.

The data type symbol is a primitive data type in ES6. Every symbol returns a unique identity.

Note

symbol() is a factory function in javascript

Javascript Symbols By Rajdeep Singh

Demo

Syntax

WriteSymbol() in javascript

let symbol= Symbol();
let symbol2= Symbol('Rajdeep singh');
let symbol3= Symbol('Javascript');

When creating a new symbol each time in javascript. So every time, a symbol provides its own unique identity.

Symbol() === Symbol() // false

use new operator makes a shows TypeError in Browser.

let Error= new Symbol() // show TypeError

You are interested to see symbols are primitive data types, so you use the typeof operator. Check data type.

typeof Symbol()  // return "symbol"

Properties

  1. Symbol.length
  2. Symbol.prototype

Symbol.length:

Symbol.length // allways return 0
Symbol("Rajdeep").length); // return undefined

Symbol.prototype:

Symbol.prototype // return Symbol{}
Symbol("Rajdeep").toString() // return Symbol(Rajdeep)
Symbol("Singh").valueOf()  // return Symbol(Singh)

Read More On Medium

Reference

Play Demo

Conclusion

The symbol is useful when you work with a unique identity. you are using the symbol in an object and using it in react.js as key || keys props.

Lots of things you try, but for me, I never use them in real life.

Please tell me in the comment box if you have any queries, mistakes, or suggestions.

Contact me:

JavaScript
Javascript Tips
Javascript Symbols
Symbols
Javascript Development
Recommended from ReadMedium