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:56:16 +0100 Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net U30rYq8FrmF+ELcbYWxQXASMw8lUK3Z5Hp5ltPD5yOcOkbsJGF Cancel-Lock: sha1:5GPQaj+J5EeSddi1xwvMlUGB0k4= In-Reply-To: Xref: csiph.com comp.lang.javascript:39181 John G Harris: > 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. But this may lead to the wrong result. Example: x = "4"; y = + x + 2; Result: "6" x = "4"; y = "" + x + 2; Result: "42" In general: if it is important to get a value as number, you should always create a numeric value from it using Number() and never rely on the expression itself. -- Arno Welzel https://arnowelzel.de