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 Capturing Visitor Input and Writing It to the Page

create a variable named answer

create variable named answer but don't put a value in it yet

var= 'answer';
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
<script src="scripts.js"></script>

1 Answer

Brendon Butler
Brendon Butler
4,254 Points

you need to create a name for your variable so that it can be accessed within your Javascript code. You start off with var/let/const, then the name of your variable. If you want to give it a value, add an equal sign and then add your value.

// this creates a variable named answer, but does not assign it a value.
var answer;

// this creates a variable named answer and assigns it a value.
var answer = "answer";

// if you want to change the value of your variable, remove the identifier (var/let/const) and then set it equal to a value.
answer = "answer";

// you can also use this variable in a method. for example, this will pop up an alert in your web page
// whatever your variable is set to (the value after the equals sign) will appear wherever you place your variable.