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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FAQ server

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

    -----------------------------------------------------------------------
    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(varnam e, 10) ` or ` parseFloat(varn ame) `.
    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:






    --
    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.

  • Jorge

    #2
    Re: FAQ Topic - Why does 1+1 equal 11? or How do I convert a stringto a number? (2008-11-01)

    On Nov 1, 1:00 am, "FAQ server" <javascr...@dot internet.bewrot e:
    -----------------------------------------------------------------------
    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(varnam e, 10) ` or ` parseFloat(varn ame) `.
    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 `.
    >
    Unless the string contains a number whose base !== 10, (in which case
    you'd use parseInt(string , base)), wouldn't it be better to clearly
    recommend using the (+string) form as the best, as the one that will
    yield the less surprising results, most of the times ?

    Because it's less surprising to find out that (+"10px") yields NaN,
    than that parseInt("01234 5e6") yields 5349, or even that
    parseInt("12345 e6", 10) yields 12345, or that parseFloat("") yields
    NaN.

    Although some may prefer parseFloat().

    If not, I think it would be a good idea to at least elaborate a little
    bit further on the topic.

    But that’s just my "tuppence".
    --
    Jorge.

    Comment

    Working...