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 The Build an Object Challenge, Part 2 Solution

What is the purpose of var message?

Hi,

could you explain me why do we add var = message ' '; ? What is the purpose of adding quote marks with blank space?

Thanks

Everybody's answers here are great. It gave me a little more understanding to what I have come to know as "it's just the way it's done"

4 Answers

Luca, it's var message = '';, not var = message ''

What Dave is doing is declaring a variable named message. So why the empty String? If you don't initialize the variable you'll probably get a complaint. You can try it. Declare the variable but leave the = '' off and see what happens.

David Bath
David Bath
25,940 Points

I think you misread it. In the video it says

var message = '';

It is assigning an empty string value to the message variable.

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points
var message = '';  || var message ;

Probably in the video, Dave created this variable outside of the function. If it's outside the variable, it's a global variable(anybody can use it) and what it does is you can manipulate the same variable in different functions as you wish and make things pretty simple.

Check out this link and you'll understand what I'm talking about http://www.w3schools.com/js/js_scope.asp

But why do we ihave to initialize only the variable message and not the other ones?

Tushar Singh
Tushar Singh
Courses Plus Student 8,692 Points

Because as you save seen in the link I gave you, If the variable is defined in a function, it's a local variable. Now if you want to use that variable only in that function why would you create a global one although you can do that but why make things complicated.

In plain english, if you want some variable to be used in different functions, you declare that variable globally(simply less typing and more functionality), or you can actually declare the variable in different functions as well, just make sure you write "var" everytime.

Now I think writing var again and again would be a real pain in the ***, don't you think that?

Actually this stuff gets a little more complicated than what I've described but let's keep that for another day., first you got to understand this part.It's very important you understand this concept.

var number =10;  // this variable is different from the other one not the same, because you added a keyword var and created a global variable.
function num() {
var number = 5;
}

// take another eg, this time true or false

var gameStart = false;

function start() {
// some conditions here 

gameStart =true;
// what you did here is changed the global variable but this true value will be only in this function.
}

// outside this particular function gameStart is still false

Let me tell you really why are you struggling on this part because probably you din't make something on your own. Do that because that's the only way to get a clear picture really.It really helps when you create something on your own.

Tushar Singh
Tushar Singh
Courses Plus Student 8,692 Points

All being said, let's come back to the message variable, after your particular function your message variable will still be an empty string, you can change it again in another function (as message is empty).

Tushar Singh
Tushar Singh
Courses Plus Student 8,692 Points

You are not using many variables at this stage, that's why Dave basically gave you an overview how you can declare a variable and change it in function at later stage.

David Bath
David Bath
25,940 Points

Actually, the reason you need to initialize the message variable to an empty string is because of the way it is used in the loop. Something like:

message += '<h2>Student: ' + student.name + '</h2>';

If you did not initialize the variable to an empty string, the value would be determined to be "undefined" initially. So your message would end up being something like "undefined<h2>Student: Luca Di Pinto</h2>". Clearly that's not what you'd want. This is just something to be aware of when concatenating strings like this.