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 Basics (Retired) Storing and Tracking Information with Variables Combining Strings

jeremy lowe
jeremy lowe
3,132 Points

Assigning var values

Hi,

So I'm new to programing in general. Why can't you just assign a value to a variable, to use it.

I.E. var firstName ="Bob"; document.write(firstName);

Rather to get it to work, you have to: var firstName ="Bob"; var message = firstName; document.write(message);

It seems like the same things?

Thanks

3 Answers

rydavim
rydavim
18,814 Points

Your example code of how you expect it to work should indeed work. Does it not?

// This is perfectly fine to use...
var name = "Rydavim";
document.write(name);

// You could also do...
var name = prompt("What is your name?");
document.write(name);

EDIT - In this particular instance, I think the variable message is being used because you're concatenating various strings and variables together.

So you're using message not because it won't work otherwise, but because you're matching a meaningful variable name to a value. The firstName variable isn't a good name for the string, "Hello, [yourname]" because it's no longer just your first name.

Writing the variable message to the document also makes it more reusable, since you can always change the value of message, and it will still make sense to write that to the document.

jeremy lowe
jeremy lowe
3,132 Points

Hi,

This was my first post, so I'd like to say Thank you!

Yes, I agree the "firstName" was a bad variable name, i used it because the challenge after this video asked to do something similar. I take it, the example code above should print your name? It doesn't seem to work for me?

The challenge was;

  1. set a variable as your first name
  2. set a variable as your last name
  3. have it write your first and last name

Is this correct?

var firstName = "Jeremy";
var lastName = "Lowe";
var fullName = firstName + lastName; 
docuement.write(fullName); ```

i am not sure, what part of my code is wrong but the .JS console states:
ReferenceError: docuement is not defined
rydavim
rydavim
18,814 Points

You've got document spelled docuement. It's much easier to spot minor error in other people's code.

You'll also probably want a space between your first name and last name in the concatenation bit.

As for the example code - I'm not sure why it wouldn't work. If you're running it on just any old page, it may not want to access the document. Does it work if you use console.log instead? Sorry about that!

// This is perfectly fine to use...
var name = "Rydavim";
console.log(name);

// You could also do...
var name = prompt("What is your name?");
console.log(name);
jeremy lowe
jeremy lowe
3,132 Points

Dang! i went over it a few times too. Thank you for catching that, it does work now. I was using it on an old workspace.

Much appreciated!