Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Storing Data with localStorage | Working with JSON in JavaScript
Working with JSON and Local Storage in JavaScript

bookStoring Data with localStorage

index.js

index.js

copy

When you want to keep data in the browser so that it stays even after the page is refreshed, you can use the localStorage API. This API is built into all modern browsers and provides a way to store key-value pairs as strings. The two most commonly used methods are setItem and getItem.

The setItem method saves data in localStorage. It takes two arguments: the key (a string you choose) and the value (also a string). In the example above, "username" is the key and "alice" is the value being saved.

To get the stored value back, use the getItem method with the key you used. This returns the value as a string, or null if the key does not exist. In the code example, getItem("username") returns the string "alice".

Remember, localStorage only stores data as strings. If you want to store other types of data, like objects or arrays, you need to convert them to a string first—usually with JSON.stringify. For now, saving and retrieving strings is simple and direct with these methods.

1. Which method is used to save data in localStorage?

2. What type of data can localStorage store directly?

question mark

Which method is used to save data in localStorage?

Select the correct answer

question mark

What type of data can localStorage store directly?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you show me an example of how to use localStorage in JavaScript?

How do I remove an item from localStorage?

What are the limitations or size limits of localStorage?

Awesome!

Completion rate improved to 7.69

bookStoring Data with localStorage

Sveip for å vise menyen

index.js

index.js

copy

When you want to keep data in the browser so that it stays even after the page is refreshed, you can use the localStorage API. This API is built into all modern browsers and provides a way to store key-value pairs as strings. The two most commonly used methods are setItem and getItem.

The setItem method saves data in localStorage. It takes two arguments: the key (a string you choose) and the value (also a string). In the example above, "username" is the key and "alice" is the value being saved.

To get the stored value back, use the getItem method with the key you used. This returns the value as a string, or null if the key does not exist. In the code example, getItem("username") returns the string "alice".

Remember, localStorage only stores data as strings. If you want to store other types of data, like objects or arrays, you need to convert them to a string first—usually with JSON.stringify. For now, saving and retrieving strings is simple and direct with these methods.

1. Which method is used to save data in localStorage?

2. What type of data can localStorage store directly?

question mark

Which method is used to save data in localStorage?

Select the correct answer

question mark

What type of data can localStorage store directly?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt