What is the best way to detect if a variable (global or local) has been created?
In the following code, if you run it with the variable ‘casefile’ defined, the alert works as expected.
When you run it with no casefile = assignment, neither alert is displayed.
Why do I get no alert notification when ‘casefile’ is not assigned or defined?
When a global or local varialbe has been accidently left unassigned or undefined,
how should it be checked if maked a default assignment?
Hope I’m making myself clear. When is a variable undefined or null and how is that condition checked so a default value could be created?
?
[CODE]
<!DOCTYPE HTML>
<html>
<head>
<title> Untitled </title>
<meta charset=”utf-8″>
</head>
<body>
<script type=”text/javascript”>
var str = ”;
// var casefile; // works fine if this variable is created
// var casefile = ‘The variable casefile’; // run script with this line IN and then commented OUT
// NOTHING displays if both lines above are commented OUT
if (casefile) { str += casefile+’ is availablenn’; }
else { str += casefile+’ is not availablenn’; }
if (casefile == undefined) { str += casefile+’ is undefinednn’; }
else { str += casefile+’ is not undefinednn’; }
if (casefile == null) { str += casefile+’ is nullnn’; }
else { str += casefile+’ is not nullnn’; }
alert(str);
</script>
</body>
</html>