Thursday, November 8, 2018

TypeScript - ES6 - Template Literals

In JavaScript, if we want to write multi-line statements - we must use backticks. In the following example:

let myFavoriteCity = "Visakhapatnam";
console.log(`Last year I have been to a beautiful city.
It had several beaches. One of them was Rushikonda beach.
The city name is ${myFavoriteCity}`);

Console Output:
Last year I have been to a beautiful city.
It had several beaches. One of them was Rushikonda beach.
The city name is Visakhapatnam

In this example, while typing multi-line string using backticks we wanted to print the myFavoriteCity variable. For this, we have used the template literal. A template literal is variable enclosed within curly braces which is preceded by dollar sign.


No comments:

Post a Comment