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?

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

You might do this:

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

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

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

You might do this:

1const combinedString = a + age + d
2console.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.

Inside backticks you normal put javascript string.

1const a = `My name is Anjan Shomodder`

If you want to use variables then you do this.

1const a = 'My name is Anjan Shomodder.'
2const b = 'I am a software engineer.'
3const c = 'I am'
4const d = 'years old.'
5const age = 20
6
7const 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.

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

You can put whatever expressions you like.

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

And you get the point.

You can also write multiline strings.

1console.log(`string text line 1
2 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:
1import {gql} from "apollo-server-express"
2
3const schema = gql`
4 type Query {
5 findUser(id: String!): User!
6 }
7
8 type User {
9 name: String!
10 }
11`

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

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

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

Shameless Plug

I have made an Xbox landing page clone with React and Styled components. I hope you will enjoy it. Please consider like this video and subscribe to my channel.

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

Contacts

Blogs you might want to read:

Videos might you might want to watch:

Previous PostEverything you need to know about Javascript Destructuring