Header Ads

Database in JavaScript Using Local Storage

What is Local Storage Local Storage is an API provided by browsers that allows you to create,read,update and delete records in the browser. These records are limited to the browsers only,meaning data stored in chrome is not availble on firefox and vice versa.
Database in JavaScript Using Local Storage
Before We Start The local storage data is in JSON Format which means when storing the data, we need to convert the Javascript Object to a JSON format and vice versa. 
const person = { name:"w3technology" };
const personToJSON = JSON.stringify(person);

Visual View To view the local storage, follow the following steps:

 → Open Developer Tools
 → Go To Applications Tab 
 → Select Local Storage

Create New Record Here's how to create a new record in the local storage. 
const person = { name:"w3technology" }
const personToJSON = JSON.stringify(person);
localStorage.setItem("person",personToJSON);
Read a Record Here's how to read a record in the local storage. 
const personJSON = localStorage.getItem("person");
const JSONToPerson = JSON.parse(person.JSON);
console.log(JSONToPerson);

Update a Record Here's how to update an existing record in the local storage. 
const person = { name:"w3technology", age:33 }
const personToJSON = JSON.stringify(person);
localStorage.setItem("person",personToJSON);

Delete a Record Here's how to Delete an existing record in the local storage. 
localStorage.removeItem("person");

No comments:

Powered by Blogger.