9.8 ToString

2010-05-04

string ToString(input)

This is the abstract function of converting a value to string. This is not the toString method! Although it uses this function.

Code: (Meta Ecma)
function ToString(input){
if (input === undefined) return "undefined";
if (input === null) return "null";
if (input === true) return "true";
if (input === false) return "false";
if (typeof input == "string") return input;
if (typeof input == "object") return ToString(ToPrimitive(input, "String"));
// otherwise the input is a number for which a complex algorithm is given by the specification.
// my example will not be the complete algorithm.
if (isNaN(input)) return "NaN";
if (input === +0 || input === -0) return input;
if (input < 0) return "-" + ToString(-input); // prefixed dash
if (input === infinity) return "Infinity"; // no need for negative version, see above
// next follow a sequence of complex operations to convert
// a number into a string. we've completed checks for edge
// cases and will now convert the number, taking into account
// a certain precision, etc.
}

It is noted in the specification (but not "normative", or "forced by the specification") that ToNumber(ToString(x)) === x for any other number than -0.
It's also noted that the least significant digit (right-most) is not always uniquely determined by step 5. It also gives a more exact (but probably more complex) version of that step in the algorithm.