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

Matthew Cahn
Matthew Cahn
6,301 Points

I didn't use the var identifier but it still works. Is there something wrong with programming like this?

Here's my code:

//Create a story-telling program:

//1. Use the prompt() command several times to collect different types of words -- nouns, verbs, adjectives.
//2. Store the result of each prompt() command in a different variable.
noun1 = prompt("Enter a noun");
console.log(noun1);
verb = prompt("Enter a verb");
console.log(verb);
adjective = prompt("Enter an adjective");
console.log(adjective);
noun2 = prompt("Enter a noun");
console.log(noun2);

//3. Combine the variables with other strings to create one or more non-sensical statements.
var statement = noun1 + ' ' + verb + " the " + noun2;
//4. Print the resulting story to the browser using the document.write() command.
document.write(statement);

//Story: The noun1 verb the adjective noun2

1 Answer

David Trejo
David Trejo
19,914 Points

Omitting the var keyword doesn't cause much of a problem in small programs. However, when you deal with larger scale programs it is best practice to use the "var" keyword to declare variable because omitting the "var" keyword in a variable causes the variable to become a global. Once a variable becomes global, its kind of available to all and modifiable from any source. I hope this helps -David

Kirby Abogaa
Kirby Abogaa
3,058 Points

just to add...not all browsers interpret a "variable" as it is without the identifier which could cause unexpected bugs especially when your nesting functions...though some of this are not discussed here yet, it is best to practice using the identifier now

and uhm, not sure, but...

i think a variable only becomes global if it is 'outside' of a function(s) relative to it...

Let me know if im wrong...would appreciate it!

Once a variable becomes global, its kind of available to all and modifiable from any source.

Can you elaborate? Available to all? Modifiable from any source? What does these all mean?