Select Categories Below
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.
Status: Closed Points: 75 Time: 13:33 - Jan 09, 2007
dustPuppy
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:
Add Categories
When adding more than one category, separate them with commas.
Advertisement
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
Time:: 14:36
thanks, Jakob. What if it is not even declared?
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...
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
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
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:
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?
Enter your email address below and we will resend your login information to you.
Login Information Sent
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
Questions
How do I unset a variable, array element or object property in JavaScript?
You have 100 characters to use
Rank
Expert
Points
1.
665
2.
505
3.
428
4.
280
5.
200
6.
125
7.
8.
87
9.
80
10.
75
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 it
Language Options
English:
Español:
Sponsors
Questions and Answers Software Real Estate Postcards Marketing Fulfillment