What's the shortest way to convert any JavaScript value to a string?
I have a function, say:
setValue: function(myValue) {
...
}
The caller can pass a string, number, boolean, or object. I need to make sure that the value passed down the line is a string. What's the safest way to do this? I understand that some types (like Date) can be converted to strings, but I'm just looking for something sane out of the box.
I could write a series of operators like:
if (typeof myValue == "boolean") {}
else if () {}
...
But this can be error prone as types can be omitted.
Firefox seems to support things like:
var foo = 10; foo.toString()
But will this work with all web browsers? I need to support IE 6 and up.
In short, what is the shortest way to perform the conversion while covering each individual type?
-Erik
+1
a source to share