From: FAQ server on
-----------------------------------------------------------------------
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. Since plus (` + `) is
also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `.
The string determines what ` + ` does. To overcome this, 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 by using
the unary ` + ` operator: ` +'1' + 1 ` result is ` 2 `.

Additional Notes:

http://jibbering.com/faq//faq/notes/type-conversion/

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.

From: Dr J R Stockton on
In comp.lang.javascript message <4bb5257b$0$286$14726298(a)news.sunsite.dk
>, Thu, 1 Apr 2010 23:00:03, FAQ server <javascript(a)dotinternet.be>
posted:

>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. Since plus (` + `) is
>also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `.

/Non sequitur/ (although true). Needs rewriting.


Variables are not typed; their values are.
Where the context needs it, type conversion occurs automatically.

The operator "plus" (` + `) adds when its operands are numeric or
Boolean; other non-String operands are converted to String, then the
operator concatenates. So, ` '1' + 2 ` and ` 1 + '2' ` give ` '12' `.

To convert a String operand to a Number, one can use ` +operand ` or `
Number(operand) ` or ` parseInt(operand, 10) ` or ` parseFloat(operand)
`.

Form control values are strings, as is the result from a ` prompt `
dialog. Convert these to numbers before performing addition by using
the unary ` + ` operator: ` +'1' + 1 ` result is ` 2 `.


// "varname" is too restrictive, since the item may be a function or
// method call result. Use "operand" instead.

// Note : the wording needs to be correct for all cases that it does not
// exclude, including such expressions as [1, 2.0, 3] + isFinite .

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)