To fix referenceerror: localstorage is not defined with JavaScript, we should make sure we spell it correctly and only try to use it in the browser environment.
For instance, we write
const foo = localStorage.getItem("foo");
to call localStorage.getItem with 'foo' to return the value of the local storage entry with key 'foo'.
And we write
localStorage.setItem("foo", 1);
to call setItem to set the item with key 'foo' to value 1.
And we write
localStorage.removeItem("foo");
to call removeItem to remove the item with key 'key'.
We should only try to use these methods in code that runs in the browser.