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 trialMatthew Cahn
6,301 PointsI 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
19,914 PointsOmitting 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
3,058 PointsKirby Abogaa
3,058 Pointsjust 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!
Santosh Kumar
2,232 PointsSantosh Kumar
2,232 PointsOnce 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?