Javascript string new line
In JavaScript, there are a few ways to represent a new line in a string:
- Using the newline character (
\n
): This is the most common way to represent a new line in a string. You can use the\n
character to insert a new line in your string.const myString = "Hello\nWorld!";
This will render as:
Hello World!
- Using the carriage return character (
\r
): This character is used to move the cursor to the beginning of the current line. You can use it in combination with the newline character (\n
) to create a new line.const myString = "Hello\r\nWorld!";
This will render as:
Hello World!
- Using the Unicode escape sequence (
\u000A
): This is another way to represent a new line in a string. The\u000A
sequence represents the Unicode character for a newline.const myString = "Hello\u000AWorld!";
This will render as:
Hello World!
- Using the string concatenation method: You can also use string concatenation to create a new line in a string. For example:
const myString = "Hello" + "\n" + "World!";
This will render as:
Hello World!
Note that the \n
character is platform-independent, meaning it will work on both Windows and Unix-based systems. The \r
character, on the other hand, is specific to Windows and may not work on Unix-based systems.