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.

0

7 Answers 7

165

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

Sign up to request clarification or add additional context in comments.

6 Comments

you are checking if x = a string, not if x is undefined.
Jonathan, are you sure about that? John Resig uses this exact approach in his javascript tutorial here: ejohn.org/apps/learn/#11 You can run the script on the page and see for yourself.
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.
@Ben: The string "undefined" is more correct – if !== is used then the quotes are necessary because typeof results in a string.
@BenBederson @BenAlpert, additionally, undefined can be overridden! Use the string.
|
52

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

if ('undefined' !== typeof x) {

6 Comments

hey, i reverse the order too! can't remember where i learned it though...
You want to use !==, not ===. :)
reverse assignment i hate.
.sentences english those completely reverse
This 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.
|
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();
}

Comments

1

You can do that with:

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

2 Comments

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: ahedg.es/84
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.
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.

Comments

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();

Comments

-5

try to use undefined

if (x !== undefined)

This is how checks for specific Browser features are done.

4 Comments

Are you sure that exact syntax works Nick? It comes up as an error for me, x is undefined.
How did this get 4 votes? It's doing almost the same wrong thing as what OP had.
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.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.