Gilles Ruppert

a.k.a. elduderino78

Splitting a Long JavaScript String Across Multiple Lines

Whilst reading a comment by Mike DeBoer on Ajaxian¬†about delimiting JavaScript strings with backslashes, I thought it’s worth mentioning it here as well, if only to remind myself.

String concatenation always creates a new string.

Thus, writing:

1
2
3
var str = "This is a long" +
  " string. That's why we want to " +
  " split it over several lines";

is slower & more memory intensive than writing:

1
2
3
var str = "This is a long\
 string. That's why we want to\
 split it over several lines";

Just be careful you don’t have trailing spaces as that will mess it up.