Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts

    the purpose of 'IF(variable)'

    all,

    can someone clarify what the following code does?
    Code:
    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!

  • #2
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by ajetrumpet View Post
    all,

    can someone clarify what the following code does?
    Code:
    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:

    Code:
    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:

    Code:
    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

  • Users who have thanked Krupski for this post:

    ajetrumpet (12-26-2010)

  • #3
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    It also won't execute if the value is 0

  • #4
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by gizmo1650 View Post
    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?

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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.

    Code:
    var int = 0;
    
    if(int) {
      alert('int is true');
    } else {
      alert('int is false');
    }
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    ajetrumpet (12-26-2010)

  • #6
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by DJCMBear View Post
    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.

    Code:
    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! =)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    All of them missed the fact that the code reads
    Code:
    if (form_field) {
       //some code
    }
    It does *NOT* read
    Code:
    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
    Code:
        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
    Code:
    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:
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Old Pedant View Post
    All of them missed the fact that the code reads
    Code:
    if (form_field) {
       //some code
    }
    It does *NOT* read
    Code:
    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.......

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    Quote Originally Posted by Krupski View Post
    "form_field" could be just a plain ole' variable.......
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •