Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge Solution

Kanav Anand
Kanav Anand
2,368 Points

How do I get spacing between two concatenated declared variables like firstName+lastName?

var firstName=prompt("please Enter First Name); var lastName= prompt("please Enter Last Name); document.write(firstName+lastName);

The result will be no spacing between the two. how do I get the space.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Kanav Anand! You have a couple of options here. There's nothing particularly special about a space really. It's a character just like any other but we call it a white space because it has no visual representation. Or rather, the visual representation is a null space.

So you could do this:

console.log(firstName + " " + lastName);

This might be a bit out of the scope of where you are right now, but you could also use one of the newish JavaScript features called a JavaScript Template literal. Enclosing a string within backticks, you can use a dollar sign and curly braces to tell JavaScript to place the value of a variable in that place. For instance, using a JavaScript template literal we could write the above line with the same results like so:

console.log(`${firstName} ${lastName}`);

Because these are separated with a space inside the backticks, the space will also be present in the log statement. If you have about 9 minutes, I highly suggest you take a look at this workshop.

Hope this helps! :sparkles: