views:

385

answers:

8

It doesn't really have to add newlines, just something readable.

Anything better than this?

str = "line 1" +
      "line 2" +
      "line 3";
+6  A: 

You could do

str = "\
line 1\
line 2\
line 3";

As mentioned in the comments, javascript parsers handle this fine (it works in all major browsers), but is not officially part of ECMA Script syntax. As such it may or may not work with compressors, error checkers, and isn't guaranteed to work in browsers.

This may be more readable, but isn't the 'best' way to do it. Maybe ECMA script will support something like c#'s @"" someday.

Gordon Tucker
Wow. This really works. Thanks Gordon! I didn't know you could do this at all...
Robert Koritnik
readable but not correct for js error parsers!
Elzo Valugi
@Thomas: "In every example I've seen, you also put a \ after line3." Just bc the article you linked to had a typo does not mean it's right. read the rest of your article, he only did it once, in the first example.
geowa4
@Elzo Valugi "readable but not correct for js error parsers!" jslint doesn't validate it, but I use the error parser at http://www.javascriptlint.com, which does validate it.
Gordon Tucker
This notation is not part of the official ECMA standard, but every major JS engine has supported it for years. However, you may run into trouble with JS compressors, syntax highlighters, etc. etc.
Pumbaa80
+1  A: 

Yes! You can use the \ character to have JavaScript ignore end of line characters.

str = 'line 1 \
line 2 \
line 3';

However, as pointed out by Elzo Valugi, this will not validate using JSLint.

Thomas Owens
As far as I remember, this does not work in some browsers. Presumably some version(s) of IE.
Ionuț G. Stan
Ionut: Yes, you would need to test it in all browsers you are concerned with, and if it were to fail in a browser, I would suspect it would be IE. But I tested this in Firefox and it works there.
Thomas Owens
+4  A: 

FYI. The way you suggest it is the correct way and better than the other answers. JsLint only validates your version.

Elzo Valugi
+1 for jslint .
geowa4
Tip: keep those lines short. If they stretch off the screen, you don't see the + and it gets unreadable. Or put the + at the beginning of the lines like Ionut suggests.
Nathan Long
A: 

Consistently.

Whichever way you choose, do it exactly the same way throughout your application. If you're working on an application that already has code written, accept the convention they set and go with it.

Dean J
+3  A: 

I like this version (different than yours just in formatting of the code):

var str = "line 1"
        + "line 2"
        + "line 3";
Ionuț G. Stan
Yes - it's easier to understand at a glance.
Nathan Long
this doesnt validate as well. I had once a script with some html like this and I had to redo it.
Elzo Valugi
You had to escape the HTML, the way the string is constructed has nothing to do with validation.
Ionuț G. Stan
+3  A: 
var str = [
    "line 1",
    "line 2",
    "line 3"
].join("");
// str will contain "line1line2line3"

If you actually want newlines in the string, then replace .join("") with .join("\n")/

NickFitz
is this method still faster than the "str" + "str" concatenation alternative or does it not matter to today's browsers?
Ty W
join is faster if you have maaaaaaaaaany parts to concatenate, because "+" will be executed (n-1) times, creating temporary results in each step. For details see http://video.yahoo.com/watch/4141759/11157560 at 23:08
Pumbaa80
We had a script that build the entire page through "str" + "str" and it was pretty slow (about 30 sec page load). We changed to use an array based appending system like this and it dropped to less than one second. So, yes, it is faster :)
Gordon Tucker
A: 

This will only work in browsers with E4X support - I wish we could use it in IE

var str = <><![CDATA[

   Look, a multi-line
   string! < " // ' ? &

]]></>.toString();
Pumbaa80
You don't need the XMLList literal (`<>..</>`). You can just do `<![CDATA[..text..]]>.toString()`
Eli Grey
Have you tested? For some reason it doesn't work in Firefox. According to the standard, an XMLCDATA section is XMLMarkup, thus is an XMLInitialiser, which should be recognized by the engine as a PrimaryExpression (?)
Pumbaa80
A: 

Almost identical to NickFitz's answer:

var str = [""
    ,"line 1"
    ,"line 2"
    ,"line 3"
].join("");
// str will contain "line1line2line3"

The difference, the code is slightly more maintainable because the lines can be re-ordered without regard to where the commas are. No syntax errors.

dreftymac