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

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);