line break in typescript

To create a line break within a string in TypeScript (or JavaScript), you can use the escape sequence "\n". This will create a new line wherever it appears within the string.

index.ts
let myString: string = "This is the first line.\nThis is the second line.";
console.log(myString);
99 chars
3 lines

Alternatively, you can use template literals, which allow for multi-line strings without the need for escape sequences. To use template literals, surround the string with backticks (`) and use ${} to insert variables or expressions within the string.

index.ts
let firstLine: string = "This is the first line.";
let secondLine: string = "This is the second line.";
let myString: string = `${firstLine}
${secondLine}`;
console.log(myString);
180 chars
6 lines

Both of these methods will output:

index.ts
This is the first line.
This is the second line.
49 chars
3 lines

gistlibby LogSnag