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



-----------------------------------------------------------------------
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:
` Number(varname) ` or ` varname*1 ` or ` varname-0 ` or
` parseInt(varname, 10) ` or ` parseFloat(varname) ` or ` +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_notes/type_convert.html

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/12a450e5-4818-4a09-9878-cd7c6cd2a248.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.

.



Relevant Pages

  • Re: FAQ Topic - How do I communicate between frames in a web browser? (2009-08-28)
    ... september.org>, Mon, 31 Aug 2009 23:39:57, Garrett Smith ... Web developers tend to like IE less and may favor Opera or Firefox more and may also tend to spoof the UA string, sometimes humorously, sometimes masquerading as another browser, copying its userAgent string exactly. ... The FAQ does not mention this strategy and I do not know of an example where it has been used. ... We also have |fwin| that points to a a frame's content window, |iframeEl| points to an IFRAME element, the string "iframeName" which stands for the NAME of the FRAME or IFRAME. ...
    (comp.lang.javascript)
  • Re: FAQ Topic - How can I create a Date from a String? (2009-10-05)
    ... NaN and might be 0NaN. ... You do realize, of course, that NaN is not a valid ISO8601 format. ... A Date Object is generally converted to String in order ... complexity WRT explaining ISO8601, in a javascript FAQ. ...
    (comp.lang.javascript)
  • Re: FAQ Topic - How can I create a Date from a String? (2009-10-05)
    ... once in String. ... is no example in the FAQ. ... ISO8601 format. ... HTML 5 deviates from ISO8601, in that it allows the year to be any ...
    (comp.lang.javascript)
  • Re: FAQ Topic - How can I create a Date from a String? (2009-10-05)
    ... NaN and might be 0NaN. ... You do realize, of course, that NaN is not a valid ISO8601 format. ... was to show that your "NaN" string is going to be ... We can always revisit this for the FAQ, and what is done so far is at ...
    (comp.lang.javascript)
  • Re: Automation of comp.lang.javascript FAQ
    ... I cannot see a blanket statement about how string manipulation is "best ... and that is out of place in the FAQ. ... language whereas "Regular Expression" only occurs in descriptions of the ... expression is a pattern of text that consists of ordinary characters ...
    (comp.lang.javascript)