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

Separate lines on the document using document.write();

How would I put the concatenated string of "What do you think of JavaScript so far?" on a new line?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/main.css">
  <title>Using the prompt() method.</title>
 </head>
<body>
  <div class="container">
  <h1>Using the prompt() method</h1>
  <script src="scripts.js"></script>
  </div>
</body>
</html>
var visitor = prompt("What is  your name?");

var message = "Hello " + visitor;

document.write(message);

message += ". What do you think of JavaScript so far?";

document.write(message);

1 Answer

Stephan Olsen
Stephan Olsen
6,650 Points

You could add a line break tag to the message:

message += "<br>What do you think of JavaScript so far?";

Also I believe concatenation is only when you're putting strings together, here you're simply changing the variable. The proper way to go about it is probably building a paragraph tag around it though. Like this:

var visitor = prompt("What is  your name?");

var message = "<p>Hello " + visitor + "</p>";

document.write(message);

message = "<p>Hello " + visitor + " What do you think of JavaScript so far?</p>";

document.write(message);