-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)?

5
  • There is also globalThis stackoverflow.com/questions/57157864/… but note lack of IE support Commented Jul 23, 2019 at 22:33
  • You can try typeof window and see if that's the string "undefined" Commented Jul 23, 2019 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. Commented Jul 23, 2019 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. Commented Jul 23, 2019 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". Commented Jul 23, 2019 at 22:39

2 Answers 2

2

You could use a try/catch statement.

try {
  if (!!someVariable) {
    console.log('yep');
  }
} catch {
  console.log('nope');
}

Sign up to request clarification or add additional context in comments.

7 Comments

Syntax errors would not be caught by that; try/catch is for runtime errors. Syntax errors happen before the code runs.
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
@Paulpro true. I just mirrored the code provided by OP
Could also do if("someVariable" in this) console.log('yep');. Should work based on scope.
@ug_ No... What is this supposed to be here? That would work only in the global/module scope, whereas this answer works everywhere.
|
-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
}

1 Comment

If it's null it "exists" inasmuch as it's null.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.