1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

+= javascript What does it mean?

Discussion in 'General Programming Chat' started by dllll, Oct 20, 2014.

  1. dllll

    dllll Registered Member

    Joined:
    Jun 20, 2014
    Messages:
    90
    Likes Received:
    43
    Im learning javascript and im confused on what this means +=
    I know the + is concatenation and = is equals but im still confused, i looked it up of course on w3schools but im still confused lol?
    Its being used between 2 variables on the code im editing.
     
  2. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,063
    Likes Received:
    2,872
    Gender:
    Male
    That's for appending value to a variable...

    example..
    Code:
    var1 = 'a';
    var1 += 'b';
    alert(var1)
    // var1 will now become ab
    
    it is essentially the same code as

    Code:
    
    var1 = 'a';
    var1 = var1+ 'b';
    alert(var1);
    //var1 will now become ab
    
     
    • Thanks Thanks x 2
  3. dllll

    dllll Registered Member

    Joined:
    Jun 20, 2014
    Messages:
    90
    Likes Received:
    43
    Thanks i understand it now :)
     
  4. Repulsor

    Repulsor Power Member

    Joined:
    Jun 11, 2013
    Messages:
    712
    Likes Received:
    267
    Location:
    PHP Scripting ;)
    g0g0l cleared it up for you.

    Just FYI its .= in php.

    Adds something to the existing value of the variable.
     
  5. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    Note that if your variables are numbers, it's an shorthand for addition.

    Code:
    var a=1,b=2;
    b += a; // => 3
    // Same as b = b + a;
    
    Just remember this: for strings + is concatenation and for numbers it's addition. This is called operator overloading (same operator acts differently depending on the operand types).
     
    • Thanks Thanks x 2