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 Create a variable with a string

Cordez Coleman
PLUS
Cordez Coleman
Courses Plus Student 1,231 Points

Use the document.write() function

Maybe i'm not understanding the concept of the document.write(). I've tried doing the code in different ways and i'm just not understanding what i'm doing wrong.

app.js
var player = 'Jasmine'
document.write('player');
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>

2 Answers

Tyler Durden
Tyler Durden
2,406 Points

Yep, when ever you use a method and want to pass a variable through it, you just put the variable without any quotes.

When you put the quotes, the method thinks its a string, so it will just output "player".

So if var blah = 4; and you want to output that, just:

document.write(blah);

Oh, and by the way, you should really get used to using document.getElementById('the id name in your HTML where you want this to output to').innerHTML = whatever you want

You basically add an id="name of your choice" to anywhere on your HTML skeleton where you want your output of your JavaScript to be displayed. The document.write(); is super 1990's.

So you could have a <div> </div> on your HTML page that you want content to be displayed, so you could do:

<div id = 'output'> </div>

Then, in your javascript, you could "target it" by using getElementById, like this:

var num1 = 5;
var num2 = 7;

if(num1 > num2) {

document.getElementById('output').innerHTML = num1 + ' is bigger than ' + num2;
}
else {
document.getElementById('output').innerHTML = num2+ ' is less than ' + num1;
}

So yeah that's how you use it. Good luck!

Steven Parker
Steven Parker
231,236 Points

Note that most of these comments apply to the code that was provided by the challenge and not the part written by the student.