views:

3728

answers:

5

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

Something like....
1. alert ( "Please Select file
2. \ to delete" ); <-- ?

Thanks

+19  A: 

In your example, you can break the string into two pieces:

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

Or, when it's a string, as in your case, you can use a backslash as @Gumbo suggested:

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

When working with other code (not in quotes), line breaks are ignored, and perfectly acceptable. For example:

if(SuperLongConditionWhyIsThisSoLong
  && SuperLongConditionOnAnotherLine
  && SuperLongConditionOnThirdLineSheesh)
{
    // launch_missiles();
}
Michael Haren
Can you break up an if statement ?
Tommy
Yes: I updated the answer with an example.
Michael Haren
thanks. (Launch Missiles!)
Tommy
But beware of the automatic semicolon insertion mechanism: Try to have return on one row and a "string" on the next one at the end of the function and you get undefined as a result.
some
+5  A: 

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.

Gumbo
+1  A: 

You can just use

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

That should work

ferrari fan
+1  A: 

Break up the string into two pieces

alert ("Please Select file" +
       "to delete");
Jason Punyon
A: 

Put \n to get one long string in multiple lines inside the alert.

Pritesh