Solved

SPOT THE DIFFERENCE between toString() and + ''.

Posted on 2006-06-17
6
1,023 Views
Last Modified: 2008-01-09
Can someone tell me what functional difference (if any) there is between these two expressions...
  obj.func1(x.toString());
... and ...
  obj.func1(x + '');

I'm working on a script that calls functions of an object provided from a separate system.  We recently had a problem where the other system was not correctly accepting a value because it was an integer and the function required a string.

They suggested that rather than just pass the variable containing the integer value (x) that we pass x + ''.  They had tried it and it worked.

I thought that was okay, but just thought the + '' looked untidy, and preferred toString() as it makes the code much clearer (strange idea to try and make Javascript clear - I know).

So I adjusted my script using toString() and they said, "No it doesn't work".

So I experimented with this...

var x = 12;
var y = x.toString();
var z = x + '';

alert(y === z);

... and is said "true".  Which inclined me to think that the two expressions ARE the same.

So what's going on here?  Am I missing something?  Or is someone pulling my chain?
0
Comment
Question by:Hamlet081299
6 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 16926565
First Google shot was this:
http://www.webreference.com/js/column26/tostring.html

So I would confirm the readability aspects of toString() but at the end I would use +''

0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 16926567
Hrm...toString should be working:

var number = 12;
alert(typeof mynumber + ' is not a ' + typeof mynumber.toString());

Should alert "number is not a string".  So it is being converted.  What version of browser are they using?  Have you tried to pass the integer to the function, then on the first line of the function, convert it to a string?

x += ''  Adding a string to an integer in Javascript converts the entire variable to a string variable.  Many other languages will give you a type mismatch error.
0
 
LVL 10

Expert Comment

by:ClickCentric
ID: 16926752
My guess is that if they suggested a specific method and you went with another, this is more a case of bitterness than coding.  You can't program out the human factor.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Accepted Solution

by:
clockwatcher earned 300 total points
ID: 16927539
Hate to be the bearer of bad news but they're not the same.  In the case that x is null, the toString will bomb the + '' won't.


<script language="javascript">

var a = null;

try
{  
   alert(a.toString());
}
catch (e)
{
    alert("sorry but this throws an error: " + e.description);
}


// this doesn't

alert(a + '');

</script>
0
 
LVL 5

Author Comment

by:Hamlet081299
ID: 16928243
Thanks for comments so far.  I'd like to see if there are any other suggestions before I give out points.

Morcalavin: They're using IE6, but something I forgot to mention is that the code specifies this as Javascript1.2 - which could make some difference I guess?  
I don't have direct access to their code, and am just providing them with a script to use in their system.

ClickCentric: I don't think it's them being bitter - They could be mistaken though and maybe the whole thing is a red herring.

clockwatcher: Good point regarding nulls.  Doesn't apply in this particular case as the variable being passed will always be defined, and always be an integer (before being forced to string)  - But good point to remember.

At this stage I have to assume that either they are mistaken, OR we haven't hit on it yet.

I might throw some more points out there and see if I can attract any additional suggestions.



0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 16929446
Chages were made to the toString() method between 1.2 and 1.3.  Zvonko's post points out at least one of those changes in toString when it is operated on an object.  Perhaps you could have them specify 1.3 code and see if that makes a difference?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now