[javascript] How do I break a string across more than one line of code in JavaScript?

Is there a character in JavaScript to break up a line of code so that it is read as continuous despite being on a new line?

Something like....

1. alert ( "Please Select file   
2. \ to delete" );

This question is related to javascript line-breaks

The answer is


ECMAScript 6 introduced template strings:

Template strings are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

For example:

alert(`Please Select file   
to delete`);

will alert:

Please Select file   
to delete

No need of any manual break in code. Just add \n where you want to break.

alert ("Please Select file \n to delete");

This will show the alert like

Please select file 
to delete.

ECMAScript 6 introduced template strings:

Template strings are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

For example:

alert(`Please Select file   
to delete`);

will alert:

Please Select file   
to delete

Break up the string into two pieces 

alert ("Please select file " +
       "to delete");

You can just use

1:  alert("Please select file" +
2:        " to delete");

That should work


I tried a number of the above suggestions but got an ILLEGAL character warning in Chrome code inspector. The following worked for me (only tested in Chrome though!)

alert('stuff on line 1\\nstuff on line 2);

comes out like...

stuff on line 1
stuff on line 2

NOTE the double backslash!!...this seems to be important!


Put the backslash at the end of the line:

alert("Please Select file\
 to delete");

Edit    I have to note that this is not part of ECMAScript strings as line terminating characters are not allowed at all:

A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash \. The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.

So using string concatenation is the better choice.


Update 2015-01-05    String literals in ECMAScript5 allow the mentioned syntax:

A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.


The backslash operator is not reliable. Try pasting this function in your browser console:

function printString (){
  const s = "someLongLineOfText\
  ThatShouldNotBeBroken";
  console.log(s);
}

and then run it. Because of the conventional (and correct) indentation within the function, two extra spaces will be included, resulting in someLongLineOfText ThatShouldNotBeBroken.

Even using backticks will not help in this case. Always use the concatenation "+" operator to prevent this type of issue.


You can just use

1:  alert("Please select file" +
2:        " to delete");

That should work


I tried a number of the above suggestions but got an ILLEGAL character warning in Chrome code inspector. The following worked for me (only tested in Chrome though!)

alert('stuff on line 1\\nstuff on line 2);

comes out like...

stuff on line 1
stuff on line 2

NOTE the double backslash!!...this seems to be important!


Break up the string into two pieces 

alert ("Please select file " +
       "to delete");

Put the backslash at the end of the line:

alert("Please Select file\
 to delete");

Edit    I have to note that this is not part of ECMAScript strings as line terminating characters are not allowed at all:

A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash \. The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.

So using string concatenation is the better choice.


Update 2015-01-05    String literals in ECMAScript5 allow the mentioned syntax:

A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.


You can break a long string constant into logical chunks and assign them into an array. Then do a join with an empty string as a delimiter.

var stringArray = [
  '1. This is first part....',
  '2. This is second part.....',
  '3. Finishing here.'
];

var bigLongString = stringArray.join('');
console.log(bigLongString);

Output will be:

  1. This is first part....2. This is second part.....3. Finishing here.

There's a slight performance hit this way but you gain in code readability and maintainability.


No need of any manual break in code. Just add \n where you want to break.

alert ("Please Select file \n to delete");

This will show the alert like

Please select file 
to delete.

Break up the string into two pieces 

alert ("Please select file " +
       "to delete");

Interesting to note. Tried:

alert("Some \
    string \
    wrapped \
    across \
    mutliples lines.")

And this worked. However, on accident!, there was a space character following the final backslash (all other backslashes were at the end of the line). And this caused an error in the javascript! Removing this space fixed the error, though.

This is in ADT for Android using Cordova.


A good solution here for VSCode users, if a string breaking down into multiple lines causes the problem (I faced this when I had to test a long JWT token, and somehow using template literals didn't do the trick.)


Put the backslash at the end of the line:

alert("Please Select file\
 to delete");

Edit    I have to note that this is not part of ECMAScript strings as line terminating characters are not allowed at all:

A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash \. The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.

So using string concatenation is the better choice.


Update 2015-01-05    String literals in ECMAScript5 allow the mentioned syntax:

A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.


You can break a long string constant into logical chunks and assign them into an array. Then do a join with an empty string as a delimiter.

var stringArray = [
  '1. This is first part....',
  '2. This is second part.....',
  '3. Finishing here.'
];

var bigLongString = stringArray.join('');
console.log(bigLongString);

Output will be:

  1. This is first part....2. This is second part.....3. Finishing here.

There's a slight performance hit this way but you gain in code readability and maintainability.


You can just use

1:  alert("Please select file" +
2:        " to delete");

That should work


Interesting to note. Tried:

alert("Some \
    string \
    wrapped \
    across \
    mutliples lines.")

And this worked. However, on accident!, there was a space character following the final backslash (all other backslashes were at the end of the line). And this caused an error in the javascript! Removing this space fixed the error, though.

This is in ADT for Android using Cordova.


Break up the string into two pieces 

alert ("Please select file " +
       "to delete");

A good solution here for VSCode users, if a string breaking down into multiple lines causes the problem (I faced this when I had to test a long JWT token, and somehow using template literals didn't do the trick.)