DEV Community

Cover image for JavaScript template literals
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

JavaScript template literals

Something cool that was introduced in JavaScript ES6 is Template Literals.

You might wonder, what the hell are Template Literals Chris, and more importantly, why do I even need them?

Template Literals are a very quick and widely adopted new way of defining strings.

Besides that, they come with some extra new and handy methods. I'm going to be showcasing some examples of the changes between the old method and the new one.

Difference between strings and template literals

If we want to define a string in JavaScript, we can do that using quotes, either single (') or double (") quotes.

const myString = 'Cool I can be anything';
const otherString = "Me too!";
Enter fullscreen mode Exit fullscreen mode

With template literals we can do the same, but using a backtick.

` (backtick)
Enter fullscreen mode Exit fullscreen mode
const templateLiteral = `I can also be a string`; 
Enter fullscreen mode Exit fullscreen mode

Ok, but that almost looks the same so what makes them so great?

Template literals can be multiline

Creating a multiline string was always an issue when it comes to JavaScript, we had to include a \n or just use multiple strings.

const multi = 'Hello\
Also hello'
console.log(multi); 
// 'HelloAlso hello'

const multi = 'Hello \n Also hello';
console.log(multi); 
// 'Hello 
// Also hello'
Enter fullscreen mode Exit fullscreen mode

As you can see this can be super annoying to include these \n escapes everywhere.

With template literals this is way easier, we can just type multiple lines.

const multi = `Hi

Yes I'm on multiple lines
How cool`;
console.log(multi);
//"Hi
//
//Yes I'm on multiple lines
//How cool"
Enter fullscreen mode Exit fullscreen mode

Ok that's a big pro already!

It can handle variables

Another thing that will annoy you as a developer is that you have to escape the string to use a variable.

console.log("What is 2 + 3: " + ( 2 + 3 ) + "!");
//'What is 2 + 3: 5!'
Enter fullscreen mode Exit fullscreen mode

Jeez, such a hassle to include a simple variable or function.

With template literals, we can use variables using this syntax ${variable}.

console.log(`What is 2 + 3: ${2+3}!`);
//'What is 2 + 3: 5!'
Enter fullscreen mode Exit fullscreen mode

Oh, wow way shorted and easier on the eyes already!

Another great example and everyday use-case would be expressions.

Let's say we have a dynamic class we need to add.

With normal string we have to either escape or break this in two separate things.

let bodyClass = 'body ' + (isLargeScreen() ? 'large' : 'small' );
console.log(bodyClass);
// 'body large'
Enter fullscreen mode Exit fullscreen mode

With template literals this can be made easier.

let bodyClass = `body ${isLargeScreen() ? 'large' : 'small'}`;
console.log(bodyClass);
// 'body large'
Enter fullscreen mode Exit fullscreen mode

So with this being said, create a habit of using template literals, it will make your life so much easier.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (2)

Collapse
 
fericoder profile image
Fariborz

Thank you for the excellent article...❤❤

Collapse
 
dailydevtips1 profile image
Chris Bongers

You are more than welcome ✨