JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Accessing Object Properties

Sandra AlSaig
seal-mask
.a{fill-rule:evenodd;}techdegree
Sandra AlSaig
Front End Web Development Techdegree Student 8,548 Points

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 */