Solved

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

Posted on 2006-06-17
6
1,021 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

707 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

11 Experts available now in Live!

Get 1:1 Help Now