-1

Please read the question before marking duplicate. This isn't about an undefined variable. It's about variables which don't exist in the namespace.

I'm working on a codebase which is run in browser and in nativescript. The globals differ between the two. For instance, I'd like to check if window exists at all, but something like this:

if (!!window) {
}

will throw an error:

JS ERROR ReferenceError: Can't find variable: window

Is there a way to test whether or not a variable exists in js (not just undefined)?

Seph Reed
  • 8,797
  • 11
  • 60
  • 125
  • There is also `globalThis` https://stackoverflow.com/questions/57157864/what-is-globalthis-in-javascript-what-will-be-the-ideal-use-case-for-this but note lack of IE support – charlietfl Jul 23 '19 at 22:33
  • You can try `typeof window` and see if that's the string `"undefined"` – Pointy Jul 23 '19 at 22:33
  • You'll have to explain the difference between `typeof(window)` being `"undefined"` and what you want here. In any valid browser environment `window` will not be undefined, it'll be something else. JavaScript has limited reflection capabilities and there's not really a difference between "undefined" as in declared with no value and "undefined" as in does not exist. – tadman Jul 23 '19 at 22:35
  • 1
    Your question specifically says "run in browser", so that part implies you want to detect the presence of `window` which `typeof` will do. You could try `this['window']` if your code is running in the global context. In a browser that returns something, in Node, for example, it does not. – tadman Jul 23 '19 at 22:36
  • Yes, I am reading your question and trying to explain *exactly why `typeof` is the answer*. Can you explain why that's not appropriate in your use case? I can't see why not, so you'll need to fill in the gaps. This is precisely what `typeof` is for: Finding out if variables exist or not. In the browser it *will* be defined. In a non-browser environment it probably will not, and will return `"undefined"`. – tadman Jul 23 '19 at 22:39

2 Answers2

2

You could use a try/catch statement.

try {
  if (!!someVariable) {
    console.log('yep');
  }
} catch {
  console.log('nope');
}
Olian04
  • 6,480
  • 2
  • 27
  • 54
  • 1
    Syntax errors would *not* be caught by that; `try/catch` is for *runtime* errors. Syntax errors happen before the code runs. – Pointy Jul 23 '19 at 22:32
  • Why the if statement? You don't log anything for variables that are defined with a falsy value. You could just use `someVariable; console.log( 'yep' );` as the body of the try block – Paul Jul 23 '19 at 22:35
  • @Paulpro true. I just mirrored the code provided by OP – Olian04 Jul 23 '19 at 22:36
  • Could also do `if("someVariable" in this) console.log('yep');`. Should work based on scope. – ug_ Jul 23 '19 at 22:37
  • @ug_ No... What is `this` supposed to be here? That would work only in the global/module scope, whereas this answer works everywhere. – Paul Jul 23 '19 at 22:38
  • @Paulpro the scope in which your checking.. In this case global scope. In the browsers case its `window` in node its `global` – ug_ Jul 23 '19 at 22:40
  • @ug_ but it doesn't work in any other scope, like inside a function, whereas this answer works everywhere... – Paul Jul 23 '19 at 22:40
-2

You need to use the typeof operator to see if a variable exist or no you need to test if the variable you are concerned with is undefined of null like so :

if (typeof variable === 'undefined' || variable === null) {
    // variable does not exist
}
DerDjamel
  • 104
  • 1
  • 6