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.

- 58,249
- 14
- 102
- 93

- 2,865
- 3
- 17
- 6
7 Answers
I got it to work using if (typeof(x) != "undefined")

- 30,033
- 48
- 152
- 225

- 2,865
- 3
- 17
- 6
-
you are checking if x = a string, not if x is undefined. – FlySwat Sep 26 '08 at 23:02
-
1Jonathan, 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
-
39I 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
To avoid accidental assignment, I make a habit of reversing the order of the conditional expression:
if ('undefined' !== typeof x) {

- 21,688
- 16
- 67
- 79
-
hey, i reverse the order too! can't remember where i learned it though... – just mike Sep 27 '08 at 13:44
-
2
-
22
-
2
-
1This is undeniably the better way to do this, but its awkwardness bugs me (plus it's often inconsistent with other people's code). I want it to be wrong. – Jason Newell Mar 19 '16 at 00:10
-
Dont' write code like this. We aren't Yoda. Code exists purely to be readable. If readability isn't your goal, write assembly and save the cycles. Python solved this best by created an explicit assignment within if statements. – Luke Dupin Nov 19 '20 at 20:31
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();
}

- 5,749
- 4
- 23
- 28
You can do that with:
if (window.x !== undefined) { // You code here }

- 1,931
- 19
- 28
-
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
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.

- 75,346
- 19
- 113
- 141
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();

- 11,910
- 6
- 45
- 46
try to use undefined
if (x !== undefined)
This is how checks for specific Browser features are done.

- 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
-
1How 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