Print new line in javascript
In JavaScript, you can print a new line using the following methods:
-
Using the
\n
character:console.log("Hello"); console.log("\nWorld");
This will print "Hello" followed by a new line, and then "World".
-
Using the
document.write()
method:document.write("Hello"); document.write("<br>"); document.write("World");
This will print "Hello" followed by a
<br>
tag, which represents a new line, and then "World". -
Using the
innerHTML
property:var paragraph = document.createElement("p"); paragraph.innerHTML = "Hello"; document.body.appendChild(paragraph); paragraph.innerHTML += "<br>"; paragraph.innerHTML += "World";
This will create a new paragraph element, set its inner HTML to "Hello", append it to the document body, and then append a new line and "World" to the paragraph.
-
Using the
console.log()
method with a newline character:console.log("Hello"); console.log("World");
This will print "Hello" and "World" on separate lines in the console.
Note that the \n
character is not recognized by all browsers, so it's generally recommended to use the document.write()
or innerHTML
methods for printing new lines in a web page.