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

Vlad Legkowski
Vlad Legkowski
9,882 Points

+= Parameters in JS

Hi all,

I know this been covered before, but I cant get my head around it.

when we create a message+= for person.country, we are really saying to JS the following:

message = message + '<p>I live in the ' + person.country + '</p>';

But the current variable for message is:

'<p>Hello, my name is ' + person.name + '</p>';

'<p>Hello, my name is ' + person.name + '</p>'; = '<p>Hello, my name is ' + person.name + '</p>'; + '<p>Hello, my name is ' + person.name + '</p>'; ??

Do you see why I am confused?

Hope you understand what I mean.

2 Answers

Billy Bellchambers
Billy Bellchambers
21,689 Points

I see you've got yourself into a bit of a pickle there.

The += basically means this.

example:

message += string;

is same as

message = message + string;

Both statements are basically saying.

add "string" to the existing value of message.

Working example

message = "Hi my name is ";

message += " Billy.";

now message is: message = "Hi my name is " + " Billy.";

The process basically tags on the addition to the end of the current value for message.

Hope this helps clarify your confusion.

Happy coding

Vlad Legkowski
Vlad Legkowski
9,882 Points

I get this billy, but then, when we use

function print(message) { var div = document.getElementById('output'); div.innerHTML = message; }

message = "Hi my name is ";

message += " Billy.";

print(message);

at the end we get the following

(first line) Hi my name is

(second line) Billy

Why dont we get

(first line) Hi my name is

(second line) Hi my name is Billy

Billy Bellchambers
Billy Bellchambers
21,689 Points

Because at the point print(message); is fun the current value for message is already the full string Hi my name is Billy.

if however you did this.

function print(message) { var div = document.getElementById('output'); div.innerHTML = message; }

message = "Hi my name is ";

print(message); -----message value at this print function would give "hi my name is"

message += " Billy.";

print(message); --- message value at this print function would give "hi my name is billy"

This would result in the 2 step printing I believe you was expecting but as we only include one print(message) only the last one will print in your example.

Basically putting it simply a function will only use the current value for a variable at the time its running the function.

Hope that helps

Just to reinforce Billy:

When calling variables of any kind, the system looks at the current value and not what the value was in the past or in the future. The variable always returns what it is at that exact moment.