Asked 7 years ago
11 Jan 2017
Views 1367
yogi

yogi posted

what is symbol in JavaScript ?

use of Symbol in JavaScript ?
what is use of Symbol in JavaScript ?

const evy = Symbol()


what is meaning of Symbol in JavaScript?
how to use Symbol in JavaScript ?
is there any practical use of Symbol in JavaScript ? is Symbol keyword , operator or datatype ?
Symbol is identifiers that are used to refer to something else . it created to use as indentifier - Rasi  
Jan 13 '17 05:48
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

in Javascript Symbol is primitive Data Type
is immutable
is always unique.
unique means

Symbol("$") !== Symbol("$")


Symbols is work like identifiers , it uniquely identify and its like constant means it never change.
good definition - shyam  
Jan 13 '17 04:31
shyam

shyam
answered Nov 30 '-1 00:00

what is use of Symbol in JavaScript ?
Symbol is always be unique . we can use it where we need immutable and unique datatype . its good identifiers
one can say its uniquer number and we can use it anywhere we need unique identifier
suppose

var myKey = Symbol();

Symbol will create unique new Symbol which never match with other Symbol

var Key1 = Symbol();
var Key2 = Symbol();
var obj ={};
obj[Key1]="Shyam";
obj[Key2]="Ram";
console.log(obj[Key1]); // Ram
console.log(obj[Key2]); // Shyam
console.log(Key2==Key1); // false

Symbol work like private key to every property for object in JavaScript
one can use it as key to identify , and one Symbol never match with other Symbol Key2==Key1 is false , so no duplication for any key identifier with Symbol in JavaScript

So Symbol uses as identifier in JavaScript and one identifier never match with other , so no overwrite happen .

Why not Use String as identifier instead of Symbol
Symbol vs String

mostly we use string as identifier to object property or array property .

var player={};
player["isRunning"]=true;


it cant be always unique and possibly sometime it goes duplicate or overwrite happen so make it more unique string should be hash or long digit sequence so chances to match with other goes low . but again its not human readable .

Symbol solve that problem human readable constant which never match with other , always be unique


var player={};
player[Symbol("isRunning")]=true;



Symbol is one type unique constant which not match itself so it can be used as identifier

Rasi

Rasi
answered Nov 30 '-1 00:00

Symbol used to make Private properties in JavaScript
lets define a class and try to make its private property and see how it work

var Product = (function() {
    var  name;
    function Product(name) {
        this.name = name;
    }
    Product.prototype.getName = function() {
        return this.name;
    };

    return Product;
}()); 
var product = Product ('Apple');
console.log(product.getName());//Apple
delete product.name;//true
console.log(product.getName());//undefined


delete product.name -- delete the property name of object . so its very good accessible from outside so we can not say its private property of obejct.

lets use Symbol to make private property of object


var Product= (function() {
    var nameSymbol = Symbol('name');

    function Product(name) {
        this[nameSymbol] = name;
    }

    Product.prototype.getName = function() {
        return this[nameSymbol];
    };

    return Product;
}());
var product = Product ('Apple');
console.log(product.getName());//Apple
delete product.name;//true
console.log(product.getName());//Apple


when we use Symbol we cant able to delete property product.name or delete product.nameSymbol (try to delete Symbol ) but still we get result at product.getName()

Note :: use [Symbol] to get or set the Symbol at object
Post Answer