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!
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
Why doesn't using the 'message +=' more than once, for eg. three times, repeat that message three times as well?
Emily Peregrine16,848 Points
I'm not entirely sure what your asking, but I'll explain how the
+= works and that should cover it
Okay so lets say we have a message,
var message = "Hello, ";
we now want to add our name to the message, there are two main ways we could do this.
message = message + "Emily"
message += "Emily"
Both statements have the same outcome, but the second one is shorter.
When we use the
If we were to do this mulitple times, it may look something like this:
var msg = ""; msg += "Hello, "; //msg == "Hello, " msg += "Emily"; //msg == "Hello, Emily" msg += "! How are you?"; //msg == "Hello, Emily! How are you?" msg += msg; //msg == "Hello, Emily! How are you?Hello, Emily! How are you?"