By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

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

P: n/a
-----------------------------------------------------------------------
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(varname, 10) ` or ` parseFloat(varname) `.
Prompt and form control values are strings, as is the result from
a prompt window. Convert these to numbers before performing addition.

Additional Notes:

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.javascript 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
Share this Question
Share on Google+
2 Replies


P: n/a
In comp.lang.javascript message <47***********************@news.sunsite.
dk>, Mon, 31 Dec 2007 00:00:02, FAQ server <ja********@dotinternet.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(varname, 10) ` or ` parseFloat(varname) `.
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(varname, 10) ` or ` parseFloat(varname) `.
/* 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.demon.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

P: n/a
In comp.lang.javascript message <Nq*********************@giganews.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.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Jan 1 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.