Add new line from js
You can add a new line in JavaScript using the following methods:
- Using the
\n
character:console.log("Hello" + "\n" + "World");
This will output:
Hello World
- Using the
document.write()
method:document.write("Hello<br>"); document.write("World");
This will output:
Hello World
- Using the
innerHTML
property:var paragraph = document.createElement("p"); paragraph.innerHTML = "Hello<br>World"; document.body.appendChild(paragraph);
This will output:
Hello World
- Using the
createElement()
method and theappendChild()
method:var paragraph = document.createElement("p"); var textNode = document.createTextNode("Hello\nWorld"); paragraph.appendChild(textNode); document.body.appendChild(paragraph);
This will output:
Hello World
Note: The
\n
character is used to represent a new line in most programming languages, including JavaScript. However, in HTML, you need to use the<br>
tag to create a new line.