Empty Strings and Zero

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mugs321
    New Member
    • Sep 2006
    • 22

    Empty Strings and Zero

    Hey all,
    I have a simple function which submits a form when a radio-button is clicked:
    Code:
    function submitAccessForm(frmAction, frmActionID){
    	if ((frmActionID+'') != ''){
    		document.getElementById('frmAction').value = frmAction;
    		document.getElementById('frmActionID').value = frmActionID;
    		document.getElementById('step3form').submit();
    	}
    }
    You'll notice I used (+'') to type-cast the frmActionID var to a string. The original looked like this:
    Code:
    if (frmActionID != ''){
    In the original statement, when frmActionID was zero (int zero, not string), the 'if' stmt returned false. Any number other than zero returned true. Does JS view zero (0) as an empty string?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    :) yes ... it does since it uses implicit typecasts here to compare different types ... have a look at the following examples:

    [CODE=javascript]var val = ('' == 0); // val is true

    var val = (' ' == 0); // val is true

    // but:

    var val = ('' === 0); // val is false
    [/CODE]
    the === operator checks for identical types of the values ... so use it in cases where this should be checked too.

    kind regards

    Comment

    • Mugs321
      New Member
      • Sep 2006
      • 22

      #3
      thx for the reply... but I'm still trying to come to terms with this conundrum.

      Consider the following:
      [CODE=javascript]
      alert(0+'xx')
      [/CODE]
      By the 'empty string' logic, shouldn't the alert display 'xx' instead of the '0xx' it actually returns? Even (0+'') returns 0. If the string = '0' (has a length=1) how can it be considered empty?

      I will accept "It's like that... and that's the way it is..." ;)

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

        #4
        0 is the number 0.
        '0' is the string containing the character 0.

        When you coerce a number with + and a string, 0 becomes '0'.
        0+''= '0';
        0+'1'=' 01'

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          Originally posted by Mugs321
          thx for the reply... but I'm still trying to come to terms with this conundrum.

          Consider the following:
          [CODE=javascript]
          alert(0+'xx')
          [/CODE]
          By the 'empty string' logic, shouldn't the alert display 'xx' instead of the '0xx' it actually returns? Even (0+'') returns 0. If the string = '0' (has a length=1) how can it be considered empty?

          I will accept "It's like that... and that's the way it is..." ;)
          nope ... its really consistent ... when using the + operator to add a number type and a string it converts the number to string and concats them ... and when you do:

          [CODE=javascript]'' == '0'
          [/CODE]
          you'll get a false :) since you would compare two strings and js don't need to try a typecast.

          kind regards

          ps: so its a small design-problem with javascript that you don't need to declare the types of a variable but of course there are types and the type is relevant for any operation you do with variables ... so you have to know what types you use and how they will behave during the operation ...

          Comment

          Working...