String(symbol)

Domenic Denicola domenic at domenicdenicola.com
Tue Aug 26 09:26:48 PDT 2014


`x+''` produces different results than ToString when `x = { toString() { return '5'; }, valueOf() { return 10; } }`
________________________________________
From: es-discuss <es-discuss-bounces at mozilla.org> on behalf of Brendan Eich <brendan at mozilla.org>
Sent: Tuesday, August 26, 2014 12:14
To: Claude Pache; Jeff Walden
Cc: Erik Arvidsson; es-discuss at mozilla.org
Subject: Re: String(symbol)

Claude Pache wrote:
> Personally, I use the following expression in order to coerce a variable to a string:
>
>       var obj = this + ''
>
> and its in-place variant:
>
>       x += ''
>
> I think it'll continue to work in ES6.

Agreed, this is the correct-and-most-concise way to do it. It should
throw on symbols, that's important.

Having String(sym) not throw is a win for other reasons Arv identified,
but using String(any) to invoke ToString is verbose and fragile, because
String could be rebound :-P. Use +'' if you want ToString in the
language, I say.

/be
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list