JSON.stringify JSON.parse Javascript
- What is
JSON.stringify()?
- It is a function that takes an object as input and converts it into a string & returns it.
- What is
JSON.parse()?
- It is a function that takes a string as input and converts it into an object & returns it.
const user = {
name: "Chaitanya",
age: 22,
};
// converting object to string
const strObj = JSON.stringify(user);
// converting string back to object
console.log(JSON.parse(strObj));
// usecase
localStorage.setItem("test", strObj);
console.log(JSON.parse(localStorage.getItem("test")));