Tutorials References Exercises Videos Pro NEW Website NEW Get Certified Log in
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON PHP BOOTSTRAP HOW TO W3.CSS JAVA JQUERY C C++
DOM Collections
DOM Node Lists ADVERTISEMENT
JS Browser BOM
JS Window
JS Screen
JS Location
JS History
Web Storage API
JS Navigator
❮ Previous Next ❯
JS Popup Alert
JS Timing
JS Cookies The Web Storage API is a simple syntax for storing and retrieving data in the browser. It is very easy to use:
JS Web APIs
Web API Intro Example
Web Forms API
Web History API localStorage.setItem("name", "John Doe");
Web Storage API localStorage.getItem("name");
Web Worker API
Web Fetch API Try it Yourself »
Web Geolocation API
JS AJAX The Web Storage API is supported in all browsers:
Chrome IE/Edge Firefox Safari Opera
Yes Yes Yes Yes Yes NEW
We just launched
W3Schools videos
The localStorage Object
The localStorage object provides access to a local storage for a particular Web Site. It allows you to store, read, add,
modify, and delete data items for that domain.
The data is stored with no expiration date, and will not be deleted when the browser is closed.
Explore now
The data will be available for days, weeks, and years.
The setItem() Method COLOR PICKER
The localStorage.setItem() method stores a data item in a storage.
It takes a name and a value as parameters:
Example
localStorage.setItem("name", "John Doe"); Get certified
by completing
a JavaScript
course today!
The getItem() Method school
w3 s
The localStorage.getItem() method retrieves a data item from the storage.
2
It takes a name as parameter:
CE
02
TI 2
R
FI .
ED
Example Get started
localStorage.getItem("name");
CODE GAME
ADVERTISEMENT
The sessionStorage Object
The sessionStorage object is identical to the localStorage object.
Play Game
The difference is that the sessionStorage object stores data for one session.
The data is deleted when the browser is closed.
ADVERTISEMENT
Example
sessionStorage.getItem("name");
Try it Yourself »
The setItem() Method
The sessionStorage.setItem() method stores a data item in a storage.
It takes a name and a value as parameters:
Example
sessionStorage.setItem("name", "John Doe");
The getItem() Method
The sessionStorage.getItem() method retrieves a data item from the storage.
It takes a name as parameter:
Example
sessionStorage.getItem("name");
Storage Object Properties and Methods
Property/Method Description
key(n) Returns the name of the nth key in the storage
length Returns the number of data items stored in the Storage object
getItem(keyname) Returns the value of the specified key name
setItem(keyname, value) Adds that key to the storage, or update that key's value if it already exists
removeItem(keyname) Removes that key from the storage
clear() Empty all key out of the storage
Related Pages for Web Storage API
Property Description
window.localStorage Allows to save key/value pairs in a web browser. Stores the data with no expiration
date
window.sessionStorage Allows to save key/value pairs in a web browser. Stores the data for one session
❮ Previous Next ❯
ADVERTISEMENT
ADVERTISEMENT
Report Error Forum About Buy Certificate
Top Tutorials Top References Top Examples Web Courses
HTML Tutorial HTML Reference HTML Examples HTML Course
CSS Tutorial CSS Reference CSS Examples CSS Course
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Course
How To Tutorial SQL Reference How To Examples Front End Course
SQL Tutorial Python Reference SQL Examples SQL Course
Python Tutorial W3.CSS Reference Python Examples Python Course
W3.CSS Tutorial Bootstrap Reference W3.CSS Examples PHP Course
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Course
PHP Tutorial HTML Colors PHP Examples Java Course
Java Tutorial Java Reference Java Examples C++ Course
C++ Tutorial Angular Reference XML Examples C# Course
jQuery Tutorial jQuery Reference jQuery Examples XML Course
Get Certified »
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid
errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.