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 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");
verb = prompt("Enter a verb");
adjective = prompt("Enter an adjective");
noun2 = prompt("Enter a noun");

//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.

//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?