Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of undefined variables in if-statements

Tags:

javascript

This snippet results in a JavaScript runtime error: (foo is not defined)

if (foo) {
    // ...
}

I have to define foo first, like so:

var foo = foo || null // or undefined, 0, etc.

... And only then can I do:

if (foo) {
    // ...
}

Why is that?


Update:

This was somewhat of a brainfart on my side of things: 'fcourse you can't access a variable which is not allocated.

Fun stuff that you can do a typeof() on an undefined variable thou. I'm gonna accept miccet's answer since I think it's the most elegant solution.

like image 233
cllpse Avatar asked Feb 24 '09 11:02

cllpse


People also ask

How do you use an undefined if statement?

Answer: Use the equality operator ( == )In JavaScript if a variable has been declared, but has not been assigned a value, is automatically assigned the value undefined . Therefore, if you try to display the value of such variable, the word "undefined" will be displayed.

What does it mean when a variable is undefined?

An undefined variable is a variable used in a program that was not previously declared in the source code. In most programming languages, this results in an error.

What is the use of undefined in JavaScript?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

How do you know if a variable is undefined?

To check if a variable is undefined, you can use comparison operators — the equality operator == or strict equality operator === . If you declare a variable but not assign a value, it will return undefined automatically. Thus, if you try to display the value of such variable, the word "undefined" will be displayed.


3 Answers

I sense you are asking because you are aware that javascript seems to allow undefined variables in some situations (ie no runtime errors) and not in others.

The reasoning is as follows: javascript always throws an error on checking undefined variables, but never throws an error on checking undefined properties, as long as you only use one level of indirection.

Example:

// throws runtime error
if(foo) {
    // ...
}

// does not throw runtime error
if(window.foo) {
    // ...
}

// does not throw runtime error
var obj = {};
if(obj.foo) {
    // ...
}

// throws runtime error
if(obj.foo.bar) { // going two levels deep, but foo is undefined
    // ...
}

Hope that clears it up a bit.

like image 83
Crescent Fresh Avatar answered Sep 23 '22 14:09

Crescent Fresh


You'll have to define it, to be able to check it for a value. In this case you're checking weather it's true. This variable is obviously not set to anything at all, same as null in C# and Nothing in VB for example.

If you must, debugging issues or whatever, you could check if the variable is undefined like this:

if (typeof(variable) == "undefined")
like image 19
miccet Avatar answered Sep 21 '22 14:09

miccet


That would be because you're now defining it with:

var foo = foo || null

Why don't you define it in the first place? That seems pretty straightforward to me (unless I'm missing something).

Using a variable before it's created (or set to something) is bad programming practice, and should be avoided. My advice is to not use that trick to ensure it is set to something but to track down the logic error and fix it.

like image 1
paxdiablo Avatar answered Sep 22 '22 14:09

paxdiablo