Results 1 to 4 of 4

Thread: if (var) vs. if (var != )

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default if (var) vs. if (var != )

    Sample:

    Code:
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <title>Check Text Field Value</title>
        <style>
        </style>
    </head>
    
    <body>
        <textarea rows="5" cols="35" id="field" oninput="indicate();"></textarea>
        <div id="indicator"></div>
        <script>
            function indicate() {
                var field = document.getElementById('field');
                var indicator = document.getElementById('indicator');
                if (field.value) {
                    indicator.innerHTML = 'Some content';
                } else {
                    indicator.innerHTML = 'No content';
                }
            }
            indicate();
        </script>
    </body>
    
    </html>
    Demo: http://jsfiddle.net/RainLover/eZaRd/

    I want to check if the text field is empty or not. I've seen many questions and answers some using if (textarea.value) and some using if (textarea.value != ""). Or the following pair:
    if (!textarea.value) vs. if (textarea.value == "").

    I wonder what's the difference between them.

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,336
    Thanks
    1
    Thanked 241 Times in 236 Posts

    Default

    Hi there Rain Lover,

    does this help...
    Code:
    
    <!DOCTYPE html>
    <html>
    
    <head>
    <meta charset="UTF-8">
    <title>Check Text Field Value</title>
    
    <script>
    (function() {
       'use strict';
    
       var field,indicator;
    
    function init() {
       field=document.getElementById('field');
       indicator=document.getElementById('indicator');
    field.oninput=function() {
       indicate();
     }
       indicate();
     }
    
    function indicate() {
    if(field.value.replace(/\s/g,'').length==0) {
       indicator.innerHTML='No content';
     } 
    else {
       indicator.innerHTML='Some content';
      }
     }
       window.addEventListener?
       window.addEventListener('load',init,false):
       window.attachEvent('onload',init);
    })();
    </script>
    
    </head>
    <body>
    
    <textarea id="field" rows="5" cols="35"></textarea>
    <div id="indicator"></div>
    
    </body>
    
    </html>

    coothead

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The more or less new HTML 5 oninput event is not backwardly compatible. IE 8 and less, and perhaps other older browsers in other browser families, will not fire it. This can be a problem because IE 8 is still in fairly wide use, and for users of Win XP, there's no upgrade path for the IE family of browsers. You can fake it though, I think it's a combination, of onkeyup and onpaste, the later of which can also be faked if unavailable.

    That said, to answer the question, there really is no difference here that I can see. These sorts of variations in testing whether a value is or is not something (or more accurately here - is or is not nothing) can be important if the value can be a boolean false while not being empty or vise versa (like a number or an object). Here it (as far as I can tell) has to be a string or false, so I don't think any of this matters). In any case you just need to make sure you're testing for whatever it is that you really want to know without allowing some other value that might not be what you're allowing, to pass the test. And not allowing some value that you want to pass be seen as unacceptable. If all else fails, use trial and error of various extreme/unusual possible values to test your test. But usually logical thought alone will enable you to tell if your test is necessary and sufficient.

    When I'm confronted with such choices and it appears to make no difference which is used, I usually go for the simplest looking one and/or the one that makes the most sense if said out loud at that point in the code. Often though, especially with existing code, I'll just use whatever was used as long as it works, there are no exceptions, and it doesn't make the code overly confusing to read.
    Last edited by jscheuer1; 02-11-2014 at 03:40 PM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Rain Lover (02-11-2014)

  5. #4
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Thumbs up

    Quote Originally Posted by jscheuer1 View Post
    The more or less new HTML 5 oninput event is not backwardly compatible. IE 8 and less, and perhaps other older browser in other browser families, will not fire it. This can be a problem because IE 8 is still in fairly wide use, and for users of Win XP, there's no upgrade path for the IE family of browsers. You can fake it though, I think it's a combination, of onkeyup and onpaste, the later of which can also be faked if unavailable.
    Many thanks for the answer, John!
    For IE8 I use onpropertychange="indicate();".

Bookmarks

Posting Permissions

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