...

View Full Version : the purpose of 'IF(variable)'



ajetrumpet
12-26-2010, 12:35 AM
all,

can someone clarify what the following code does?
if (form_field) {
//some code
}
I work with visual basic and this does not generally work to check for null or empty fields or controls. Is that what this simple statement does? I assume that it basically checks for emptiness of a form control. Is that completely correct, or are there other factors involved as well?

thanks!

Krupski
12-26-2010, 02:23 AM
all,

can someone clarify what the following code does?
if (form_field) {
//some code
}I work with visual basic and this does not generally work to check for null or empty fields or controls. Is that what this simple statement does? I assume that it basically checks for emptiness of a form control. Is that completely correct, or are there other factors involved as well?

thanks!

That simply execute "some code" if "form_field" contains something (that is, not false or empty or null).

Here's an example:


var form_field = false;

if(form_field)
{
alert('This code would execute');
}
else
{
alert('Nothing would happen');
}
In the above code, the alert 'Nothing would happen' would pop up.

Now, look at this same code with one extra line:


var form_field = false;

form_field = 'Hello';

if(form_field)
{
alert('This code would execute');
}
else
{
alert('Nothing would happen');
}
Now, 'This code would execute' pops up.

Hope this helps.....

-- Roger

gizmo1650
12-26-2010, 02:39 AM
It also won't execute if the value is 0

ajetrumpet
12-26-2010, 05:53 PM
It also won't execute if the value is 0

are you sure about this? I have a hard time believing that. If so, is that because 0 is the equivalent to the FALSE boolean, and is read as a boolean by default?

DJCMBear
12-26-2010, 06:25 PM
If the value being checked is empty, false or 0 the if statement will not run as all three are taken as false, that's why some codes do this.



var int = 0;

if(int) {
alert('int is true');
} else {
alert('int is false');
}

ajetrumpet
12-26-2010, 07:55 PM
If the value being checked is empty, false or 0 the if statement will not run as all three are taken as false, that's why some codes do this.



var int = 0;

if(int) {
alert('int is true');
} else {
alert('int is false');
}

the most helpful thing I got from this post is that validation site you gave me! thanks! =)

Old Pedant
12-27-2010, 07:01 AM
All of them missed the fact that the code reads

if (form_field) {
//some code
}
It does *NOT* read

if (form_field.value) {
//some code
}
So, almost surely, that code is *NOT* testing the value of any form field.

Whether it is doing anything useful, at all, depends on where the code is executed.

You can't, in general, refer to a field in a form by it's bare name or id (except in MSIE, and even then only sometimes). You can if you are referring to it, by name, from within an event handler defined on some field in the same form.

Of course, if prior to that line you had done something like

var form_field = document.forms[0].someFieldName;
or even
var form_field = document.forms[0][7];
then indeed it could be referring to a valid field in a valid form.

But there's another reason we know it's not testing the value of any form field: Quite simply, form fields ALWAYS have a value! And it's ALWAYS a string. Oh, it can be the blank string "", but it won't be null and so that if condition would *always* be true if you were testing for a value, so why would you bother making that test??? [Note: yes, yes, I know that you *can* force an exception to this rule.]

And even if you don't give an explicit value to, say, a checkbox or radio button, it *does* automatically get a default value given to it by the browser (happens to be "on" for checkbox and radio).

So...

We can almost surely assume that if you ever see code such as

if (form_field) {
//some code
}
it is testing for the *existence* of a given form field, not for whether or not the field has a value. And, yes, if you simply omit a field from your form and then try to reference it, it will be null.

Finally, whether or not that code makes a smidgen of sense at all depends on the context. Which you didn't supply. So show more context and maybe we can make sense of it.

And just for fun, a demo:


<html>
<head>
<script type="text/javascript">
function see( form )
{
var msg = "";
for ( var i = 0; i < form.elements.length; ++i )
{
var fld = form.elements[i];
msg += fld.name + "=" + fld.value + " (is null?" + (fld.value==null) + ")\n";
}
alert( msg );
}
</script>
</head>
<body onload="see(document.forms[0])">
<form>
<input name="myText">
<input type=checkbox name="myCheckbox">
<input type=radio name="myRadio">
<input type=button name="myButton">
<input type=submit name="mySubmit">
<select name="mySelect">
<option></option>
</select>
</form>
</body>
</html>

Go ahead. Try it.

Krupski
12-27-2010, 04:31 PM
All of them missed the fact that the code reads

if (form_field) {
//some code
}It does *NOT* read

if (form_field.value) {
//some code
}So, almost surely, that code is *NOT* testing the value of any form field.

"form_field" could be just a plain ole' variable....... :p

Old Pedant
12-27-2010, 07:26 PM
"form_field" could be just a plain ole' variable....... :p

Yes, and I mentioned that, when I said one might have done var form_field = ...

But recall the context of the original post:

I work with visual basic and this does not generally work to check for null or empty fields or controls. Is that what this simple statement does? I assume that it basically checks for emptiness of a form control.

And, I say again, it is clearly *NOT* testing for the "emptiness" of any form control. Existence, perhaps. Emptiness, never. At least it shouldn't be done this way.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum