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
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.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
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…

770 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