By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

Empty Strings and Zero

P: 22
Hey all,
I have a simple function which submits a form when a radio-button is clicked:
Expand|Select|Wrap|Line Numbers
  1. function submitAccessForm(frmAction, frmActionID){
  2.     if ((frmActionID+'') != ''){
  3.         document.getElementById('frmAction').value = frmAction;
  4.         document.getElementById('frmActionID').value = frmActionID;
  5.         document.getElementById('step3form').submit();
  6.     }
  7. }
  8.  
You'll notice I used (+'') to type-cast the frmActionID var to a string. The original looked like this:
Expand|Select|Wrap|Line Numbers
  1. if (frmActionID != ''){
  2.  
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?
Jan 10 '08 #1
Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,333
:) yes ... it does since it uses implicit typecasts here to compare different types ... have a look at the following examples:

Expand|Select|Wrap|Line Numbers
  1. var val = ('' == 0); // val is true
  2.  
  3. var val = ('    ' == 0); // val is true
  4.  
  5. // but:
  6.  
  7. var val = ('' === 0); // val is false
  8.  
the === operator checks for identical types of the values ... so use it in cases where this should be checked too.

kind regards
Jan 10 '08 #2

P: 22
thx for the reply... but I'm still trying to come to terms with this conundrum.

Consider the following:
Expand|Select|Wrap|Line Numbers
  1. alert(0+'xx')
  2.  
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..." ;)
Jan 10 '08 #3

100+
P: 428
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'
Jan 10 '08 #4

gits
Expert Mod 5K+
P: 5,333
thx for the reply... but I'm still trying to come to terms with this conundrum.

Consider the following:
Expand|Select|Wrap|Line Numbers
  1. alert(0+'xx')
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. '' == '0'
  2.  
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 ...
Jan 10 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.