Answers
Jan 09, 2007 - 10:28 AM
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");
elsealert("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
Jan 09, 2007 - 10:36 AM
What if it is not even declared?
Jan 09, 2007 - 10:48 AM
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...
Jan 09, 2007 - 11:28 AM
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
Jan 09, 2007 - 11:39 AM
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
Feb 10, 2007 - 02:01 AM
points well deserved...
Feb 11, 2007 - 08:38 AM
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!)
Jan 24, 2009 - 03:40 AM
Jan 30, 2010 - 01:19 PM
// 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
";
} // 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
";
} // 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
";
} // if
document.write(output_msg);
} // run_demo
run_demo()
Add New Comment