5

WTF to convert integer to string:

var i = 1;
var str = i + "";

Comments
  • 3
    What's wrong with it?
  • 0
    Well, it's shorter than i.toString()
  • 2
    Why not
    var i = 1;
    i = i + “”;
  • 0
    Also I prefer

    i = "" + i;

    so I'm sure, what the resulting datatype is. Consider

    i = 5 + c * s + f + a + i + "";

    for the "proof".
  • 0
    @moagggi pretty much as expected. Adding ' ' at start and end won't make any different. Anything did I miss?
  • 2
    Foremost, it looks ugly.

    Secondly, the purpose of '+' operator is addition/concatenation, not to convert number into string.

    Lastly, engine/compiler has to do 2 operations: first convert the number and then concatenate empty string.
Add Comment