Is it possible to use ES6 Symbols in LocalStorageStoring a Symbol from React state in localStorage loads as “undefined”Storing Objects in HTML5 localStorageWhat is the max size of localStorage values?How do I store an array in localStorage?Clearing localStorage in javascript?Map vs Object in JavaScriptWhat is the motivation for bringing Symbols to ES6?ES6 class variable alternativesIs it possible to apply CSS to half of a character?Using Node.js require vs. ES6 import/exportWhen should I use curly braces for ES6 import?

Is there an idiom that means "accepting a bad business deal out of desperation"?

How can I minimize the damage of an unstable nuclear reactor to the surrounding area?

One word for 'the thing that attracts me'?

Gravitational Force Between Numbers

Why was this character made Grand Maester?

Are PMR446 walkie-talkies legal in Switzerland?

Visual Block Mode edit with sequential number

How did the Allies achieve air superiority on Sicily?

Are there historical examples of audiences drawn to a work that was "so bad it's good"?

Why did OJ Simpson's trial take 9 months?

Are cells guaranteed to get at least one mitochondrion when they divide?

Moons and messages

Count all vowels in string

These Two Cubes are The Only Ones That Are All Pure Prime..name them

What did the 'turbo' button actually do?

ifconfig shows UP while ip link shows DOWN

Goldfish unresponsive, what should I do?

Storing voxels for a voxel Engine in C++

Are there guidelines for finding good names for LaTeX 2e packages and control sequences defined in these packages?

Piping the output of comand columns

What is Orcus doing with Mind Flayers in the art on the last page of Volo's Guide to Monsters?

Why is 'additive' EQ more difficult to use than 'subtractive'?

How to escape dependency hell?

Can a multiclassed Kensei monk/Swashbuckler rogue use an offhand finesse weapon to trigger Sneak Attack, without using a bonus action?



Is it possible to use ES6 Symbols in LocalStorage


Storing a Symbol from React state in localStorage loads as “undefined”Storing Objects in HTML5 localStorageWhat is the max size of localStorage values?How do I store an array in localStorage?Clearing localStorage in javascript?Map vs Object in JavaScriptWhat is the motivation for bringing Symbols to ES6?ES6 class variable alternativesIs it possible to apply CSS to half of a character?Using Node.js require vs. ES6 import/exportWhen should I use curly braces for ES6 import?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















When using a ES6 Symbol as the key for a key/value pair in LocalStorage, can we then still access it after reloading the page?



I found this tutorial that claims this to be possible when using Symbol.for, but so far I have no success and get an undefined when I try to retrieve the LocalStorage key/value pair.



As a side question: does it make sense to use Symbols over here?










share|improve this question






















  • Can you share an example where it is not working? For example, on jsbin.com.

    – sdgluck
    Nov 13 '17 at 14:46


















3















When using a ES6 Symbol as the key for a key/value pair in LocalStorage, can we then still access it after reloading the page?



I found this tutorial that claims this to be possible when using Symbol.for, but so far I have no success and get an undefined when I try to retrieve the LocalStorage key/value pair.



As a side question: does it make sense to use Symbols over here?










share|improve this question






















  • Can you share an example where it is not working? For example, on jsbin.com.

    – sdgluck
    Nov 13 '17 at 14:46














3












3








3








When using a ES6 Symbol as the key for a key/value pair in LocalStorage, can we then still access it after reloading the page?



I found this tutorial that claims this to be possible when using Symbol.for, but so far I have no success and get an undefined when I try to retrieve the LocalStorage key/value pair.



As a side question: does it make sense to use Symbols over here?










share|improve this question














When using a ES6 Symbol as the key for a key/value pair in LocalStorage, can we then still access it after reloading the page?



I found this tutorial that claims this to be possible when using Symbol.for, but so far I have no success and get an undefined when I try to retrieve the LocalStorage key/value pair.



As a side question: does it make sense to use Symbols over here?







javascript ecmascript-6 local-storage






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '17 at 11:56









sjbuyssesjbuysse

1,0312820




1,0312820












  • Can you share an example where it is not working? For example, on jsbin.com.

    – sdgluck
    Nov 13 '17 at 14:46


















  • Can you share an example where it is not working? For example, on jsbin.com.

    – sdgluck
    Nov 13 '17 at 14:46

















Can you share an example where it is not working? For example, on jsbin.com.

– sdgluck
Nov 13 '17 at 14:46






Can you share an example where it is not working? For example, on jsbin.com.

– sdgluck
Nov 13 '17 at 14:46













1 Answer
1






active

oldest

votes


















3














You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem().



Unfortunately localStorage doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...



