Search code examples
javascriptjslint

What is the JSLint approved way to convert a number to a string?


I've always converted numbers to strings by adding an empty string to them:

var string = 1 + '';

However, JSLint complains of this method with Expected 'String' and instead saw ''''., and it does look a little ugly.

Is there a better way?


Solution

  • I believe that the JSLint approved way is to call .toString() on the number:

    var stringified = 1..toString();
    // Note the use of the double .. to ensure the the interpreter knows 
    // that we are calling the toString method on a number -- 
    // not courting a syntax error.
    // You could also theoretically call 1["toString"];