Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPratik Rai
6,856 Pointswhy are we adding html += ?
I don't understand why are we adding html+=. could anybody plz explain that to me?
7 Answers
Mustafa Başaran
28,046 Pointshi Rai,
I prepared a simpler example based on the current exercise and explained what html += does via http://codepen.io/uldiz/pen/YXzxLp?editors=101
Please check out the explanation on js. On each instance of the loop, it is taking an empty string, html, and appending some stuff to it. Is it OK now? I hope that this helps.
Ognjen Jevremovic
13,028 PointsHello Rai. How are you? First of I'd like to congratulate you for making it so far in the track! But yet, there's still a lot to learn. So keep on hacking mate!
To answer your question: if you just used
html = '<div style="background-color:' + rbgColor + '"></div>';
you would actualy asign a new value to a html variable (which in this case is a contatination of strings that actualy creates that circle colored div) each time the loop runs. Meaning, for example, if the first time when the loop run the blue color circle was created, the next time that same loop run the violet circle div was created and since you didn't use += but instead you used just = you actualy assigned a new value to that html variable. Meaning, you have actualy overriden that blue circle colored div with a new, violet colored div circle, created when the loop run the 2nd time. And then, when the loop run for the 3rd time, let's assume tomato colored circle div (Guil's favorite color, lol) was created that actualy overriden that violet circle that overriden that blue circle created when the loop first run.
Where as, if you use +=, you are actualy adding that new value of an html variable, or in this case the new violet colored circle, to an already existing blue colored circle, which will not be overriden but will instead add up that violet circle to an already existing blue colored circle (value of an html variable).
Long story short, if you ommited that + before an = (meaning you were using html=) you would end up with only one colored circle that was generated when the loop run for the 10th time, that overriden the colored circle generated when the loop run for the 9th time, and that circle overriden the another circle generated when the loop run for the 8th time, etc. Meaning each time the loop runs new colored circle overides the circle generated one step before.
I hope this explanation is not so confusing, as if so, I'd make a simple few line JS in my workspace and share it with ya. Keep us updated with your progress.
All the best!
Mustafa Başaran
28,046 PointsHi Rai, html += '...' is a way of building/adding HTML elements together. In the for loop example, we are creating ten divs with random RGB background colors. In each instance, we are selecting values for red, green and blue and then combining them in rgbColor variable Once this part is done, we are adding these blocks together via adding them on an empty html variable in each instance of the for loop. Please note that html +='..'; is same as html = html + '...'; in string concatenation
I hope that this helps.
Jayden Spring
8,625 PointsYour variable html is of type string. If you use the += operator on a string all it is doing is appending the string after the += to the html string.
It can be used on any string variable you declare within your script
Jonathan Cousins
4,160 PointsString concatenation ftw!
Pratik Rai
6,856 Pointshi mustafa your explanation is very good. html += ' ' ; is same as html = html + ' '; but why are again adding the variable html. I hope you understand what my problem is.
Pratik Rai
6,856 Pointsthanks man that helped me a lot. understood but need practise.
Mustafa Başaran
28,046 PointsI highly recommend jQuery and AJAX intro courses in that order following this one. They are building further on this idea and teaching how to create interesting javascript applications.
Joseph Casey
6,782 PointsJoseph Casey
6,782 PointsIt isn't actually necessary since the code can be shortened by adding the print function inside the for loop. You'll have the same results with a cleaner code set.