Create new line in side string in javascript

In JavaScript, you can create a new line inside a string using one of the following methods:

  1. Using the newline character (\n):

    const myString = "Hello\nWorld!";

    This will create a new line between "Hello" and "World".

  2. Using the escape sequence (\r\n):

    const myString = "Hello\r\nWorld!";

    This will also create a new line between "Hello" and "World", but this method is more commonly used in Windows environments.

  3. Using the HTML <br> tag:

    const myString = "Hello <br> World!";

    This will create a new line between "Hello" and "World", but this method is more suitable for HTML contexts.

  4. Using the template literal syntax (ES6+):

    const myString = `Hello
    World!`;

    This will create a new line between "Hello" and "World", and is a more modern and concise way to create multi-line strings.

  5. Using the replace() method:

    const myString = "Hello";
    myString = myString.replace(/\n/g, "<br>");

    This will replace all newline characters (\n) with <br> tags, creating a new line between "Hello" and the next string.

Note that the first three methods will create a new line in the string, but the string itself will not be split into multiple lines. If you want to split the string into multiple lines, you can use the split() method:

const myString = "Hello\nWorld!";
const lines = myString.split("\n");
console.log(lines); // Output: ["Hello", "World!"]