Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Arno Welzel Newsgroups: comp.lang.javascript Subject: Re: Writing type conversions Date: Tue, 24 Nov 2020 09:49:47 +0100 Lines: 43 Message-ID: References: <87eekjrfz0.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net myh3G0OSylDDuBGysBXAOwo+GruMTdVWOjCQsomT5V1dkje7Vr Cancel-Lock: sha1:ADtYfnSdWD26cDNtiNgC641gc8s= In-Reply-To: <87eekjrfz0.fsf@bsb.me.uk> Xref: csiph.com comp.lang.javascript:39180 Ben Bacarisse: > John G Harris writes: > >> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram) >> wrote: >> >> >>> However, in the Java world many deemed »+ ""« to be bad >>> style because it does not express the intent of the >>> programmer to convert something to a string but would >>> mystify the reader. I do not share this point of view, >>> however, because I think the intent of »+ ""« is clear. >> >> "" + x + 1 >> makes it more obvious. > > Am I missing a joke? (It does something else altogether.) No - this is not a joke. Since JavaScript is loosely typed, the expression "" + x + 1 is totally valid and will always create a string as result - so having "" in the beginning makes this more visible. However, the result value depends on wether x is a string not not. x = "4"; y = "" + x + 2; y will be the string "42" x = 4; y = "" + x + 2; y will be the string "6" x = 4; y = x + 2; y will be the number 6 -- Arno Welzel https://arnowelzel.de