Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: Additions

  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Additions

    document.write(16/2) will write 8
    a=16
    b=2
    document.write(a/b) will write 8

    BUT
    document.write(16+2) will write 162
    and
    a=16
    b=2
    document.write(a+b) will write 162!!!

    AAAAAARRRRRGGGGGGG

    What's the deal with that?
    How am I supposed to add stuff?
    Shawn

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is because JavaScript treats them as strings rather than numbers. In JavaScript to join two strings you us "+". Try this:
    document.write(eval(16+2))

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    No need to use eval there - just enclose the addition in parentheses:
    Code:
    document.write((a+b));
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya, the + operator is what is called 'overloaded' - meaning it's result is dependant on context. Something is causing the parser to assume you want to concatenate two strings. I acutally tested

    document.write(16+2);

    and I got 18 - so I'm not sure why you get 162.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks all, will use extra parentheses ()

    Beetle: Either your strange, or I'm strange
    Shawn

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Hmm, if the first of them happen to be a string instead of a number, you could use parseInt or parseFloat on it. Or, you could simply write (0+a+b) to assure that they are handles as were they numbers.

    (All these problems lie within the automatic type conversion system. 16+'2' should not give 162 but 18, while '16'+2 should give 162; This because in the first case numbers is the "closest" conversion available while in the second, string is)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Owl
    Owl is offline
    New Coder
    Join Date
    Jun 2002
    Posts
    62
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sorry liorean, but you are incorrect on both counts.

    (0+a+b) if a is a string, will give you string concatenation
    (no matter what b is, string or num).

    16+'2' will give the exact same result as '16'+2 both 162.

    The algorithm of the addition operator is fairly simple:

    If one of the operands (left or right) is a string, perform string concatenation.

    If the two operands are numbers, perform numeric addition.

    That's it.

    I hope you don't treat your patients (assuming you will graduate)
    based on the same source of knowledge you used to give those advices.

    ( •) (• )
    >>V

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Yeah, I just noticed that. I've been fooling around in three different languages lately, and JavaScript is unlike them all on that operators aren't handled as were they either regular global functions or methods of the first object...

    Use parseFloat(string), parseInt(string), Number(string) or 1*string to explicitly get a number from a string, and then perform the addition on that.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #9
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    Use parseFloat(string), parseInt(string), Number(string) or 1*string to explicitly get a number from a string, and then perform the addition on that.
    those are the best ways.

  • #10
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    OWL:

    document.write(16+2);

    Both operands are numbers, and it still treats it as astring and write 162...
    Shawn

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    document.write(16+2) gives 18 on ie6w, moz and op7 at least...

  • #12
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    does document.write((16+2)) give 18 in all browsers?
    Shawn


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •