Jump to content


Photo

What Exactly Does += Mean?


  • Please log in to reply
5 replies to this topic

#1 Stephenius

Stephenius

    Member

  • Member
  • PipPipPip
  • 68 posts
  • LocationIreland

Posted 03 January 2013 - 11:24 AM

Hi there all,
Can somebody tell me what exactly += means and how exactly does it differ with =? I kinda get it by trial and error but I'm looking for more of a definition.



http://jsfiddle.net/GdBBf/46/
When I run this function with +=, the first and second div show up in a single alert box. So I know this is because I am using += but what exactly += telling the variable to become?



http://jsfiddle.net/GdBBf/47/

When I run this function only with =, then only the 2nd
<class="span"> shows up and not the first <class="span"> Why didn't both fire sequentially as the .each function looped through the <class="span">?


Stephen


[ps. I'm not too familiar with fiddlejs --> does that stuff stay up there forever?]

Edited by Stephenius, 03 January 2013 - 11:32 AM.

  • 0

#2 Ben

Ben

    Administrator

  • Administrators
  • 5,682 posts
  • LocationGainesville, Florida

Posted 03 January 2013 - 11:48 AM

Here's a simple example. Say you have this:

x = 1;
x += 2;

The result of x should be "3". The += adds whatever is on the right side of the "+=" to whatever is on the left. It's the same as saying:

x = 1;
x = x + 2;

In your sample, when you click the button, you use jQuery to get the value of each .span, adding it to "x" each time. The alert message that appears after the loop completes displays the values for all .spans in the page.

The first time it loops, it grabs the value for the first .span: "Nationality: " and adds it to the x variable. The second time it loops, it gets the second value " Irish", and adds that to x. Once the loop completes and there are no more .spans to access, Javascript uses an alert message to display the final value for x.

Hope that helps?
  • 1
Benjamin Falk
Falken Creative : Twitter

#3 Stephenius

Stephenius

    Member

  • Member
  • PipPipPip
  • 68 posts
  • LocationIreland

Posted 03 January 2013 - 12:16 PM

Here's a simple example. Say you have this:

x = 1;
x += 2;

The result of x should be "3". The += adds whatever is on the right side of the "+=" to whatever is on the left. It's the same as saying:

x = 1;
x = x + 2;

In your sample, when you click the button, you use jQuery to get the value of each .span, adding it to "x" each time. The alert message that appears after the loop completes displays the values for all .spans in the page.

The first time it loops, it grabs the value for the first .span: "Nationality: " and adds it to the x variable. The second time it loops, it gets the second value " Irish", and adds that to x. Once the loop completes and there are no more .spans to access, Javascript uses an alert message to display the final value for x.

Hope that helps?


I'm glad I asked because this is a perfect explanation and I wasn't clear about what I was doing. Now I am.

Here I added var x ="My"; and the stuff on the right is added to the stuff on the left like your said ---.> http://jsfiddle.net/GdBBf/58/

Here I added var x = "My"; and then used the = sign instead of += and it replaces what is in the x variable. --> http://jsfiddle.net/GdBBf/59/
*** I think rather than replacing what is in var x ="My"; --> It just grabs the last item that looped through the .each function which was the
Irish Span (would i be right here?) *******

Thanks a lot.

By the way, do you know what happens to the stuff on fiddlejs -- is it left there forever?
Do you use fiddlejs for this type of question/example?

Edited by Stephenius, 03 January 2013 - 12:36 PM.

  • 0

#4 Ben

Ben

    Administrator

  • Administrators
  • 5,682 posts
  • LocationGainesville, Florida

Posted 03 January 2013 - 01:21 PM

I think rather than replacing what is in var x ="My"; --> It just grabs the last item that looped through the .each function which was the
Irish Span (would i be right here?)

Correct.

By the way, do you know what happens to the stuff on fiddlejs -- is it left there forever?
Do you use fiddlejs for this type of question/example?

I can't really comment on this -- it isn't something I use. You might want to check the FiddleJS support or FAQs or something. Though I don't use it much, it is useful for questions where the code needs to run, and means it's a bit easier for me to help you with any questions without having to manually create my own files and copy/paste code.
  • 0
Benjamin Falk
Falken Creative : Twitter

#5 Stephenius

Stephenius

    Member

  • Member
  • PipPipPip
  • 68 posts
  • LocationIreland

Posted 03 January 2013 - 01:43 PM

Correct.


I can't really comment on this -- it isn't something I use. You might want to check the FiddleJS support or FAQs or something. Though I don't use it much, it is useful for questions where the code needs to run, and means it's a bit easier for me to help you with any questions without having to manually create my own files and copy/paste code.


I'm going to look into it a bit more. I think there are other programs like fiddlejs too. But that's why I used it because it makes testing and asking questions easier for other people & they should have to stress the minimum to get an answer!
Thanks again.



  • 0

#6 grabenair

grabenair

    Advanced Member

  • Member
  • PipPipPipPip
  • 361 posts
  • LocationIowa

Posted 03 January 2013 - 03:35 PM

I myself am just learning jQuery. But I just want to point out that the tutorials that I am using all tell me to put the link to the jQuery library first and then the link to the jQuery file that you write.
  • 0
Standards will change. But your website needs to remain in serving its audience.
My Portfolio site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

★★★★★ 5 Star Rated Web Developer Course - check it out now!