473,574 Members | 2,269 Online

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

-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
-----------------------------------------------------------------------

Javascript variables are loosely typed: 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 deciding what + does. To overcome this, first convert the
string to a number. For example: ` +varname ` or ` Number(varname) ` or
` parseInt(varnam e, 10) ` or ` parseFloat(varn ame) `.
Prompt and form control values are strings, as is the result from
a prompt window. Convert these to numbers before performing addition.

http://www.jibbering.com/faq/faq_not...e_convert.html

http://msdn.microsoft.com/library/de...7c6cd2a248.asp
--
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javas cript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.

Dec 31 '07 #1
2 2205
In comp.lang.javas cript message <47************ ***********@new s.sunsite.
dk>, Mon, 31 Dec 2007 00:00:02, FAQ server <ja********@dot internet.be>
posted:
>FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
>Javascript variables are loosely typed: the conversion between a
string and a number happens automatically.
Not a helpful way of starting, when the situation is that the conversion
needed by the coder does NOT happen.
Since plus (+) is also
used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `: the
String deciding what + does. To overcome this, first convert the
string to a number. For example: ` +varname ` or ` Number(varname) ` or
` parseInt(varnam e, 10) ` or ` parseFloat(varn ame) `.
Prompt and form control values are strings, as is the result from
a prompt window. Convert these to numbers before performing addition.

Javascript variables are not typed by declaration, but are variants
typed by content; the type (String, Number, Boolean, ...) is stored, as
well as the ostensible value.

Prompt and form control values are of type String, as is the result from
a prompt window. They must be converted to Number for arithmetic.

Type conversion happens automatically, as required for the operator.
For example, the two operands of star (*) are always used as Numbers.

Plus (+), with two operands, is used both for addition and for string
concatenation. It is a language feature that concatenation occurs
unless each operand is already Number or Boolean - so ` 1 + 2 ` gives `
3 ` and ` 1 + true ` gives ` 2 ` but ` "1" + 2 ` gives ` "12" `.
However, unary plus, prefixing a single operand of any type, always
results in a Number (maybe NaN); so ` +"1" + 2 ` and ` 1 + +"2" ` and `
+"1" + +"2" ` give ` 3 `.

A decimal string, perhaps with leading and/or trailing whitespace, is
best converted by ` +varname ` or ` Number(varname) `. A decimal string
which may have visible trailing characters is converted by
` parseInt(varnam e, 10) ` or ` parseFloat(varn ame) `.
/* That needs consideration to verify that all statements are exactly
true whatever types, including Boolean, Function, Object, are used.
I've checked, for example, LZ*3 and LZ+3 with LZ a function. */

Note : with ` ` used as above, it is better to use " rather than ' for
strings.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Dec 31 '07 #2
In comp.lang.javas cript message <Nq************ *********@gigan ews.com>,
Tue, 1 Jan 2008 12:00:41, Randy Webb <Hi************ @aol.composted:
>Dr J R Stockton said the following on 12/31/2007 4:02 PM:
> Javascript variables are not typed by declaration, but are variants
typed by content; the type (String, Number, Boolean, ...) is stored, as
well as the ostensible value.

I don't agree with that. They are typed by how they are declared. If
you want a different type, then you have to either declare it
differently or convert it. Either automatically or intentionally.
In a declaration such as the one in
function X() { var J ; alert(9+J) }
variable J is not usefully typed (OK, "undefined" is a type).

In Javascript the type of a variable is set by its most recent
assignment.

Intentional conversion of a variable, as in S = "66" ; S = +S ; is
by reassignment. In T = +S + 2 ; the type of S is not changed. The
value taken from it is changed, by the unary +, to type Number.

Granted that my wording needs minor upgrade to accommodate var J=0; .

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;