119

How can i check in JavaScript if a variable is defined in a page? Suppose I want to check if a variable named "x" is defined in a page, if I do if(x != null), it gives me an error.

Jason Bunting
  • 58,249
  • 14
  • 102
  • 93
SSharma
  • 2,865
  • 3
  • 17
  • 6

7 Answers7

165

I got it to work using if (typeof(x) != "undefined")

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
SSharma
  • 2,865
  • 3
  • 17
  • 6
  • you are checking if x = a string, not if x is undefined. – FlySwat Sep 26 '08 at 23:02
  • 1
    Jonathan, are you sure about that? John Resig uses this exact approach in his javascript tutorial here: http://ejohn.org/apps/learn/#11 You can run the script on the page and see for yourself. – Paul Batum Sep 26 '08 at 23:13
  • 39
    I just checked it in firebug, and undefined does map to the string "undefined", someone was seriously smoking crack when they wrote JS, I stand corrected. – FlySwat Sep 26 '08 at 23:32
  • 2
    @Ben: The string `"undefined"` is more correct – if `!==` is used then the quotes are necessary because `typeof` results in a string. – Sophie Alpert Aug 14 '11 at 23:52
  • 1
    @BenBederson @BenAlpert, additionally, `undefined` can be overridden! Use the string. – Derek Prior May 08 '12 at 19:45
  • @Derek Whoa, I just learned something. Seems they've started fixing it in recent browsers, at least, so hopefully it's something I can forget again in about ten years. – Jason Newell Mar 19 '16 at 00:32
52

To avoid accidental assignment, I make a habit of reversing the order of the conditional expression:

if ('undefined' !== typeof x) {
Andrew Hedges
  • 21,688
  • 16
  • 67
  • 79
22

The typeof operator, unlike the other operators, doens't throws a ReferenceError exception when used with an undeclared symbol, so its safe to use...

if (typeof a != "undefined") {
    a();
}
Pablo Cabrera
  • 5,749
  • 4
  • 23
  • 28
1

You can do that with:

if (window.x !== undefined) { // You code here }

  • This works with "variables" in the global scope only because those are actually properties of the ''window'' object. Variables declared using the ''var'' statement will not be testable this way. More here: http://ahedg.es/84 – Andrew Hedges Aug 08 '11 at 17:16
  • Also, this assumes that the global object is window, which is (usually?) the case in a browser, but isn't necessarily true in other environments. – Jason Newell Mar 19 '16 at 00:16
1

As others have mentioned, the typeof operator can evaluate even an undeclared identifier without throwing an error.

alert (typeof sdgfsdgsd);

Will show "undefined," where something like

alert (sdgfsdgsd);

will throw a ReferenceError.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
0

Assuming your function or variable is defined in the typical "global" (see: window's) scope, I much prefer:

if (window.a != null) {
   a();
}

or even the following, if you're checking for a function's existence:

if (window.a) a();
kamens
  • 11,910
  • 6
  • 45
  • 46
-5

try to use undefined

if (x !== undefined)

This is how checks for specific Browser features are done.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • Are you sure that exact syntax works Nick? It comes up as an error for me, x is undefined. – Paul Batum Sep 26 '08 at 23:19
  • 1
    How did this get 4 votes? It's doing almost the same wrong thing as what OP had. – Ruan Mendes Feb 12 '11 at 01:21
  • The problem with this is that _undefined_ can be _redefined,_ so only use this if you are certain that isn't the case in your scope. – Andrew Hedges Aug 08 '11 at 17:14
  • 1
    @AndrewHedges As others have pointed out, another problem is that it assumes x is declared. Try pasting "x == undefined" into a JS console and you'll get a reference error. – Jason Newell Mar 19 '16 at 00:35