1

Possible Duplicate:
How to distinguish between a variable that is not declared and a varrable that is declared but not be assigned any value?

I want to see in my code whether someone in the past has already declared the a variable.

I thought about this:

alert(typeof(a)=="undefined") //true

but then I tested:

var a;
alert(typeof(a)=="undefined") //also true !

so how can I check whether there was var a somewhere in the past?

Community
  • 1
  • 1
  • 3
    [In javascript, how to distinguish between a variable that is not declared and a varrable that is declared but not be assigned any value?](http://stackoverflow.com/questions/8675839/in-javascript-how-to-distinguish-between-a-variable-that-is-not-declared-and-a) –  Dec 31 '11 at 14:22
  • 3
    There is [not much difference](http://stackoverflow.com/questions/8675839/in-javascript-how-to-distinguish-between-a-variable-that-is-not-declared-and-a) between `var a; // no value` and not creating `a` at all. Why do you need this? – Lightness Races in Orbit Dec 31 '11 at 14:22

3 Answers3

4

The only way to check whether a variable is (locally) declared is to test the value, and see if any ReferenceError is thrown.

try {
    a === 1; // Some statement to trigger a look-up for the a variable
    alert("a is declared!");
} catch (e) {
    alert("a is not declared!");
}
Esailija
  • 138,174
  • 23
  • 272
  • 326
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • hi rob its RoyiNamir , I run out of available question for today... I guess the try catch statment are the only option here.... yhanks. –  Dec 31 '11 at 14:23
  • 1
    @doomday1, there are probably other options. You just need to tell us what are you trying to do as you forgot to state this in your original question. The real question is why do you need to test against something like this? – Darin Dimitrov Dec 31 '11 at 14:27
  • @Royi Hello ;) In the future, you might want to use the search feature to check whether the question has already been asked. That saves you some time, and you won't run out of questions. – Rob W Dec 31 '11 at 14:27
  • 1
    `a;` would be enough to trigger a `ReferenceError`. – pimvdb Dec 31 '11 at 14:27
  • @Rob did you see the title of the duplicated question ? how can you compare my search string : `check if javascript variable as already been declare` to this long and unfindable result : `How to distinguish between a variable that is not declared and a varrable that is declared but not be assigned any value?` –  Dec 31 '11 at 14:29
  • 2
    @doomday1 Dont type whole sentences. Type the keywords and tags. For example, the following will show both questions (yours + duplicate):`[javascript] variable declared` – Rob W Dec 31 '11 at 14:31
  • and if i want to look up whether property in object has been declared but is not defined so it'd be undefined. How would i check. Because `var o={}; o.c=undefined. or Object.defineProperty(o,'c',{...}); o.c == undefined //true and o.whoAmI == undefined //true..` When clearly one has been declared and on has not been. – Muhammad Umer Aug 10 '13 at 11:44
  • @MuhammadUmer Use the [`in` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in). In your example: `'c' in o // true` and `'whoAmI' in o // false`. This includes any property inherited from the prototype chain. If you want to exclude the inherited values, use [`hasOwnProperty`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty). – Rob W Aug 10 '13 at 12:44
  • http://stackoverflow.com/questions/8675839/how-to-distinguish-between-a-variable-that-is-not-declared-and-a-varrable-that-i/18161908#18161908 i wrote answer there just for me to reference back. – Muhammad Umer Aug 10 '13 at 13:19
2

AFAIK, you can't do this with local variables. Global variables, you can test with 'a' in window.

console.log('a' in window);
   // false

a = undefined;

console.log('a' in window);
   // true

However, why would you do this? It looks like an extremely fragile way to build your program.

EDIT: I lied. This might work:

try {
  a
  console.log("a is declared");
} catch (x) {
  if (x instanceof ReferenceError) {
    console.log("a isn't declared");
  } else {
    // i have no idea why else this could throw an exception...
  }
}

Oh, one more thing: that better not be a console.log in a real application, or make sure you shim it in, because some browsers do not have console defined (such as Firefox without Firebug). That too would be a ReferenceError, btw...

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

Initiate it with null, like:

var a = null;

And check for that.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78