90

Possible Duplicate:
Detecting an undefined object property in JavaScript

From the below JavaScript sample,

try {
    if(jsVar) {
        proceed();
    }
}
catch(e) {
    alert(e);
}

this jsVar is declared and initialized in another file.

The problem is that code throws undefined error when this code is executed before the other file (where its declared and initialized) is executed. That is why it is surrounded by try and catch.

What's the best way to handle this undefined error than try catch?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Madhu
  • 5,686
  • 9
  • 37
  • 53
  • 1
    I don't get it, How come even after marked as duplicate this question still got 48 up votes. Isn't it's lack of judgement and decreases quality of these up votes. And then voting might be broke if duplicate questions get up votes. – jyotibisht Oct 19 '18 at 22:34

3 Answers3

119

You can check the fact with

if (typeof jsVar == 'undefined') {
  ...
}
Patrick Hollweck
  • 5,604
  • 2
  • 19
  • 34
alex.zherdev
  • 23,914
  • 8
  • 62
  • 56
  • Does this have any advantage over "my" way? – Jan Hančič Dec 31 '09 at 09:54
  • It may throw an error: jsVar is not defined. You should test it this way : if (window.jsVar !== undefined) {}. It seems like typeof() "catches" this error. – Fabien Ménager Dec 31 '09 at 10:15
  • @Fabien: Or perhaps use if("jsVar" in window){}... but I would avoid code that would require coding for this error case. – Thomas Eding Jan 01 '10 at 09:51
  • @logic-unit that is in theory better, but `==` is enough for a `typeof` comparission, since there are only a [few possible values](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) that can be returned. – berbt May 09 '14 at 20:00
  • You can use the typeof to determine the type of the unevaluated operand. It will return the type of operand in a string. You can use "===", if you know they are equal value and equal type. – DevBert Jul 30 '15 at 09:31
15

As is often the case with JavaScript, there are multiple ways to do this:

typeof foo !== 'undefined'
window.foo !== undefined
'foo' in window

The first two should be equivalent (as long as foo isn't shadowed by a local variable), whereas the last one will return true if the global varible is defined, but not initialized (or explicitly set to undefined).

Christoph
  • 164,997
  • 36
  • 182
  • 240
  • hmm 'foo' in windows seems to return false even if foo is defined for me, in fact only typeof seems to work in my case – Fuseteam Jul 29 '20 at 23:35
1

In JavaScript, the following values will cause the if condition to fail and not execute its statement: null, undefined, false, NaN, the number 0, and the empty string ''.

Assuming that the variable jsVar is a boolean and that we want to call the proceed() method when jsVar is true, we can do the following check.

if (jsVar && jsVar == true)
    proceed();

The above code snippet first check that jsVar has been defined and then checks that its value is true. The if condition will be satisfied only if both the conditions are met.

If jsVar is not a boolean then we can substitute the appropriate check in place of jsVar == true in the code above.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Scott
  • 181
  • 7
  • keep in mind that this does not check for boolean `true` as you're not using strict comparison (in JS, eg `'1' == true` is true!); this will still throw the reference error as well if the variable has not been defined, ie your code doesn't solve Madhu's problem – Christoph Dec 31 '09 at 10:46
  • @Christoph you're right! – Scott Dec 31 '09 at 13:54