Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Use Javascript's Local Storage to Save Information
MikeJMS8910 (234)

How to use Javascript's Local Storage

What is Local Storage?

Javascript's local storage is a free database to use. This can save data in keys similar to something like repl's DataBase.

What can it be used for?

Local Storage can save information so that when a program is stopped you can get the data back. You can also use it for things like a log-in system.

How to Make a Key

A key is a thing that has data saved to it. Accessing the key will give you the data. To make a key and have data saved to it you will have something like this:

`localStorage.setItem('Key', 'Value');

In this case, we are making a key called 'Key' and giving it a value of 'Value'. You are able to replace these with variables too.

How to Access a Key

Now if we have a value saved to a key we can access the value of the key.

const value = localStorage.getItem('Key');

The variable 'value' will now have the value 'Value' since of the key that we used. We can test this by printing it.

'console.log(value);'

The output should say 'Value' or whatever you made the Value for the Key.

How to Delete a Key

Let's say that you made a key by accident or something else and you want to get rid of a key.

localStorage.removeItem('Key');

This will now delete the key from the database.

Extra Information

  • Local storage can only save 5MB of information

  • Information is only saved on Devices so if you save something on one device it will not be accessible on another

  • Altho Local Storage is easy to use it isn't very secure and shouldn't be used to save important information

Comments
hotnewtop
ch1ck3n (2335)

it's not very secure, and you can't log in on another device if you use localstorage.

MikeJMS8910 (234)

@ch1ck3n yea I put that it cant be logged in another device in extra information ill add the not very secure too

ch1ck3n (2335)

@MikeJMS8910 So its not useful for a log-in system

MikeJMS8910 (234)

@ch1ck3n I would still use it for repl because it doesn't matter to much but for a professional website then no

Coder100 (18882)

nah, the storage space is browser-dependent