Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 13:33 - Jan 09, 2007  

dustPuppy

how to check if a javascript variable is defined?

I have a javascript variable that I need to check if it is defined or not. It' s a boolean.How do I do that and prefererably in a cross-browser - way ?

Categories

Answer Discussion
Tutorials

 

jgivoni

Date:: Jan 09, 2007

Time:: 14:28

Here are some reliable testing scenarios for a javascript variable:

var x;

if (x == null) alert("x is undefined");
else if (x === false) alert("x is false");
else if (x === true) alert("x is true");
else alert("x is " + x);

Remarks:
Declare your variable in the beginning of your script if you know you're gonna test it later. Don't give it a value at this point.
A declared variable that has not been assigned a value will have the value 'null'

Use the === compare operator to check whether the variable is really boolean false.
The == operator is not reliable as a value of 0 will also evaluate to false.
(Basically, the === asks whether variable are of same value AND datatype!)

Hope this helps!
Cheers, Jakob

dustPuppy

Date:: Jan 09, 2007

Time:: 14:36

thanks, Jakob.
What if it is not even declared?

dustPuppy

Date:: Jan 09, 2007

Time:: 14:48

i had a look at this page: http://jehiah.cz/archive/javascript-isde...
but it confuses me more than helps. I didn't test the solutions, but according to the comments they don't seem to work in all the main browsers...

jgivoni

Date:: Jan 09, 2007

Time:: 15:28

Yeah, I wasn't able to find a solution so far for the case that it is not even declared - any testing on a non-declared variable gives a javascript error "x is not defined".

Instead of an individual variable you could also try working with a property of a declared object. That way you don't have to declare the specific property first. You do have to declare the object though, but that could be used for multiple purposes.

Keep me posted if you come across a way of checking wether a variable has been declared.

Jakob

jgivoni

Date:: Jan 09, 2007

Time:: 15:39

I just checked the link you posted and found this easy solution:

if (window.x === undefined) alert("x is undefined");

Just add the "window." in front of the variable name, then you won't get the error message and the expression will evaluate to true in both cases;
- if the variable has been declared but not assigned
- if the variable has simply not been declared.

Actually it's related to what I suggested about using a property instead of a variable. Since all variables are also properties of the window object!

Jakob

dustPuppy

Date:: Feb 10, 2007

Time:: 06:01

thanks, jgivoni.
points well deserved...

Question Answered

This question has been closed, and points have been rewarded to the following experts:


jgivoni: 65
dustPuppy: 10

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

jgivoni

Date:: Feb 11, 2007

Time:: 12:38

Here is an example (testing the variable "x"):

if (window.x === undefined) alert("x is undefined/undeclared");
else if (x === false) alert("x is false");
else if (x === true) alert("x is true");
else alert("x is " + x);

Notes:

In the first line I am using "window.x" instead of just "x". This works because all variables are also defines as properties of the window object.
"x === undefined" will only work if the variable has actually been declared with "var x" (but not assigned a value). Therefore it is safer to use "window.x" which will not result in an error if the variable hasn't been declared.

Use the === compare operator to check whether the variable is really boolean false.
The == operator is not reliable as a value of 0 will also evaluate to false.
(Basically, the === asks whether variable are of same value AND datatype!)


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

alx77

Date:: Jan 24, 2009

Time:: 07:40

if(typeof(myVar) !== 'undefined')


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

nehajoshi1985

Date:: Apr 23, 2009

Time:: 02:20

thank's..one of my small but imp solved because of your suggestion "atx77"


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

martinvahi

Date:: Jan 30, 2010

Time:: 17:19

// There is a difference between local variables and
// global variables. The following code demonstrates,
// how to check their existence regardless of their nature.

var output_msg = ":-) ";

globalizing_economy = function() {
return "Only the nations, where people care for each other, will survive.";
} // globalizing_economy
run_demo = function() {
var a_local_variable = 4;
if ((typeof(window.a_local_variable) === "undefined") && (typeof(a_local_variable) === "undefined")) {
// If the test works properly, we're in here only,
// if the a_local_variable is not defined.
output_msg = output_msg + "test 1 was faulty <br/>";
} // if
function ct() {
// One tests the closure case.
if ((typeof(window.a_local_variable) === "undefined") && (typeof(a_local_variable) === "undefined")) {
output_msg = output_msg + "test 2 was faulty <br/>";
} // if
} // ct
if ((typeof(window.globalizing_economy) === "undefined") && (typeof(globalizing_economy) === "undefined")) {
// If the test works properly, we're in here only,
// if the globalizing_economy is not defined.
output_msg = output_msg + "test 3 was faulty <br/>";
} // if
document.write(output_msg);
} // run_demo
run_demo()


Click here to see the Answer Discussion that preceded this tutorial.


Login to rate this tutorial: Good  |  Bad

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10279

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es