Skip to content Skip to sidebar Skip to footer

How Can I Store And Retrieve Many Javascript Primitives In And From Local Storage

My application has the following Javascript primatives: var bearerToken = 'a'; var expirationDate = 'b'; var firstName = 'c'; var authenticated = true; var lastName = 'd'; var logi

Solution 1:

Make an object and store them on the same object

var someObj = {lastName:'test',firstname:'joe'};

Just add all the properties to the object like above.

You can store it as a json string

localStorage.setItem('someName', JSON.stringify(someObj));

Solution 2:

The easiest way would be to use JSON.stringify and store the object as a string. Use JSON.parse to retrieve it:

var o = {
    bearerToken: 'Foo',
    authenticate: true,
    subjectId: 3
}
localStorage.setItem('myData', JSON.stringify(o));
console.log(JSON.parse(localStorage.getItem('myData')));

Solution 3:

localStorage.setItem("bearerToken","") will help you to save an item in the local storage. localStorage.getItem("bearerToken") will retrieve that value localStorage.removeItem("bearerToken") will remove that item.

Post a Comment for "How Can I Store And Retrieve Many Javascript Primitives In And From Local Storage"