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 trialAlaa Kassab
240 PointsI don't understand why do you repeat the name of the value 4 times? and why it is only showing one time in the console?
I mean :
message += message += message += message +=
so I expected to see the value of message which is "Hello" to appear on the console 4 times
2 Answers
Allan Cheow (he/him)
Full Stack JavaScript Techdegree Student 12,808 PointsHi Alaa Kassab,
The process of concatenation used in this lesson is the addition of string onto it's previous value.
The best visualization is
//This line request user to input name
const name = prompt("What is your name?");
//This line of code assign the first set of string into the variable message
let message = "Hello " + name + ". Welcome to my music site. ";
//Result: Hello Allan. Welcome to my music site.
//This next line adds additional strings to the existing result, above
message += "I'm so happy you came by to visit, ";
//Result: Hello Allan. Welcome to my music site. I'm so happy you came by to visit,
//This next line adds name to the existing result, above
message += name;
//Result: Hello Allan. Welcome to my music site. I'm so happy you came by to visit, Allan
//This next line adds additional strings to the existing result, above
message += ". Feel free to come again to listen to more music.";
//Result: Hello Allan. Welcome to my music site. I'm so happy you came by to visit, Allan. Feel free to come again to listen to more music.
console.log(message);
Since it's building on each line, it will not output the "Hello" four times.
This would be the same as your example of "message += message += message += message +=". It will assign the value from the right side of the equal (=) first and then the left, one at a time.
So, first it'll evaluate
message += message += message += message +=
then
message += message += message +=
followed by
message += message +=
lastly
message +=
Hope this helps and sorry for the long post.
Clinton Hays
Front End Web Development Techdegree Graduate 18,156 PointsHi Alaa Kassab!
I skimmed through the video and I think what you should be looking at is how many times console.log(message)
called. Every time the message variable is called, it is concating new strings to the original message which is then logged to the console once.
Hope that helps!