Set var for typeof

How to save for a var like:
typeof(Storage) !== "undefined"

if(typeof(Storage) !== "undefined") {
  console.log("Local storage is supported.");
  // Local storage is available on your browser
}

Something like?

if(thevar) {
  console.log("Local storage is supported.");
  // Local storage is available on your browser
}

Thanks!

Can you explain your question a little more? I’m not sure what you’re asking for help with.

How to make a var with typeof(Storage) !== “undefined”?

Something like var storage123 = typeof(Storage) !== "undefined";?

If you are asking how to make a variable that is undefined, then you can declare the variable without assigning a value:

var Storage;

Like I want to check if there support local storage with

if(typeof(Storage) !== "undefined") {
  console.log("Local storage is supported.");
  // Local storage is available on your browser
}

So in check in later in code i need to set again this?
Any shorthand for this?

if(typeof(Storage) !== "undefined") {
}

To run if local storage supported? Is not can set for a var for it? to call?

Are you trying to make a variable undefined after it already has a value?

1 Like

You can make a function that checks if you can set and remove an item without throwing and return true or false (feature testing as opposed to feature detection).

Taken from Modernizr and here is a refactored version.

function hasStorage() {
  try {
    localStorage.setItem('localStorage', 'works');
    localStorage.removeItem('localStorage');
    return true;
  } catch (e) {
    return false;
  }
}
if (hasStorage()) {
  console.log('Has support for localStorage');
} else {
  console.log('Do not support localStorage');
}

You can test it in the console using a blank tab in the browser about:blank, that should throw.

1 Like