/    Sign up×
Community /Pin to ProfileBookmark

Detecting assigned and undefined variables

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>
[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@rootNov 05.2013 — [CODE]if(typeof varName == "undefined") alert("Undefined!");[/CODE]
Copy linkTweet thisAlerts:
@tech_soul8Nov 05.2013 — Hm... this is interesting but... you have to know some theory ?


First when a variable is defined in top of the code, throughout the program (not locally) it becomes a property of a [I]global object[/I]. In client-side JavaScript it becomes property of a [I]window object.[/I]

Now let's start with the first example when the [I]casefile[/I] is defined but not initialized to any value:

<i>
</i>var casefile;


[B]The first[/B] if statement:

<i>
</i>if (casefile) { str += casefile+' is availablenn'; }
else { str += casefile+' is not availablenn'; }


would evaluate to [I]false[/I] because [I]casefile[/I] variable is defined and became a property of the [I]global object[/I] but its value has not been initialized and so it is [I]undefined.[/I] JavaScript [B]type conversion[/B] specifies that any [I]undefined[/I] value is converted to Boolean [I]false[/I] and that's the reason why yo have result of [I]"undefined is not available[/I].


[B]The second[/B] if statement:

<i>
</i>
if (casefile == undefined) { str += casefile+' is undefinednn'; }
else { str += casefile+' is not undefinednn'; }


evaluates to [I]true[/I] because [I]undefined equals undefined[/I].

[B]The third[/B] if statement:

<i>
</i>if (casefile == null) { str += casefile+' is nullnn'; }
else { str += casefile+' is not nullnn'; }


i think that it gets evaluated to true, because JavaScript considers [I]undefined and null[/I] as both values that represents absence of value but undefined represents some deeper kind of absence.

You could check this with the strict equality operator:

<i>
</i>if (casefile === null) { str += casefile+' is nullnn'; }
else { str += casefile+' is not nullnn'; }


Now the result of the above conditional check is [I]false[/I].

Ok, so what happens when you comment out both lines?

If you check your debugger you'll see that there's "Reference error" thrown up. This is because in [I]ECMAscript 5[/I] strict mode if you don't define a variable you won't get away with it (its value won't be initialized to undefined) so your code fails at the first if statement because [I]casefile[/I] is not defined.
Copy linkTweet thisAlerts:
@tech_soul8Nov 05.2013 — [CODE]if(typeof varName == "undefined") alert("Undefined!");[/CODE][/QUOTE]

As far as I know this ain't gonna happen in ECMAscript 5 strict mode if variable is not first defined. If a variable is defined but not initialized then its value becomes [I]undefined[/I]. Otherwise "Reference error" is thrown up.
Copy linkTweet thisAlerts:
@tech_soul8Nov 05.2013 — 
When is a variable undefined or null and how is that condition checked so a default value could be created?
[/QUOTE]


Undefined and null are both values that represent absence of a value as already stated in the previous reply.

Undefined value specifies some deeper kind of absence. When you try to query a property of an object that doesn't exist the result would be an [I]undefined value.[/I] The same is true when you try to access array element which doesn't exist. Undefined value is also returned as a result of function which doesn't return a value and as a value to an uninitialized variable or a value of a function parameter for which no argument has been supplied.

Null typically means no value for [I]string, number or an object.[/I]
Copy linkTweet thisAlerts:
@JMRKERauthorNov 06.2013 — Thank you for your time and expertise.

?
Copy linkTweet thisAlerts:
@tech_soul8Nov 06.2013 — You're welcome. I'm glad if I was able to help you.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...