Free Ebook cover How to use Javascript in website development

How to use Javascript in website development

5

(2)

37 pages

Data storage with LocalStorage in Javascript

Capítulo 15

Estimated reading time: 1 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The purpose of LocalStorage in Javascript is to store data in the user's browser. This allows web applications to save data on the client side, enabling persistent data storage even when the browser is closed or the computer is restarted, as described in the text.

Next chapter

Javascript game development

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.