Skip to content
Advertisement

How do I use localStorage to remember a user’s name

This code has been checked for errors and none were detected. But, when it runs, it alerts null.

For Sololearners my code bit: https://code.sololearn.com/WOv1cF0EewdB/?ref=app

Why is this?*

JavaScript

*jQuery answers are OK

Advertisement

Answer

You are not getting a hello alert is because you are checking getItem‘s return value with === true and !== true.

setItem will convert your value to string, so localStorage.getItem('visit') === true will never be true

If you want to check if the user has visited the page, you should use === "true"(visited) and !== "true"(not visited) since it returns string.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement