Add new line with js
You can add a new line in JavaScript using the following methods:
Method 1: Using document.write()
document.write("<br>");
This method writes the HTML code <br>
to the document, which will create a new line.
Method 2: Using innerHTML
document.getElementById("myDiv").innerHTML += "<br>";
This method adds the HTML code <br>
to the innerHTML of an element with the id "myDiv", creating a new line.
Method 3: Using createElement()
and appendChild()
var br = document.createElement("br");
document.getElementById("myDiv").appendChild(br);
This method creates a new <br>
element and appends it to the element with the id "myDiv", creating a new line.
Method 4: Using console.log()
console.log("\n");
This method writes a new line to the console, but it won't affect the HTML document.
Method 5: Using a string concatenation
var text = "Hello\nWorld!";
This method concatenates a string with a newline character (\n
) to create a new line.
Note that the first three methods will add a new line to the HTML document, while the last two methods will add a new line to the console or a string.