Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? (2012-05-12)

3 views
Skip to first unread message

FAQ server

unread,
May 11, 2012, 7:00:02 PM5/11/12
to
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
-----------------------------------------------------------------------

Variables are not typed; their values are. The conversion between a
string and a number happens automatically.

The addition operator (`+`) performs concatenation if either operand is a
string, thus `"1" + 1` results `"11"`. To perform addition, you might need
to first convert the string to a number. For example `+varname` or
`Number(varname)` or `parseInt(varname, 10)` or
`parseFloat(varname)`. Form control values are strings, as is the result
from a `prompt` dialog. Convert these to numbers before performing
addition: `+'1' + 1` results `2`.

Additional Notes: <URL: http://jibbering.com/faq/notes/type-conversion/>
<URL: http://msdn.microsoft.com/en-us/library/67defydd%28VS.85%29.aspx>


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

0 new messages