Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Using localStorage and sessionStorage | Storage, File Handling, and Geolocation
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Web APIs Essentials

bookUsing localStorage and sessionStorage

When you want to store data on the user's browser, HTML provides two important storage APIs: localStorage and sessionStorage. Both allow you to save key-value pairs as strings, but they differ in how long the data persists and when it is available.

localStorage saves data with no expiration time. This means that if you store something in localStorage, it will still be there even after the user closes and reopens the browser, or restarts their computer. This is useful for things like saving user preferences, themes, or login states that you want to remember across sessions.

sessionStorage, on the other hand, only keeps data for the duration of the page session. As soon as the user closes the browser tab or window, the data is cleared. sessionStorage is best used for temporary data that is only relevant while the user is on a specific page or during a single visit, such as wizard progress or temporary form data.

Both storage types are easy to use. You set an item with setItem(key, value) and get it back with getItem(key). Remember, all data is stored as strings, so you may need to use JSON.stringify and JSON.parse for objects or arrays.

index.html

index.html

script.js

script.js

copy
question mark

Which statement about localStorage and sessionStorage is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookUsing localStorage and sessionStorage

Swipe um das Menü anzuzeigen

When you want to store data on the user's browser, HTML provides two important storage APIs: localStorage and sessionStorage. Both allow you to save key-value pairs as strings, but they differ in how long the data persists and when it is available.

localStorage saves data with no expiration time. This means that if you store something in localStorage, it will still be there even after the user closes and reopens the browser, or restarts their computer. This is useful for things like saving user preferences, themes, or login states that you want to remember across sessions.

sessionStorage, on the other hand, only keeps data for the duration of the page session. As soon as the user closes the browser tab or window, the data is cleared. sessionStorage is best used for temporary data that is only relevant while the user is on a specific page or during a single visit, such as wizard progress or temporary form data.

Both storage types are easy to use. You set an item with setItem(key, value) and get it back with getItem(key). Remember, all data is stored as strings, so you may need to use JSON.stringify and JSON.parse for objects or arrays.

index.html

index.html

script.js

script.js

copy
question mark

Which statement about localStorage and sessionStorage is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1
some-alt