ES6 in Depth
  • Introduction
  • Installation
  • Block Scope
    • Constant
    • Use cases
  • Template String
    • New String Methods
    • Tagged Template Literal
  • New Number Methods
  • Arrow Function
    • Default Parameters
    • Lexical This Scope
    • Where not to use arrow function
  • Object Enhancement
  • New Array Methods
    • For Of
  • Spread Operator
  • Destructuring
    • Array Destructuring
  • Class
  • Symbols
  • New Data Structures
    • Set
    • Map
    • WeakSet
    • WeakMap
    • Iterators
    • Generators
  • Promise
  • Import Export
Powered by GitBook
On this page

Was this helpful?

Symbols

  • 6th primitive in javascript ( number, boolean, string, null, undefined )

  • Use to created unique identifier

  • or basically used to avoid conflict of variable names

let x= Symbol('hemant');
// it may happen
let classRoom = {
    'hemant': {grade: 30, gendar: 'Male'},
    'vinita': {grade: 30, gendar: 'Female'},
    'vinita': {grade: 30, gendar: 'Female'}
}

// To avoid above
let classRoom = {
    [Symbole('hemant')]: {grade: 30, gendar: 'Male'},
    [Symbole('vinita')]: {grade: 30, gendar: 'Female'},
    [Symbole('vinita')]: {grade: 30, gendar: 'Female'}
}

console.log(classRoom); // no console output

Access Symbol

let classRoom = {
    [Symbol('hemant')]: {grade: 30, gendar: 'Male'},
    [Symbol('vinita')]: {grade: 30, gendar: 'Female'},
    [Symbol('vinita')]: {grade: 30, gendar: 'Female'}
};


for(val in classRoom){
    console.log(val)
}

var rooms = Object.getOwnPropertySymbols(classRoom);
let data = rooms.map(item => classRoom[item]);
console.log(rooms); // [Symbol(hemant), Symbol(vinita), Symbol(vinita)]
console.log(data); 
// [{"grade":30,"gendar":"Male"},{"grade":30,"gendar":"Female"},{"grade":30,"gendar":"Female"}]
PreviousClassNextNew Data Structures

Last updated 4 years ago

Was this helpful?