Link to home
Start Free TrialLog in
Avatar of Hamlet081299
Hamlet081299

asked on

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

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?
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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 +''

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.
Avatar of ClickCentric
ClickCentric

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.
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hamlet081299

ASKER

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.



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?