-
Notifications
You must be signed in to change notification settings - Fork 2
Browser Storage
In order to manage data handled by your web application, you do not necessarily need a database. The respective Browser Storage features are supported by Chrome (version 4 and higher), Mozilla Firefox (version 3.5 and higher) and Internet Explorer (version 8 and higher), and a range of other browsers including those of iOS and Android.
There are two main possibilities for browser storage:
Any content/data saved to the localStorage
object will be available after the browser has been restarted (closed and opened again). In order to save an item to localStorage
, you can use the method setItem()
. This method must be handed a key and a value.
Example: localStorage.setItem("mykey","myvalue");
To retrieve the item from the localStorage, the method getItem
must be used. The getItem
method must be handed the key of the data you would like to retrieve:
Example: localStorage.getItem("mykey");
You can remove an item from localStorage
by using the removeItem()
method. This method must be handed the key of the item to be removed:
Example: localStorage.removeItem("mykey");
To clear the entire localStorage
, you should use the clear()
method on the localStorage
object:
Example: localStorage.clear();
Items saved in the sessionStorage
object will remain until the browser is closed by the user. Then, the storage will be cleared.
You can save an item to sessionStorage
, please use the method setItem()
on the sessionStorage
object:
Example: sessionStorage.setItem("mykey","myvalue");
To retrieve the item from the sessionStorage, the method getItem
must be used. The getItem
method must be handed the key of the data you would like to retrieve:
Example: sessionStorage.getItem("mykey");
You can remove an item from sessionStorage
by using the removeItem()
method. This method must be handed the key of the item to be removed:
Example: sessionStorage.removeItem("mykey");
To clear the entire sessionStorage
, you should use the clear()
method on the sessionStorage
object:
Example: sessionStorage.clear();
You cannot just save single values to the localStorage
and sessionStorage
, but you can also save the content of an array.
In this example, we have an array with numbers:
var ourArray =[1,2,3,4,5];
We can now save it to localStorage
or sessionStorage
using the setItem()
method:
localStorage.setItem("ourarraykey",JSON.stringify(ourArray));
or, for sessionStorage
:
sessionStorage.setItem("ourarraykey",JSON.stringify(ourArray));
In order to be saved, the array must first be converted to a string. In the example shown above, we are using the JSON.stringify
method to accomplish this.
When retrieving our data from the localStorage
or sessionStorage
, convert it back to an array:
var storedArray = localStorage.getItem("ourarraykey");
ourArray = JSON.parse(storedArray);
or, for sessionStorage
:
var storedArray = sessionStorage.getItem("ourarraykey");
ourArray = JSON.parse(storedArray);
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links