Jay
Jay

Reputation: 3393

Check if localStorage isset or not

I want to check if localStorage is isset or not. e.g. i am creating sample localstrorage:

localStorage.setItem('TODO_LIST', 'value');

Now, i want to check on page load whether localStorage with name TODO_LIST is isset or not, also if it is isset how to check whether it blank or not.

Upvotes: 0

Views: 639

Answers (1)

Ex-iT
Ex-iT

Reputation: 1477

How about:

If (localStorage.getItem('TODO_LIST') && localStorage.getItem('TODO_LIST') !== '') {
    // Do something when it's set and not empty
}

localStorage.getItem('TODO_LIST') returns null or falsy when it's not set.

localStorage.getItem('TODO_LIST') !== '' returns true if it's not empty.

Upvotes: 2

Related Questions