1

i know that Symbol is a new immutable primitive javascript data type, that always returns a unique value, and i know that they can be used as Object keys, but whats the point exactly, and whats the benefit of them and their use cases ?

let sym2 = Symbol('foo')
let sym3 = Symbol('foo')
Symbol('foo') === Symbol('foo')  // false

with objects:

let obj = {}
let sym = Symbol("foo")

obj[sym] = 'a'
console.log(obj) // { [Symbol()]: "a"}
  • Symbol data type was introduced by keeping "private properties" in mind. But this is little bit argumentative topic now. You can find an example here : https://stackoverflow.com/questions/73000821/why-symbol-data-type-introduced-in-javascript – Sandun Perera Jul 16 '22 at 00:57

0 Answers0