Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

TypeScript - multiline string

0 contributions
9 points

In this article, we're going to have a look at how to create multiline string in TypeScript. There are few ways how to do it:

  • template literals - introduced in ECMAScript 2015 (ES6).
  • string sums,
  • backslash at end of line - this approach is not part of standard.

1. Template literals (template strings) example

This approach was introduced i ES6 that is supported by TypeScript.

Main disadvantage of this approach are white space prefixes for each line if we want to format code clearly.

let text = `Line 1;
Line 2;
Line 3;
Line 4;
Line 5;`;



Line 1;
Line 2;
Line 3;
Line 4;
Line 5;

2. String sums example

This aproach solves problem of white character prefixes for formatted source code but introduces some complications about taking cate of " and + characters.

let text = 'Line 1;' +
    'Line 2;' +
    'Line 3;' +
    'Line 4;' +
    'Line 5;';



Line 1;Line 2;Line 3;Line 4;Line 5;

3. Backslash at endline example

This approach is verry similar to template literals and introduces problem with taking care of \ character at end of lines.

Note: this feature is not part of ECMAScript standard, so when source code is transpiled we should be sure it is transpiled to safe code too.

let text = 'Line 1;\
Line 2;\
Line 3;\
Line 4;\
Line 5;';



Line 1;Line 2;Line 3;Line 4;Line 5;

4. Array.join method example

This approach is more like cheat to achive multi line string effect.

let array: Array<string> = [





  1. Template literals - MDN
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more