321

C# uses string interpolation

int value = 100;
Console.WriteLine($"The size is {value}.");

Output:

The size is 100.

How to do the same thing in TypeScript?

zshanabek
  • 4,270
  • 3
  • 19
  • 27

3 Answers3

507

In JavaScript you can use template literals:

Template literals are literals delimited with backticks (`)

let value = 100;
console.log(`The size is ${ value }`);
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
111

Just use special `

var lyrics = 'Never gonna give you up';
var html = `<div>${lyrics}</div>`;
miken32
  • 42,008
  • 16
  • 111
  • 154
Ygalbel
  • 5,214
  • 1
  • 24
  • 32
4

It can be done with the backtick character: `

Pay attention this is NOT an apostrophe, nor a quote.

Where to find it on keyboard?

Most probably you're on a QWERTY keyboard, so you'll find it JUST below the Esc key in the top left corner:

enter image description here

In case you're using a special keyboard, you can check this exhaustive list to find the backtick.

Finally this is the code:

const value = 100;
console.log(`The size is ${value}.`);

(Valid in both JavaScript and TypeScript)

underflow
  • 1,545
  • 1
  • 5
  • 19