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 The Variable Challenge

Jessica Andhika
PLUS
Jessica Andhika
Courses Plus Student 413 Points

How do I insert a space in a variable? e.g. var1 + " " + var2 I tried putting   but it didnt show up..

This is my sample workspace..

var firstName = prompt("What's your name?");
var favAnimal = prompt("Name your favourite animal.");
var adjective = prompt("Describe yourself in 1 word.");
var hobby = prompt("Name your favourite hobby.");
var verb = prompt("What do you feel like doing right NOW?");
var noun = prompt("What is your favourite meat?");
var story = "<h2>There was once a " + adjective + "&nbsp;" + favAnimal + "named " + firstName + ".</h2>";
document.write(story);

The " " did not appear, so the sentence became e.g. There was once a cute turtlenamed Eric. Thank you.

7 Answers

jason chan
jason chan
31,009 Points
var firstName = prompt("What's your name?");
var favAnimal = prompt("Name your favourite animal.");
var adjective = prompt("Describe yourself in 1 word.");
var hobby = prompt("Name your favourite hobby.");
var verb = prompt("What do you feel like doing right NOW?");
var noun = prompt("What is your favourite meat?");
var story = "<h2>There was once a " + adjective + " " + favAnimal + " named " + firstName + ".</h2>";
document.write(story);

refactored try now!

Samantha Inmon
Samantha Inmon
5,127 Points

In addition to Jason's comment, to separate the words "turtle" and "named", you just need to write a space inside the quotes before the word like this:

favAnimal + " named " + firstName + rest of code...

jason chan
jason chan
31,009 Points
var firstName = prompt("What's your name?");
var favAnimal = prompt("Name your favourite animal.");
var adjective = prompt("Describe yourself in 1 word.");
var hobby = prompt("Name your favourite hobby.");
var verb = prompt("What do you feel like doing right NOW?");
var noun = prompt("What is your favourite meat?");
var story = "<h2>There was once a " + adjective + "   " + favAnimal + "named " + firstName + ".</h2>";
document.write(story);

You can leave the spaces as space in quotes. You don't need the html entity. That's only for HTML.

Jessica Andhika
PLUS
Jessica Andhika
Courses Plus Student 413 Points

hey guys, thanks for the prompt help, but the space I need is between the variables adjective and favAnimal. I've also tried adding just blank space, not the HTML, and it didn't show up...

Idan Melamed
Idan Melamed
16,285 Points

Hi Jessica If you want to add a space, you can put quotes around a space, like this:

" "

So in your example you would do:

adjactive + " " + favAnimal 

// This works , I tested it three times

var firstName = prompt("What's your name?"); var favAnimal = prompt("Name your favourite animal.")+ " "; var adjective = prompt("Describe yourself in 1 word."); var hobby = prompt("Name your favourite hobby."); var verb = prompt("What do you feel like doing right NOW?"); var noun = prompt("What is your favourite meat?"); var story = "<h2>There was once a " + adjective + " " + favAnimal + "named " + firstName + ".</h2>"; document.write(story);

// I added a space --> var favAnimal = prompt("Name your favourite animal.")+ " "; <--