Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

Everything you need to know about template strings

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

Why does template strings exist?

Simply to add multiple strings into a single string. For example, If I ask you to add the string a and b. How would you do this?

const a = 'My name is Anjan Shomodder'
const b = 'I am a software engineer'

You might do this:

const combinedString = a + b
console.log(combinedString)

This will work. But what if you want to add number to it. How would you write I am 20 years old.

const age = 20
const c = 'I am'
const d = 'years old.'

You might do this:

const combinedString = a + age + d
console.log(combinedString)

This will work. But it is not that readable. What if you can write all the substring, javascript expressions like variables, functions in single string? That would be great. It would be readable and easy to do.

That's why we have Es6 template strings or template literals whatever you say.

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.

The way you write is instead of normal quotaion mark you use backticks. Alt Text

Inside backticks you normal put javascript string.

const a = `My name is Anjan Shomodder`

If you want to use variables then you do this.

const a = 'My name is Anjan Shomodder.'
const b = 'I am a software engineer.'
const c = 'I am'
const d = 'years old.'
const age = 20
const combinedString = `${a}${b}${c}${age}${d} I always write good code. ha ha ha`

So instead of writing the variables directly. You add a dollar sign$ and then you wrap them by curly brackets.

output: My name is Anjan Shomodder. I am a software engineer.

You can put whatever expressions you like.

  • add two number
const a = 21
const b = 10
const resultString = `${a} and ${b} adds up to ${a + b}`
  • Ternary operator: Single line if statement.
console.log(`My name is ${3>2 : "Anjan" : "Mark" }`)
output: My name is Anjan

And you get the point.

You can also write multiline strings.

console.log(`string text line 1
string text line 2`)

Another big use cases you will see if you have experience with graphql. Graphql is a data query and manipulation language for back end development.

  • Example:
import {gql} from "apollo-server-express"
const schema = gql`
type Query {
findUser(id: String!): User!
}
type User {
name: String!
}
`

You can also see them on styled-components codes. Even though they are not really template literals. They are tagged template literal.

const Button = styled.button`
background: transparent;
border-radius: 3px;
border: 2px solid palevioletred;
color: palevioletred;
margin: 0 1em;
padding: 0.25em 1em;
`

And that's all you need to know about template strings.

Shameless Plug

Want to create your own blog? Well, I am creating a video series where you will learn about how to create a JAMstack blog with Nextjs and Chakra-UI.

Lessons

Demo

You can demo the website from here

Features

  • Static Blog pages will make the website load faster.
  • Blogs will have code blocks with syntax highlighting and many embed components like youtube videos, GitHub gist, Tweets, and so many other things.
  • Autocomplete search feature for the blog posts.
  • Real-time view counter and so on.

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.

By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full-stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project. I am available on Upwork

Contacts

Blogs you might want to read:

Videos might you might want to watch:

Previous PostEverything you need to know about Javascript Destructuring