Data storage with LocalStorage in Javascript

Página 15

Javascript is one of the most used programming languages ​​in the development of web sites. One of the features that can be used is data storage with LocalStorage.

LocalStorage is an API (Application Programming Interface) that allows you to store data in the user's browser. This data is available even after closing the browser or restarting the computer.

To use LocalStorage in Javascript, it is necessary to use the setItem() and getItem() methods. The setItem() method is used to store a value in LocalStorage. It takes two parameters: the key name and the value to be stored.

For example, to store the value "John" in the key "name", the code would be:

localStorage.setItem("name", "John");

To retrieve the stored value, use the getItem() method, which receives the key name as a parameter.

var name = localStorage.getItem("name");

In addition, you can use the removeItem() method to remove an item from LocalStorage and the clear() method to clear all stored items.

It is important to remember that LocalStorage has a storage limit of about 5 to 10 MB, depending on the browser used. Therefore, it is recommended to use LocalStorage to store small amounts of data, such as user settings and preferences.

In summary, storing data with LocalStorage in Javascript is a useful feature for web site development. With it, it is possible to store data in the user's browser persistently and access them later. It is important to use LocalStorage carefully, respecting its storage limits and avoiding storing sensitive information.

Now answer the exercise about the content:

_What is the purpose of LocalStorage in Javascript?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

16Javascript game development

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text