Java String new line

In JavaScript, you can use the following special characters to add a new line in a string:

  1. \n: This character is called a "line feed" and is used to move to the next line.
const str = "Hello, World!\nHow are you today?";
console.log(str);

// Output:
// Hello, World!
// How are you today?
  1. \r: This character is called a "carriage return" and is used to move the cursor to the beginning of the current line.
const str = "Hello, World!\rHow are you today?";
console.log(str);

// Output:
// How are you today?
  1. \r\n: This combination of characters is used to move to the next line and move the cursor to the beginning of the new line. This is the most commonly used new line character in Windows.
const str = "Hello, World!\r\nHow are you today?";
console.log(str);

// Output:
// Hello, World!
// How are you today?

Note: In HTML, you can use the <br> element to add a new line.

const str = "Hello, World!<br>How are you today?";
document.body.innerHTML = str;