-3
var a;
typeof(a);
//undefined

typeof(c);
//undefined

if(c) {}
//throw error

How can I know that c doesn't exist without try catch.

Update after marked duplicate:
typeof initializedVariable and typeof notInitializedVariable both will show 'undefined'. My question is to know whether the variable exists(initialized) or not.

vusan
  • 5,221
  • 4
  • 46
  • 81
  • `if(c !== undefined)` ? – Brewal Jul 09 '13 at 09:24
  • 2
    What was the problem with the other similar questions (see list at right, you should have seen it when making the question) ? – Denys Séguret Jul 09 '13 at 09:25
  • Why don't you want a warning if you try to use a variable that doesn't exist? – 1983 Jul 09 '13 at 10:01
  • `typeof initializedVariable` and `typeof notInitializedVariable` both will show 'undefined'. My question is to know whether the variable exists(initialized) or not. – vusan Oct 12 '17 at 10:42

1 Answers1

3

You can use the typeof operator.

 if (typeof a === 'undefined') {
     // variable is undefined
 }
AllTooSir
  • 48,828
  • 16
  • 130
  • 164