0

I know that's probably very confusing, but what I want is to get the value of a localstorage, without setting it like:

var cookies = localStorage.totalCookies

I'd want it to be like this:

var cookies = value of localstorage.totalCookies

I want this so that the var cookies doesn't equal localstorage as I want a one time change to the var cookies, not a constant localstorage updating.

  • https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem – Will Aug 03 '16 at 20:50
  • What did you save in `localStorage.totalCookies`? – trincot Aug 03 '16 at 20:52
  • Just curious, but unless we're talking about actual cookies, preferably chocolate chip, why are you storing or reading cookie data in/from `localStorage` rather than using cookies? – War10ck Aug 03 '16 at 20:58
  • This appears to work out of the box, which I would expect since you're getting a string value and not an object reference. See [this example](https://jsfiddle.net/warlock5658/umvbs1aL/2/). Can you post an example of when this doesn't occur? – War10ck Aug 03 '16 at 21:02
  • @War10ck It's a cookie clicker clone. – Felix O'Brien Aug 04 '16 at 06:49
  • @trincot The amount of cookies someone has saved locally. – Felix O'Brien Aug 04 '16 at 06:49
  • 1
    That is crucial information to understand your problem. Maybe you could edit your question, and add that? – trincot Aug 04 '16 at 07:38

2 Answers2

2

The data type of what is stored in localStorage is always string, even if you wrote a numerical value to it.

To get back the numerical value, you just need to coerce it back to a number, for instance with the unitary +. You should also provide a default value for when the entry is not present in localStorage. If that default value is 0, then you can write it like this:

var cookies = +localStorage.totalCookies || 0;

Or, if you want to test for the presence of that entry, and do another test on whether it is numerical:

if (localStorage.totalCookies === undefined) {
    console.log('The totalCookies entry was not found');
    // etc..
} else {
    var cookies = +localStorage.totalCookies;
    if (isNaN(cookies)) {
        console.log('The totalCookies entry is not numerical');
        // etc...
    } 
}
trincot
  • 317,000
  • 35
  • 244
  • 286
1

This is happening because when you declare a variable to be an object, you're creating a reference to the object, not a duplicate of it. You can use the get & set methods of localStorage, as mentioned by War10ck in the comments above:

Get: localStorage.getItem('totalCookies') Set: localStorage.setItem('totalCookies', 'some value') Remove: localStorage.removeItem('totalCookies')

But in general, you need to find a way to clone an object to prevent subsequent tampering with the original. Here's a quick way:

var cloneOfA = JSON.parse(JSON.stringify(a));

You can see a larger discussion about this here: How do I correctly clone a JavaScript object?

Good Idea
  • 2,481
  • 3
  • 18
  • 25
  • What happens is there is a timer event, that adds the value of Autoclick to the totalCookies. But when I use: getItem, or setItem every second it adds the value of the autoclick like so: var autoclick = 0; Total Cookies: 120, Next Second: Total Cookies: 1200, Next second Total Cookies: 12000. Why does it do that? – Felix O'Brien Aug 04 '16 at 07:00
  • It looks like you might be adding strings to strings instead of integers to integers. You'll need to use `parseInt()` to make sure javascript knows to treat it as an integer. Try: `var totalCookies = 0;` `totalCookies = totalCookies + parseInt(newCookie)` – Good Idea Aug 05 '16 at 18:43
  • Or see trincot's smarter answer above! – Good Idea Aug 05 '16 at 18:44