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 3 of 3

Thread: +=

  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    +=

    how can you make a += work so if you add 5+5 with += it would come out as 55 how would you make it come out as 10?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    It will automatically come out as 10 if you make sure that both sides of the "+=" are numeric values. If you (for example) read the value of an input box, it will always be a string (even if you entered a number). You'll have to convert it into a numeric value before adding 5 to it

    Example
    Code:
    var myValue = document.getElementById('myinput').value;
    myValue = Number(myValue); // convert it to a numeric value if possible, otherwise it will be "NaN"
    myValue += 5;   // mathematically add 5 to this value
    Last edited by devnull69; 07-29-2011 at 11:03 PM.

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    thanks


  •  

    Posting Permissions

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