Ok. I understand when we are setting the data in our Local server the data must be in a string. So we use JSON.stringify to convert an object to a string. But I just don't understand why the converted string needs to be converted BACK to an object when we want to retrieve it out of the local server to print them on the page? Could anyone please explain to me what exactly JSON.parse does and WHEN we need to use it?
if (user.firstName === "") {
displayMessage("error", "First name cannot be blank");
} else if (user.lastName === "") {
displayMessage("error", "Last name cannot be blank");
} else if (user.email === "") {
displayMessage("error", "Email cannot be blank");
} else if (user.password === "") {
displayMessage("error", "Password cannot be blank");
} else {
displayMessage("success", "Registered successfully");
// set new submission
localStorage.setItem("user", JSON.stringify(user));
// get most recent submission
var lastUser = JSON.parse(localStorage.getItem("user"));
userFirstNameSpan.textContent = lastUser.firstName;
userLastNameSpan.textContent = lastUser.lastName;
userEmailSpan.textContent = lastUser.email;
userPasswordSpan.textContent = lastUser.password;
}
});