HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TypeScript / TypeScript Template Strings

TypeScript Template Strings

A template string is a regular JavaScript string which is enclosed with back-tick characters (`) seeded with placeholders denoted with ${ }. At runtime, your code replaces the placeholders with real values.

Template String Example

Let’s see a very basic example of template string. In this example, at runtime, the code replaces placeholders with the actual values of variables. It’s called variable substitution.

const name: String = 'Lokesh';
const age: number = 35;

let message: string = `My name is ${ name } and my age is ${ age }`;

console.log(message);		//My name is Lokesh and my age is 35

Template strings can be multi-line

Not like with regular strings where you use plus (+) character to concatenate multi-line strings, template strings do not need plus character.

const name: String = 'Lokesh';
const age: number = 35;

let message: string = `My name is 
						${ name } 
						and
						my age is 
						${ age }`;

//Output

My name is 
Lokesh 
and
my age is 
35

Please notice that multi-line template strings output the string which is formatted with new line characters.

Drop me your questions in comments section.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

TypeScript Tutorial

  • TypeScript – Introduction
  • TypeScript – Types
  • TypeScript – Union Types
  • TypeScript – String Literal Types
  • TypeScript – var, let and const
  • TypeScript – Template Strings
  • TypeScript – Arithmetic Operators
  • TypeScript – Logical Operators
  • TypeScript – Comparison Operators
  • TypeScript – for Loop
  • TypeScript – Spread Operator
  • TypeScript – Arrays
  • TypeScript – Enums
  • TypeScript – Map
  • TypeScript – Set
  • TypeScript – Functions
  • TypeScript – Function Overloading
  • TypeScript – Transpiler
  • TypeScript – Truthy and falsy
  • TypeScript – == vs ===
  • TypeScript – undefined vs null
  • TypeScript – Variable Hoisting
  • TypeScript – tsconfig.json

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)