JQuery / Javascript: check if var exists

Possible duplicate:
How to check if a variable is defined in JavaScript? Is there a standard function for checking null, undefined or empty variables in JavaScript?

I have a script that occurs in two parts.

The first part sets var:

var pagetype = "textpage"; 

The second part is a simple if statement:

 if(pagetype == "textpage") { //do something }; 

Now the second part, the if statement, appears on all pages of my site. But the first part, where var is declared, appears only on some of my pages.

On pages without var, I get this error:

 Uncaught ReferenceError: pagetype is not defined 

So my question is: is there a way with JavaScript or JQ to determine if var actually exists (not only if it has data assigned to it)?

I think I would just use a different one if the statute, for example:

 if ("a var called pagetypes exists").... 
+45
javascript variables jquery
Dec 19 '12 at 1:10
source share
6 answers

I suspect there are many such answers on SO, but here you go:

 if ( typeof pagetype !== 'undefined' && pagetype == 'textpage' ) { ... } 
+90
Dec 19 '12 at 1:11
source share

You can use typeof :

 if (typeof pagetype === 'undefined') { // pagetype doesn't exist } 
+16
Dec 19 '12 at 1:11
source share

For your case and for 99.9% of all other answers elclanrs correct.

But since undefined is a valid value, if someone had to check an uninitialized variable

 var pagetype; //== undefined if (typeof pagetype === 'undefined') //true 

the only 100% reliable way to determine if var exists to catch an exception;

 var exists = false; try { pagetype; exists = true;} catch(e) {} if (exists && ...) {} 

But I will never write it like this.

+8
Dec 19
source share

There are two methods for checking existence.

but. "property" in object

This method checks the prototype chain for the existence of the property.

b. object.hasOwnProperty( "property" )

This method does not fit the prototype chain to check for the existence of a property; it must exist in the object you are calling the method on.

 var x; // variable declared in global scope and now exists "x" in window; // true window.hasOwnProperty( "x" ); //true 

If we tested using the following expression, it would return false

 typeof x !== 'undefined'; // false 
+4
Dec 19 '12 at 1:22
source share

Before each of your conditional statements, you can do something like this:

 var pagetype = pagetype || false; if (pagetype === 'something') { //do stuff } 
+2
Dec 19 '12 at 1:25
source share

It is not possible to determine whether a variable was specified or not, except for an attempt to try..catch to raise an error if it was not declared. Test like:

 if (typeof varName == 'undefined') 

Do not tell you if varName is a scope variable, only this is a type return testof undefined. eg.

 var foo; typeof foo == 'undefined'; // true typeof bar == 'undefined'; // true 

In the above example, you cannot say that foo was declared, but bar was not. You can check global variables using in :

 var global = this; ... 'bar' in global; // false 

But the global object is the only variable * object that you can access, you cannot access the variable object of any other execution context.

The solution is to always declare variables in the appropriate context.

  • A global object is not really a variable object, it just has properties that correspond to global variables and provides access to them, so it just seems to be one.
+2
Dec 19 '12 at 1:32
source share



All Articles