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 Foundations Strings Concatenation

Nikki Turrisi
Nikki Turrisi
15,095 Points

I don't understand the point of concatenating the multi-line? Isn't it a waste of code and characters?

Is it because you can organize the actual code better within js? It seems redundant to me.

3 Answers

Concatenation of multiple lines is very handy if you have strings that stretch far beyond your screen size going into a single variable, and you don't want to have to horizontally scroll for 3 minutes to see what you wrote because I certainly do not like horizontally scrolling to see anything! :D

An example:

// Bad Practice
var errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';

// Good Practice
var errorMessage = 'This is a super long error that was thrown because ' +
  'of Batman. When you stop to think about how Batman had anything to do ' +
  'with this, you would get nowhere fast.';

Absolutely, Mathjis!

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

You should feel free to format the code as you see fit. I can see why you might want to code multiline if you had something like this

var language = " javascript";
var mood = " it's great";

"I love" 

+" coding in" 
+ language
+ "because "
+ mood

But if you were joining together strings that were much larger, it might be easier to read if you put them one one line. :-)

Totally agree!

Kieran Kane
Kieran Kane
10,973 Points

My understanding is that it doesn't have any benefit in the example given, but it shows you how to use it for examples like Marcus and Jonathon provided.