Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  27] [ 2]  / answers: 1 / hits: 71017  / 15 Years ago, fri, may 15, 2009, 12:00:00

I'm implementing a function that receives an argument which it needs to convert to its string representation.



If a given object implements a toString() method, then the function should use it. Otherwise, the function can rely on what the JavaScript implementation offers.



What I come up with is like this:



var convert = function (arg) {
return (new String(arg)).valueOf();
}

More From » javascript

 Answers
19

I'm not sure you even need a function, but this would be the shortest way:



function( arg ) {
return arg + '';
}


Otherwise this is the shortest way:



arg += '';

[#99524] Tuesday, May 12, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austenjordang

Total Points: 544
Total Questions: 112
Total Answers: 112

Location: Monaco
Member since Sun, Jan 16, 2022
2 Years ago
;