473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Empty Strings and Zero

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
4 1878
gits
5,390 Expert Mod 4TB
:) 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
Mugs321
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
mrhoo
428 256MB
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
5,390 Expert Mod 4TB
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

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

Similar topics

2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
5
by: Krechting | last post by:
Hi ALl, I have a code that checks if the documents in a hyperlink field are still where they should be. I use fileexist(). First I want to filter out all the hyperlink fields that are empty. I...
9
by: VJ | last post by:
I have a property for a control ( source code not available) . The property accepts single char as value.. so I can set it to '/' or ':' or anything like that...At initialize of my From I want this...
4
by: Randall Parker | last post by:
I am designing a database schema. It happens to be in MySQL but I'm trying to keep it portable to other databases should the project grow. Anyway, suppose you have VARCHAR fields and will be...
6
by: Charlie | last post by:
Hi: Is there any difference between string.Empty and String.Empty? And what is the benefit of using it over "". Thanks, Charlie
1
by: Chris Davoli | last post by:
I'm using IndexOf on my strings to find if there are certain words contained in a string. Well, this works fine except when I'm looking at an empty string, because the return value is zero based...
59
by: Steve R. Hastings | last post by:
So, Python 2.5 will have new any() and all() functions. http://www.python.org/dev/peps/pep-0356/ any(seq) returns True if any value in seq evaluates true, False otherwise. all(seq) returns...
14
by: cj | last post by:
What is string.empty used for? I can't say: if string.empty then I have to use: if string = "" then which is ok, I just want to know what .empty is for.
9
by: horizon5 | last post by:
Hi, my collegues and I recently held a coding style review. All of the code we produced is used in house on a commerical project. One of the minor issues I raised was the common idiom of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.