Using Template Strings

Modified on Wed, 7 Aug at 3:07 PM

Strings support using embedded expressions (variables such as objects, arrays, numbers and more) within them. 



Using + Operator

Using Template Strings

Example

let name = "Alice";

let greeting = "Hello, " + name + "!";

greeting;//"Hello Alice!"


let name = "Alice";

let message = `Hello, ${name}!`;

message;//"Hello, Alice!"



1. The + operator 

let firstName = "John";
let age = 30;
let message = "Hello, " + firstName + ". You are " + age + " years old.";

message;//Hello, John. You are 30 years old.

2. Template Strings


To convert a regular string to a template string, you need to use backticks (`) around the string instead of double quotation marks (“”) and add any expression with the ${}` within a template string.


let firstName = "John";
let age = 30;
let message = `Hello, ${firstName.toUpperCase()}. You are ${age} years old.`;

message;//Hello, JOHN. You are 30 years old.


Here are few problems where these concepts are used:



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article