Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Storing JavaScript Objects in HTML5 localStorage</title> </head> <body> <script> var personObject = { name: "Peter", age: 18, married: false }; // Convert the person object into JSON string and save it into storage localStorage.setItem("personObject", JSON.stringify(personObject)); // Retrieve the JSON string var jsonString = localStorage.getItem("personObject"); // Parse the JSON string back to JS object var retrievedObject = JSON.parse(jsonString); console.log(retrievedObject); // Accessing individual values document.write(retrievedObject.name + "<br>"); // Prints: Peter document.write(retrievedObject.age + "<br>"); // Prints: 18 document.write(retrievedObject.married); // Prints: false </script> <p><strong>Note:</strong> Please check out the browser console by pressing the f12 key on the keyboard.</p> </body> </html>