Jump to content

What Exactly Does += Mean?


Moloney

Recommended Posts

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
Link to comment
Share on other sites

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?

  • Upvote 1
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...