When a button is clicked I check if something exists in a localstorage key as such:
var a = localStorage.getItem('foo');
if (typeof a != 'undefined') {
// Function
}
but if the key doesn’t exists at all, it return null. How can I call if not undefined and not null do function, else return true(?) or continue?
JavaScript has a concept of falsy values… i.e. 0,
null,undefinedand an empty string.Because of this, you should be able to just check if
ais “truthy” (i.e. not one of the values I’ve mentioned above), by doing this:More information from SitePoint available here