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

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