Skip to content
Sign upLog in
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
1

How do I save data?

ComputingSquid
ComputingSquid

I'm am kind of confused, I Do not know how to correctly set something as a number and then make it check if the user has visited and then not reset the variables. I am using local storage, but I don't know what I should use.

2 years ago

Voters

Comments

TopNew
1
Coder100
Coder100

Hi, localstorage saves everything as a string.

So for you:

if (!localStorage.getItem("score")) localStorage.setItem("score", "0") let score = Number(localStorage.getItem("score"));

And now, each time you change score, you will have to save it:

score ++; localStorage.setItem("score", score)

Links:
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
https://www.w3schools.com/jsref/prop_win_localstorage.asp

2 years ago
1
ComputingSquid
ComputingSquid

How do i Save as number?

@Coder100

2 years ago
1
Coder100
Coder100

didn't I tell you that? You will just have to turn the value you get from localstorage into a number

@ComputingSquid

2 years ago
1
Coder100
Coder100

that turns it into a number

@ComputingSquid

2 years ago
1
potatojs
potatojs

wait what? i just save it as a number i use it all the time


@Coder100

2 years ago
1
Coder100
Coder100

well secretly it gets coerced to type string because of js magic

@potatojs

2 years ago
1
Coder100
Coder100

if you read the type definition, it says setItem(item: string, value: string): void

@potatojs

2 years ago
1
Coder100
Coder100

@potatojs

image

Well not exactly but you know what i mean

2 years ago
1
potatojs
potatojs

oh wow js is the best lang


@Coder100

2 years ago
1
PYer
PYer

You can try using cookies: https://www.w3schools.com/js/js_cookies.asp
document.cookie = "hasvisited=1";

2 years ago
1
ComputingSquid
ComputingSquid

Yes, buy I am using local storage because ad blockers erase cookies way to much

@PattanAhmed
@PYer

2 years ago
1
ComputingSquid
ComputingSquid

Just wondering, does clear cached images and files erase local storage?

@PattanAhmed

2 years ago
1
PattanAhmed
PattanAhmed

@ComputingSquid
Oh!
Clearing cache has no effect on HTML5 localStorage or sessionStorage (but clearing cookies does!) ... If you navigate to this page in a new window or tab, or quit and relaunch your browser and come back, localStorage will remain and sessionStorage will disappear.

Reference:-

01

Hope this helps

2 years ago
1
PattanAhmed
PattanAhmed

@ComputingSquid
Hi,
You can use Databases...
Variables...
And etc.
Check this website to know more:-
Click here
That's it.
Thanks!

Hope this helps

2 years ago
Load more