1

Well i have a long string in double quotes

var Variable = "Really Long ...... String"

I would want to do this

var Variable = "Really
                Long
                ...... 
                String"

Currently I do this

var Variable = "Really\n\
                Long\n\
                ......\n\
                String"

It works correctly across all browsers. But is it the correct way to do it?

5
  • Atleast JSlint doesn't like it - Bad escapement. Commented Nov 13, 2010 at 21:12
  • Are those \n required? The 1st and 3rd are not the same string. Commented Nov 13, 2010 at 21:13
  • @KennyTM, I didn't understand you. The "......." is just to denote a long string Commented Nov 13, 2010 at 21:17
  • I think what Kenny means that there is a differences between a string with \n and without them. Commented Nov 13, 2010 at 21:22
  • @KennyTM, thanks, as you say \n turns out to be unnecessary Commented Nov 13, 2010 at 21:30

5 Answers 5

4

I think using the plus sign might be more effective.

var Variable = "Really " +
                "Long " +
                "...... " +
                "String";

In your example above, how do you know how many spaces are before the word "Long", for example? I would not count on that being consistant across browsers.

My understanding is that using the plus sign to concatentate strings is just as efficient...javascript parsers are smart enough to handle that the same way as your example.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd recommend just joining it.

var str = [
   'This is a very long piece ',
   'of string that I\'m going to join together ',
   'right about now.'
].join('')

1 Comment

If you're joining a lot of strings, this is the best way.
0

You can use an array and join the values. But I am not sure about performance. If you use string concatenation, it can (and most likely will) be optimized by the engine (meaning it generates one string on parsing the code), but whether engines are so smart to discover a simple join with an array.... I don't think so.

var Variable = ["Really ",
                "Long ",
                "...... ",
                "String"].join('');

Comments

0

JavaScript does not support "here documents" as PHP does, so you can't do much better. You will end up with many spaces at the beginning of lines though (if you indent the code the way you present it here). To avoid that, use string concatenation:

var Variable = "Really\n" +
"Long\n" +
"String";

Or array joining (as suggested by meder):

var Variable = [
    "Really",
    "Long",
    "String"
].join('\n');

Comments

0

The LineContinuation is a new feature in ECMA-262 5th edition (§7.8.4). If you absolutely must comply to ECMAScript 3, avoid it and use + or .join('') or whatever. But as you've mentioned, this particular feature is supported among major browsers (even IE 6)[1], I see no strong reason to avoid this other than having a coding convention.

(Ref: [1] http://shwup.blogspot.com/2009/05/line-continuation-in-javascript.html)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.