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

Posted on 2006-06-17
Last Modified: 2008-01-09
Can someone tell me what functional difference (if any) there is between these two expressions...
... 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?
Question by:Hamlet081299
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 63

Expert Comment

ID: 16926565
First Google shot was this:

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

LVL 18

Expert Comment

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.
LVL 10

Expert Comment

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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 25

Accepted Solution

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;

catch (e)
    alert("sorry but this throws an error: " + e.description);

// this doesn't

alert(a + '');


Author Comment

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.

LVL 18

Expert Comment

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?

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

733 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