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 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
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: