Register  |  Login



Earn money by sharing your knowledge

Quomon shares its advertising revenue with contributing experts that answer questions effectively. Join our community to share your knowledge and earn an income as you go.

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 ?

Answer Discussion
Tutorials

 

Q&A System for Websites and Corporate Collaboration

Advertisement

  • Generates significant organic traffic for websites
  • Saves companies money, resources, and time

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

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 JavaScript Experts

View More

Rank

Expert

Points

1.

nidhi

665

2.

rcastagna

505

3.

jgivoni

428

4.

xarcus

280

5.

theDude

200

6.

redcharcoal

125

7.

roger.berbel

125

8.

Anpanman

87

9.

john2

80

10.

joxley

75

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