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

Jim Andrews
Jim Andrews
5,967 Points

Used += instead of inline concatenation to make my "story" variable print the whole thing. Good or bad?

So instead of using the inline method of concatenation, I just kept updating my "story" variable, then printed that to the screen so my document.write() could be short and I could avoid this big long line of text to search through for potential errors. Thoughts? Right or wrong? Will this method cause the browser to work slower (I guess this is the real question)?

var noun = prompt("Give me a noun.");
var verb = prompt("Give me a verb.");
var adjective = prompt("Give me a adjective.");
var story = "";

story += "Once upon a time there was a great big ";
story += noun;
story += ". The ";
story += noun;
story += " would ";
story += verb;
story += " very ";
story += adjective; 
story += ". Thanks for reading!!!";

document.write(story);

1 Answer

Steven Parker
Steven Parker
230,274 Points

This method is essentially equivalent to what you started with, and as you point out can make the code easier to read and maintain. You might also consider a mix of the two, assembling a sentence or phrase at a time, but not so much to make the line overly long.

Good job, applying your new skills creatively! :+1: