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

Tiffany Sosa
Tiffany Sosa
2,599 Points

Different code syntax still works. Why?

I wrote my code differently and it worked. Is there a reason to do it his way vs the way I did it (ie. faster, efficient, etc)?

Here's my code:

var adjective = prompt("Give an adjective");
var verb = prompt("Give a verb");
var noun = prompt("Give a noun");
alert("You said " + adjective +"/ " + noun +"/ "+ verb);
document.write("There once was a " + adjective + " programmer who wanted to use " + verb + " javascript to " + noun + " save the world.");

2 Answers

Hi Tiffany,

I'd say it's a little down to personal preference but ideally you'd want to make the code easy to understand for any other developers working on your project. Dave's example is simply demonstrating building up the string using concatenation to test it and display it.

You could potentially have a large amount of variables and so writing them all in a line might require side scrolling. Breaking it down as Dave has could be considered clearer on this occasion. This is just an example of why it might be used.

Hope that helps :)

-Rich

No problem :)

-Rich