-4

To add a new line into a JavaScript string can I use the following code?

Is it according to programming standards? Is there another way to do this?

For example,

var string = "check";
alert(string);
var newstring = string + "\n\n";
alert(newstring);
rishat
  • 8,206
  • 4
  • 44
  • 69
jiju
  • 11
  • 2
  • 8

3 Answers3

0
\n 

is the right way to handle new lines in alert boxes.

NeuTronas
  • 263
  • 3
  • 11
0

Simply add a \n everytime you want a new line in your string :

var str = 'Test\nTest2\nTest3';
alert(str);
Zenoo
  • 12,670
  • 4
  • 45
  • 69
0

I think it is fine. If you are using alert for the debugging purposes, Please use console.log("string + "\n"); instead.

MS48
  • 100
  • 1
  • 10