Advertisement

06.17.2006 at 05:19AM PDT, ID: 21889896
[x]
Attachment Details

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

Asked by Hamlet in JavaScript

Tags: difference, spot, between, javascript

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?
Start Free Trial
 
Keywords: SPOT THE DIFFERENCE between toStri…
 
Loading Advertisement...
 
[+][-]06.17.2006 at 05:43AM PDT, ID: 16926565

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.17.2006 at 05:45AM PDT, ID: 16926567

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.17.2006 at 07:19AM PDT, ID: 16926752

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.17.2006 at 01:21PM PDT, ID: 16927539

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: difference, spot, between, javascript
Sign Up Now!
Solution Provided By: clockwatcher
Participating Experts: 4
Solution Grade: B
 
 
[+][-]06.17.2006 at 06:50PM PDT, ID: 16928243

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.18.2006 at 05:13AM PDT, ID: 16929446

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32