Localstorage not getting cleared by window.localStorage.clear()

  • Context: JavaScript 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the behavior of JavaScript's localStorage in relation to a counter implementation. Participants explore issues related to variable scope, the effects of clearing localStorage, and the implications of using localStorage in their code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes their JavaScript code for a counter and notes that after clearing localStorage, the counter does not reset to zero as expected.
  • Another participant suggests that there may be multiple variables named count, which could lead to confusion about which variable is being modified.
  • Some participants point out that clearing localStorage does not affect the global variable count, which remains unchanged unless explicitly set to zero.
  • There is a discussion about the importance of using localStorage.getItem to retrieve the count value from localStorage, which may clarify the counter's behavior.
  • A later reply questions the necessity of using localStorage if the counter is reset in the code after clearing localStorage.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and functionality of localStorage in the context of the counter implementation. There is no consensus on the best approach or the implications of using localStorage.

Contextual Notes

Participants highlight potential misunderstandings regarding variable scope and the interaction between localStorage and JavaScript variables. There are unresolved questions about the purpose of retrieving values from localStorage and how it relates to the counter's functionality.

Who May Find This Useful

This discussion may be useful for developers working with JavaScript who are trying to understand localStorage, variable scope, and debugging techniques related to state management in web applications.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
localstorage not getting cleared by window.localStorage.clear()
Initial code is here:
https://www.makeuseof.com/localstorage-javascript/

It's the project that I'm trying to understand. It's the javascript that I'm concerned.I tweaked the Javascript code to this:

Code:
var count = 0;
function increaseCounter() {

  count += 1;
  window.localStorage.setItem("count", count);
  document.getElementById("score").innerHTML = count;
}
function decreaseCounter() {
  count -= 1;
  window.localStorage.setItem("count", count);
  document.getElementById("score").innerHTML = count;
}
function clearCounter() {
  window.localStorage.clear();
  document.getElementById("score").innerHTML = "";
}

This is what I've tweaked
HcEpMMKmSiNgkqRuALS1hK7SYV6IVnCFAORZMYpdnPV-Z0e8kg.png

I don't get the need for the commented line in this case. Why're we using it? I by now of course know it's needed. But can't understand why.

I'm facing a problem

1) I increase count to 13.
2) I clear localstorage.
3) I click decrease score. Now, score starts to decrease from 12. So, storage isn't being cleared right?
Why's it like that?

Dry run in the above code seems fine.

1) Initial count=0
2) Click increase count button
3) count=1
4) Again click increase count button
5) count=2
6) Now I clear Counter, count should be 0.
7) I click decrease counter
8) count=-1
and so on.

What am I missing here?
 
Technology news on Phys.org
shivajikobardan said:
What am I missing here?
You are missing the "javascript" language setting for your code box.

shivajikobardan said:
3) I click decrease score. Now, score starts to decrease from 12. So, storage isn't being cleared right?
Why do you think the value in localstorage is not being cleared? How could you find out?
 
It looks to me like there are three variables named count: the global one at the top of the code, and the other two local variables in the increaseCounter() and decreaseCounter() functions. By commenting out the first line of code in these two functions, these functions are now using the globally defined counter variable. If the same two lines are uncommented, the local count variables override the global one.
 
Simple, clearing the localStorage doesn't reset the variable count.

In your link, they use the function localStorage.getItem. Pretty important detail.
 
pbuk said:
Why do you think the value in localstorage is not being cleared? How could you find out?
open console Ctrl+Shift+J and go to Application=>Local Storage. See there.
 
shivajikobardan said:
open console Ctrl+Shift+J and go to Application=>Local Storage. See there.
Well I meant how could you find out in your code, but OK, what happens when you do look in the debug console?
localstorage IS being cleared so something else must be wrong
which others have already revealed
 
pbuk said:
Well I meant how could you find out in your code, but OK, what happens when you do look in the debug console?
localstorage IS being cleared so something else must be wrong
which others have already revealed
please read the question. I've explained everything there. count is not being decreased to 0.
 
shivajikobardan said:
please read the question. I've explained everything there.
You don't understand: I know exactly what you have done wrong, I am trying to help YOU to work out for yourself what it is. But others have told you what is wrong anyway and how to fix it.
 
pbuk said:
You don't understand: I know exactly what you have done wrong, I am trying to help YOU to work out for yourself what it is. But others have told you what is wrong anyway and how to fix it.
oh. count is not being decreased to 0 when local storage is being cleared.. that's the issue. I'm not getting that.
 
  • #10
Update:

This is my project for localStorage.

JavaScript:
var count = 0;
function increaseCounter() {
// var count = Number(window.localStorage.getItem("count"));
count += 1;
window.localStorage.setItem("count", count);
document.getElementById("score").innerHTML = count;
}
function decreaseCounter() {
// var count = Number(window.localStorage.getItem("count"));
count -= 1;
window.localStorage.setItem("count", count);
document.getElementById("score").innerHTML = count;
}
function clearCounter() {
window.localStorage.clear();
count = 0;
document.getElementById("score").innerHTML = "";
}

This code works now. But is there a point of using localstorage if I'm going to do like this? My goal for asking the question was to understand the first line.

JavaScript:
  // var count = Number(window.localStorage.getItem("count"));

I didn't get what we did here and why we did here.
 
  • #11
Why should anyone bother to help you if you can't be bothered to set the language to Javascript in your code blocks as you have been repeatedly asked?
 
  • #12
I've gave JS tag already. Still edited where I could edit.
 
  • #13
shivajikobardan said:
I've gave JS tag already. Still edited where I could edit.
Thank you.
shivajikobardan said:
This code works now. But is there a point of using localstorage if I'm going to do like this?
No, not if you are going to do it like this. But there is something different about a browser's localStorage from a variable in a script: what do you think this is?
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K