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 trialJORGE GONZALEZ
3,276 PointsWhat does this javascript code mean?
If you notice, there are a few statements at the bottom that confuse me.
They are listed as this
html+= 'Code goes here' html+= 'More code here'
What does the html+= mean? In other words what is it doing and is it necessary?
I am referring to the code at the bottom right of the screenshot.
Thank you guys in advance.
4 Answers
Jeff Lemay
14,268 PointsThis is adding to the html variable which must be set above.
var html = "Hello";
html += " World!"
html += " My name is Jeff."
In the end, your html variable equals "Hello World! My name is Jeff."
JORGE GONZALEZ
3,276 Pointsvar message = '<p>Hello. My name is '+ person.name + '</p>'; message = message + '<p>But, I wish my name was '+person.name +'</p>';
This returns:
My name is "Person" But, I wish my name was "Person"
Shouldnt it return the following:
My name is "Person" My name is "Person" But, I wish my name was "Person"
In other words its not concatenating . Why is this? It does it when we deal with numerical values:
var number =6; number = number+10
6 6+10= 16.
Am I making sense?
Jeff Lemay
14,268 Pointsvar message = 'Hello. My name is '+ person.name + '';
message = message + 'But, I wish my name was '+person.name +'';
On your second line, you are resetting the value of the message variable (=) instead of appending to the end of the existing variable (+=).
To get what you want:
var message = 'Hello. My name is '+ person.name + '';
message += message + 'But, I wish my name was '+person.name +'';
JORGE GONZALEZ
3,276 Pointsyou answered it
Grace Kelly
33,990 PointsHi Jorge, the += means to add something on to a value, in this case the variable is html, but you can call it whatever you want
var html; //html has no value
html += "hello"; //html in now equal to hello
html += " world!" //html is now equal to hello world!
console.log(html); //logs "hello world!" to the console
The += makes it very easy to build up code for example creating lists, hope that helps!!
JORGE GONZALEZ
3,276 PointsIn my example:
1) html="you got" +correctAnswers + "questions right."; 2) html += '<h2> You got these questions right: </h2>;
in this case then when it runs, shouldnt the first and second lines read as follows:
1 ) "you got x questions right" 2) "you got x questions right" You got these questions right:
Note that I just used lines 42 and 43 as examples but this is the general idea.
When I run it, it doesnt run as I stated above. Why is this?
Davis Wilkinson
3,852 Pointshtml+= 'Code goes here' html+= 'More code here'
is simply shorthand for
html = html + 'Code goes here';
html = html + 'More code here';
You're simply concatenating text to the previous text and saving the new longer string into the variable html (over and over again).
So basically that format is just saving you time and space but is not necessary.
JORGE GONZALEZ
3,276 PointsIn my example:
1) html="you got" +correctAnswers + "questions right."; 2) html += ' You got these questions right: ;
in this case then when it runs, shouldnt the first and second lines read as follows:
1 ) "you got x questions right" 2) "you got x questions right" You got these questions right:
Note that I just used lines 42 and 43 as examples but this is the general idea.
When I run it, it doesnt run as I stated above. Why is this?
Davis Wilkinson
3,852 PointsWhat exactly is your code outputting?
JORGE GONZALEZ
3,276 PointsIt is outputting whats on the left of the screenshot. But I guess I just dont understand the logic.
just as an example consider these two lines of code
var message = '<p>Hello. My name is '+ 'Sarah' + '</p>'; message = message + '<p>I like that my name is'+ 'Sarah' +'</p>';
When it prints it prints the following:
Hello. My name is Sarah I like that my name is Sarah
Shouldnt it print like this?
Hello. My name is Sarah
Hello. My name is Sarah I like that my name is Sarah
Shouldnt it print like the above because message = message + '<p>I like that my name is'+ 'Sarah' +'</p>';
Jacob Bender
15,300 PointsIn this case html is a variable that was created to store content.
+= means to take existing content and add to it. So for example, if I have var html = document.getElementById("contentDiv"); and then say html+="<p>test</p>"; then a paragraph containing the word "test" will be added to the contentDiv of my html.
In general, += just means "add something to something that already exists" so if you had var total = 0; and var amount = 5; you could say total += amount; <- this would make total = 5;
JORGE GONZALEZ
3,276 PointsJORGE GONZALEZ
3,276 PointsThank you guys for the response but I dont think the code is behaving as I thought it should
In my example:
1) html="you got" +correctAnswers + "questions right."; 2) html += ' You got these questions right: ;
in this case then when it runs, shouldnt the first and second lines read as follows:
1 ) "you got x questions right" 2) "you got x questions right" You got these questions right:
Note that I just used lines 42 and 43 as examples but this is the general idea.
When I run it, it doesnt run as I stated above. Why is this?