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 trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Christopher Wallace
Christopher Wallace
6,277 Points

why does he use the html += in the answer,, I'm confused to why is not just html + ????

why does he use the html += in the answer,, I'm confused to why is not just html + ????

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Christopher,

The += is shorthand, used to set this variable equal to itself plus the item on the right. Easier to explain in code:

var html = "<p>Hey there ";
html += "John!</p>";             // == "<p>Hey there John!</p>"

// If he just used `html +` he would have to add `html = ` before it to get the same result:
var html = "<p>Hey there ";
html = html + "John!</p>";

// You also have -=, *=, /= which again is shorthand.
// This can apply to numbers as well.

var sum = 10;    // sum = 10
sum += 10;       // sum = 20       -- sum = sum + 10
sum /= 5;        // sum = 4         -- sum = sum / 5