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

Jason Clark
Jason Clark
2,499 Points

Adding to the sentence var...

In the example you created new lines to add to the var sentence. Why not just add the rest of the words in the sentence to the existing var sentence? Isn't it unnecessary to add new lines of code here?

3 Answers

Darren Joy
Darren Joy
19,573 Points

In the example linked he was building as he went and testing as well, so he was creating part of the sentence with one word input.

It would probably make sense to circle back and clean up the code so you are reusing as much of the variables as possible, minimizing lines that are not needed or duplicates etc..

As a side note though, in the Jquery courses they are often trying to make functions to do things and declaring variables for things, instead of trying to type it all into one line. This is to make it absolutely clear what each variable does, and what each function is needed for. While it seems like it could be done in one line, they chose to break it down so it was clear for the 'next' coder who might come along and have to update the code.

If I understand the question correctly, you are describing something akin to:

var sentence = "I am a ";
var word = "goat!";
var sentence = sentence + word;

They did that for illustrative purposes. In the real world, maybe one of those variables is passed by the calling function, or are user input, or any number of things... But generically, in that example, yes, the extra step is illustrative only, and generally unneeded.

Jason Clark
Jason Clark
2,499 Points

Thanks guys for the responses, helped to understand the reasoning.