0

I have created a function for deleting all game data from the localStorage but when attempting to use the function in the chrome developer console it returns undefined cant seem to figure out why

EDIT: I forgot that return may confuse some veteran devs as to what I meant My point is in the console it says undefined after running the function HardReset() and nothing in the game is actually reset

EDIT: The main issue is the function isnt actually removing the items from the storage

/*
---------------------
Reset Entire Game
---------------------
*/

function HardReset(){
    localStorage.removeItem("money");
    localStorage.removeItem("level");
    localStorage.removeItem("exp");
    localStorage.removeItem("clickIncrement");
    localStorage.removeItem("clicksTotal");

    localStorage.removeItem("HTMLSupport");
    localStorage.removeItem("CSSSupport");

    localStorage.removeItem("HTMLCoderCost");
    localStorage.removeItem("HTMLCoderOwned");
    localStorage.removeItem("CSSCoderCost");
    localStorage.removeItem("CSSCoderOwned");
}
Garbee
  • 10,581
  • 5
  • 38
  • 41
  • `localStorage.removeItem` has [__`No return value`__](https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem#Returns) – Rayon Jul 09 '16 at 05:28
  • Your function doesn't have a `return` statement, why do you expect it to return something other than `undefined`? – Barmar Jul 09 '16 at 05:29
  • Which function are you calling in the dev console? `HardReset()` or `localStorage.removeItem()`? – Barmar Jul 09 '16 at 05:30
  • **My point is in the console it says undefined after running the function HardReset()**. Why is that a problem? Your function doesn't return anything, so that's what the console says. – Barmar Jul 09 '16 at 05:53
  • How are you determining that it's not actually removing the items from the storage? Can you make an [MCVE](http://stackoverflow.com/help/mcve) that demonstrates the real problem? – Barmar Jul 09 '16 at 05:55

3 Answers3

1

Instead of what you did you can just change to

function HardReset(){
        localStorage.clear();
}

localStorage.clear() will clear everything in your localStorage

and also prove a return value.

Kondas Lamar Jnr
  • 143
  • 1
  • 11
0

https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem

localStorage.removeItem has no return value.

Your function HardReset also has no return value.

What does javascript function return in the absence of a return statement?

Community
  • 1
  • 1
danjah
  • 2,939
  • 2
  • 30
  • 47
  • Even if it did return something, his `HardReset()` function isn't returning it. – Barmar Jul 09 '16 at 05:30
  • 2
    @Barmar No receipt, no returns. Store policy. – danjah Jul 09 '16 at 05:31
  • You may need to read the values again, and reflect them in your elements displaying state data. There are more efficient ways of doing this outside the scope of this question, but your expecttions of data state and the reflection of that state may need challenging. – danjah Jul 09 '16 at 05:39
0

Testing for support vs availability

Browsers that support localStorage will have a property on the window object named localStorage. However, for various reasons, just asserting that property exists may throw exceptions.

If it does exist, that is still no guarantee that localStorage is actually available, as various browsers offer settings that disable localStorage. So a browser may support localStorage, but not make it available to the scripts on the page.

One example of that is Safari, which in Private Browsing mode gives us an empty localStorage object with a quota of zero, effectively making it unusable. Our feature detect should take these scenarios into account.

Using the Web Storage API

Take a look at that example Feature-detecting localStorage section on page. Maybe the localStorage property is not available to the scripts on a page.

Barzev
  • 402
  • 3
  • 14
Ameya
  • 726
  • 6
  • 9