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

JORGE GONZALEZ
JORGE GONZALEZ
3,276 Points

What 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.

https://drive.google.com/folderview?id=0ByGkh3KXsrkmfjNXeHZhVVJmX293SG1naEJvbE1fZVFTN1V5dWNobW5kdW9xVWpoWE9JUjg&usp=sharing

JORGE GONZALEZ
JORGE GONZALEZ
3,276 Points

Thank 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?

4 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

This 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
JORGE GONZALEZ
3,276 Points

var 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
Jeff Lemay
14,268 Points
var 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 +'';
Grace Kelly
Grace Kelly
33,990 Points

Hi 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
JORGE GONZALEZ
3,276 Points

In 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
Davis Wilkinson
3,852 Points
html+= '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
JORGE GONZALEZ
3,276 Points

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?

Davis Wilkinson
Davis Wilkinson
3,852 Points

What exactly is your code outputting?

JORGE GONZALEZ
JORGE GONZALEZ
3,276 Points

It 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
Jacob Bender
15,300 Points

In 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;