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 Data Using Objects Accessing Object Properties

Question regarding the last 3 lines.

Hi all! So, I had to concatenate the last 2 lines of the code (above the print(message) call) to make sure it looks like Dave's on the browser. (using Chrome)

I know it isn't a big deal, but I feel like I need to know why that is.. πŸ™ƒπŸ€“

Thanks in advance for any help..

Have a great day & Happy coding!!! πŸŒ»πŸ™πŸΌ

var person = { name : 'Sarah', country : 'US', age : 35, treehouseStudent : true, skills : ['JavaScript', 'HTML', 'CSS'] }; function print(message) { var div = document.getElementById('output'); div.innerHTML = message; } var message = "<p> Hello. My name is " + person.name + "</p>"; message += "<p> I live in the " + person.country + "</p>"; person.name = "Rainbow Dash"; message += "<p> But I wish my name was " +person.name + "</p>"; person.age += 1; message += "<p> My age is now " + person.age + "</p>";

πŸ‘‰πŸΌmessage += "<p> I have " + person.skills.length + " skills: " + person.skills.join(", ") + "</p>";

print(message);

2 Answers

Actually he has concatenated the last two strings. On the message variable he has used a += then written the paragraph. The += signs are used frequently in JS for a number of reasons of which one of the reasons is to take a set of strings and put them together.

Check out this link for some detail:

https://masteringjs.io/tutorials/fundamentals/string-concat#:~:text=The%20%2B%20Operator,used%20to%20concatenate%20two%20strings.&text=You%20can%20also%20use%20%2B%3D,for%20a%20%3D%20a%20%2B%20b%20.&text=If%20the%20left%20hand%20side,hand%20side%20to%20a%20string.

Concatenating means joining up strings. If you dont concatenate the above strings then it would just be treated as separate lines. By adding the strings together, it will treat it as a single complete string. I hope that made sense.

For example, lets suppose I had two variable:

var string1 = "Hello my name is";

var string2 = "furkan";

/* No concatenation and the strings will be treated separately. But I want to concatenate the two so it says "Hello my name is furkan"... So, you can use the plus operator (+) to join the two strings up */

Hi Furkan, I was more wondering how come Dave didn't have to concatenate them and still ended up with those 2 lines as 1.