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

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

The forum discussion centers around issues with JavaScript's localStorage functionality, specifically when using the window.localStorage.clear() method. The user initially faced a problem where the counter did not reset to zero after clearing localStorage. The solution involved ensuring that the global variable count was properly managed, particularly by setting count = 0 in the clearCounter function. The importance of using localStorage.getItem to retrieve the stored count value was also emphasized, as it allows for persistent state management across page reloads.

PREREQUISITES
  • Understanding of JavaScript variable scope (global vs local variables)
  • Familiarity with the localStorage API in JavaScript
  • Basic knowledge of DOM manipulation using JavaScript
  • Experience with debugging JavaScript in web browsers
NEXT STEPS
  • Learn how to use localStorage.getItem and localStorage.setItem effectively
  • Explore JavaScript variable scope and its implications on state management
  • Investigate best practices for using localStorage in web applications
  • Understand the differences between localStorage and sessionStorage in JavaScript
USEFUL FOR

Web developers, particularly those working with front-end JavaScript, anyone implementing state management in web applications, and developers seeking to understand localStorage behavior and debugging techniques.

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