36

This code

alert("Hello again! This is how we" + "\n" + "add line breaks to an alert box!");

doesn't work. Firefox JavaScript console names error as "unterminated string literal" and points on " symbol before \n. I want to fire an alert with multi-line text. No jQuery please.

j08691
  • 204,283
  • 31
  • 260
  • 272
Pave
  • 2,347
  • 4
  • 21
  • 23
  • 1
    The offending code is not that piece of code. Note that the syntaxerror messages are almost always misleading – Esailija Feb 18 '12 at 10:04
  • when i add // before this line everything works fine – Pave Feb 18 '12 at 10:06
  • 7
    Nevertheless, if you run that code in isolation, it will work normally, I just tested it. Please provide more code. – Esailija Feb 18 '12 at 10:08
  • This alert fires in function function FCKeditor_OnComplete( editorInstance ) { alert("Hello again! This is how we"+String.fromCharCode(10)+"add line breaks to an alert box!"); – Pave Feb 18 '12 at 10:16
  • That function is missing `}` but works fine after adding it – Esailija Feb 18 '12 at 10:23
  • It's not the problem, but `"\n"` doesn't need to be separate, just say `"...how we\nadd line..."`. – nnnnnn Feb 18 '12 at 11:02

12 Answers12

63

Haven't tested but does this work?

alert("Hello again! This is how we \n add line breaks to an alert box!");
okyanet
  • 3,106
  • 1
  • 22
  • 16
25

This just happened to me... exactly. I needed to change it to \\n instead of \n.

alert("Hello again! This is how we"+"\\n"+"add line breaks to an alert box!");
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
  • Thanks, this is correct when all the javascript code comes from server side as a string, example: "alert('Hello again! This is how we'+'\\n'+'add line breaks to an alert box!');"; --> with \\n line break works, otherwise fails. – FabianSilva Jul 03 '14 at 16:31
  • 1
    Nope, \n alone is correct. If you put it into another string (like when you make an echo "alert('x\ny');";), the result would be an alert('x[linebreak]y');, which is incorrect. You have to double escape it with \\n, yes, but this has nothing to do with JavaScript. – StanE Oct 19 '15 at 11:28
7

Question is old but I want to give a clear answer and explain why this happens for others who comes here:

First of all, there is nothing wrong with the JavaScript code line in the question. It is absolutely valid and produces no parsing error. The reason for the problem in Vitalmax' case was most likely because he/she didn't posted additional code, which surrounded that line.

Here is an example in PHP, which shows why the JS parser complains about the syntax:

<?php
  echo "alert('Hello again! This is how we\nadd line breaks to an alert box!');";
?>

The parsed server-side output then is (this is what the browser gets):

alert("Hello again! This is how we
add line breaks to an alert box!");

In JavaScript, strings must not have real line breaks.* Instead, they must always be escaped (like: \n), so the browser complains about an "unterminated string literal" at the real line break. There are some exceptions to this rule, like for horizontal tabulators (\t). So (in this case) you have to escape the line breaks twice with \\n. So when PHP parses and converts it from \\n to \n, JavaScript can convert it from \n to [real line break].

Correct PHP example would be:

<?php
  echo "alert('Hello again! This is how we\\nadd line breaks to an alert box!');";
?>

Or:

<?php
  echo 'alert("Hello again! This is how we\nadd line breaks to an alert box!");';
?>

In the second case you don't have to double escape it because escaped characters within PHP strings with single quotes are not decoded (\n stays \n).

*Note: It is possible to use the special quote sign ` which allows real line breaks in JS. Example:

alert(`Hello again! This is how we
add line breaks to an alert box!`);
StanE
  • 2,704
  • 29
  • 38
6

Adding changing \n to \\n should work if you are rendering the alert serverside (ie: On a website), but if you are running client side (ie: testing etc.) just \\n will not work work. For example, try running the following lines on this website.

alert("Hello again! This is how we"+"\n"+"add line breaks to an alert box!");

or:

 alert("Hello again! This is how we" +"\\n" +"add line breaks to an alert box!");

In jsfiddle, the first one works, because it runs it client side, but if you are running it serverside, it requires the double slash (\\n) I'm not sure why this is, but I have tested it multiple times.

Hope this helps!

Travis
  • 1,274
  • 1
  • 16
  • 33
1
<script>
alert("Hello. \n How are you?")
</script>
Travis
  • 11
  • 1
  • 1
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 09 '16 at 23:54
1

This script works for me.

<script>
  alert("Hello. \r\n How are you?");
</script>
Yannis
  • 1,682
  • 7
  • 27
  • 45
1

error "missing ; before statement" That tells me the semicolon is missing from above this line of code. Usual syntax gotchya are if statements that don't close parentheses properly. Or curly braces.

Krista K
  • 21,503
  • 3
  • 31
  • 43
0

Use single quotes.

alert("Hello again! This is how we"+'\n'+"add line breaks to an alert box!");
David Starkey
  • 1,840
  • 3
  • 32
  • 48
0

A more functional way i would suggest is to define your own print() function and forget about putting \\n for each line manually

//define
var print = new Function("var lines=arguments[0]+'\\n';; for(var i=1;i<arguments.length;i++){lines+=arguments[i]+'\\n';} alert(lines);");
    
//call
print("say","hello","to","javascript","this","is ","awesome");

Now you may call the print() with multiple arguments and each argument will be available in a separate line.

Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
0

Another way is using String.fromCharCode():

alert("This unit has been removed from stock" + String.fromCharCode(13) + " Do you wish to RETURN this unit to stock?");
Jonathan Brizio
  • 1,087
  • 1
  • 14
  • 28
0

alert("Hello again! This is how we" + "\n" + "add line breaks to an alert box!");

\n --> this is working working with \n

\n --> does not work, it just treat it as continuous string of alert msges does not working with \n

Lalit Dubey
  • 51
  • 1
  • 9
  • It is no quite clear with your formatting what you mean, you could wrap your code inside a JavaScript snippet as examples (as the author have done in the question) to make it easier for people to understand what the differences are. – Johan Sep 25 '18 at 16:08
  • thanks @Johan, actually i am totally new to stack overflow and i was trying to insert output images to make difference between the two things which i tried to explain but fail to do so. but now am aware about how to use snippet. thanks a lot – Lalit Dubey Sep 26 '18 at 09:13
  • No problem, it's a great initiative to share your solutions! – Johan Sep 26 '18 at 09:15
0

I had issues where \n and \n didn't work, then I tried using html break tag i.e.
since that panel was written in html and it worked.

example -

some text here <br> some more text

output:

some text here
some more text
Gabcvit
  • 1,468
  • 1
  • 15
  • 32