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

Why separate the variable in a function?

Watching the Functions video in Intro to Programming, and we wrote this code:

var sayHello = function () {
    var message = "Hello";
    message = message + " World!";
    console.log(message);
}

What is going on with the "message = message + " World!"? What is the advantage of separating it? Why didn't we just define the variable "message" as "Hello World!" from the start? Like so:

var sayHello = function () {
    var message = "Hello World!";
    console.log(message);
}

a little confused...Also, what is the "message = message + "World" called? An element? A variable? An object?

4 Answers

Hey Jeff : All this example is showing you is that you can use something called 'concatenation' in order to define a statement. For example you could receive a user's name from a form on an HTML page and then add the user name with another string to welcome the user with a pre coded message. In this example you can imagine receiving a string that is the user's name and then instead of console.log() yielding "Hello World" it could say something like - "Welcome Jeff" - which would be useful for some interactivity on your site. This is a simple example, but it's setting you up for more advanced interactivity where you want to use your site to 'respond' to user input. For clarification the : "message = message + World" is a statement and it is executed when called by your HTML page. In our example situation from above when the user would hit a submit button on your HTML page. Hope this helps a bit !

Ok, thank you! That makes sense. Even though in this particular case it was unnecessary, I can see how it would be useful when making a site interactive.

As for the concatenation, with the interactivity in mind, does this mean for a site we would have written something more like:

var welcome = prompt("Greetings, what is your name?");

var sayHello = function () {
    var message = "Hello";
    message = message + welcome + "!";
    console.log(message);

Then the user would get a prompt asking their name, and after entering it, would get back an alert with "Hello [username]!" Is that correct?

I'm not a pro in javascript but I believe that it isn't a advantage by separating it. I believe the video was made that way to show how it could be done and the possiblities. The more code you make, the more work it is for the browser/computer. And less work is better.

Hey Jeff,

You can technically write

   var message = "Hello World";

as a substitute it to the console.log() method and it will give you the same results. However, I think they are trying to demonstrate some of the different things you can do with Javascript code. message = message + " world!" is still a variable assignment (to answer your last question), but it shows that the local variable "message" is being reassigned to the concatenation of two strings. This demonstrates that after the concatenation, console.log(message) will output "Hello World!" instead of simple just "Hello."

the point was to show you how you can RE-assign variable values