In JavaScript, you can create a new line in a string using the newline character \n
. When you include \n
in a string, it signifies a line break.
For example, we write:
var multiLineString = "This is line 1.\nThis is line 2.";
console.log(multiLineString);
This will output:
This is line 1.
This is line 2.
You can use the newline character \n
within strings in various contexts, such as in console.log()
outputs, in HTML elements (for example, setting the innerText
or innerHTML
properties), or when constructing strings dynamically.