You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString() representation:



const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))


[ Check it out on jsbin.com ]




As a side question: does it make sense to use Symbols over here?




I suppose you could use reserved Symbols (e.g. Symbol.iterator) to extend the functionality of the localStorage global. But that's kind of besides the point.






share|improve this answer

























  • So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?

    – sjbuysse
    Nov 13 '17 at 15:20






  • 1





    @sjbuysse None. Which is why you should not do this.

    – Bergi
    Nov 13 '17 at 15:57






  • 3





    @sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.

    – sdgluck
    Nov 13 '17 at 16:00












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47263812%2fis-it-possible-to-use-es6-symbols-in-localstorage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem().



Unfortunately localStorage doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...



You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString() representation:



const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))


[ Check it out on jsbin.com ]




As a side question: does it make sense to use Symbols over here?




I suppose you could use reserved Symbols (e.g. Symbol.iterator) to extend the functionality of the localStorage global. But that's kind of besides the point.






share|improve this answer

























  • So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?

    – sjbuysse
    Nov 13 '17 at 15:20






  • 1





    @sjbuysse None. Which is why you should not do this.

    – Bergi
    Nov 13 '17 at 15:57






  • 3





    @sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.

    – sdgluck
    Nov 13 '17 at 16:00
















3














You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem().



Unfortunately localStorage doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...



You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString() representation:



const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))


[ Check it out on jsbin.com ]




As a side question: does it make sense to use Symbols over here?




I suppose you could use reserved Symbols (e.g. Symbol.iterator) to extend the functionality of the localStorage global. But that's kind of besides the point.






share|improve this answer

























  • So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?

    – sjbuysse
    Nov 13 '17 at 15:20






  • 1





    @sjbuysse None. Which is why you should not do this.

    – Bergi
    Nov 13 '17 at 15:57






  • 3





    @sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.

    – sdgluck
    Nov 13 '17 at 16:00














3












3








3







You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem().



Unfortunately localStorage doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...



You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString() representation:



const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))


[ Check it out on jsbin.com ]




As a side question: does it make sense to use Symbols over here?




I suppose you could use reserved Symbols (e.g. Symbol.iterator) to extend the functionality of the localStorage global. But that's kind of besides the point.






share|improve this answer















You can use Symbols as keys in an object - that's ultimately their purpose. But localStorage is not your typical object. It has an API to set/get values in the store, which the tutorial you have shared is not using: localStorage.set,getItem().



Unfortunately localStorage doesn't accept Symbols as keys, only strings. So the real answer is no, you can't use Symbols as keys in LocalStorage, but...



You could do the following. It isn't actually using a Symbol as the key, but the Symbols toString() representation:



const sym = Symbol.for('Hello').toString()
localStorage.setItem(sym, 'World')
document.write('Hello, ' + localStorage.getItem(sym))


[ Check it out on jsbin.com ]




As a side question: does it make sense to use Symbols over here?




I suppose you could use reserved Symbols (e.g. Symbol.iterator) to extend the functionality of the localStorage global. But that's kind of besides the point.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '17 at 17:25

























answered Nov 13 '17 at 14:49









sdglucksdgluck

10.6k13051




10.6k13051












  • So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?

    – sjbuysse
    Nov 13 '17 at 15:20






  • 1





    @sjbuysse None. Which is why you should not do this.

    – Bergi
    Nov 13 '17 at 15:57






  • 3





    @sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.

    – sdgluck
    Nov 13 '17 at 16:00


















  • So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?

    – sjbuysse
    Nov 13 '17 at 15:20






  • 1





    @sjbuysse None. Which is why you should not do this.

    – Bergi
    Nov 13 '17 at 15:57






  • 3





    @sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.

    – sdgluck
    Nov 13 '17 at 16:00

















So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?

– sjbuysse
Nov 13 '17 at 15:20





So, this means you'd be saving the value under a key that is a string that says "Symbol(isShown)"? Then what's the advantage of using this in stead of just a key that says "isShown"?

– sjbuysse
Nov 13 '17 at 15:20




1




1





@sjbuysse None. Which is why you should not do this.

– Bergi
Nov 13 '17 at 15:57





@sjbuysse None. Which is why you should not do this.

– Bergi
Nov 13 '17 at 15:57




3




3





@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.

– sdgluck
Nov 13 '17 at 16:00






@sjbuysse There is no advantage, but you might be using Symbols elsewhere in objects in your application that you want to then use in LocalStorage too for consistency.

– sdgluck
Nov 13 '17 at 16:00




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47263812%2fis-it-possible-to-use-es6-symbols-in-localstorage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript