Categories
JavaScript Answers

How to fix referenceerror: localstorage is not defined with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.