372

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?

0

3 Answers 3

576

In JavaScript you can use template literals:

Template literals are literals delimited with backticks (`)

let value = 100;
console.log(`The size is ${ value }`);
Sign up to request clarification or add additional context in comments.

2 Comments

any polyfill for typescript like it's available for babel?
@lawphotog polyfill for what? this is supported in typescript.
137

Just use special `

var lyrics = 'Never gonna give you up';
var html = `<div>${lyrics}</div>`;

7 Comments

yes, notice it's the special ``, not a '.
Gives syntax error on IE
I can't believe I got rickrolled here.
@Ygalbel:can you tell me how to iterate array with ${arrayData}?
i wonder who in the world came with this fancy idea!
|
17

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)

1 Comment

As somone who is very visual thanks for this i was wondering what a did wrong was the back tick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.