Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Template literals
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. React JS Tutorials
  3. Template literals

React JS Tutorial

Modern Javascript Core Concepts

  • Modern Javascript Core Concepts
  • Variables
  • Arrow functions
  • Rest and spread
  • Object and array destructuring
  • Template literals
  • Class
  • Callbacks
  • Promise
  • Async Or Await
  • ES Modules

React Js Basic Fundamentals

  • React JS Introduction
  • How to install React

Template literals

Template Literals are a new ES2015 / ES6 feature that allows you to work with strings in a novel way compared to ES5 and below.

The syntax at a first glance is very simple, just use backticks instead of single or double quotes:

const a_string = `something`


They are unique because they provide a lot of features that normal strings built with quotes do not, in particular:

they offer a great syntax to define multiline strings
they provide an easy way to interpolate variables and expressions in strings
they allow you to create DSLs with template tags (DSL means domain specific language, and it’s for example used in React by Styled Components, to define CSS for a component)
Let’s dive into each of these in detail.

Multiline strings

Pre-ES6, to create a string spanning over two lines you had to use the \ character at the end of a line:

const string = 'first part \ second part'


This allows to create a string on 2 lines, but it’s rendered on just one line:

first part second part

To render the string on multiple lines as well, you explicitly need to add \n at the end of each line, like this:

const string ='first line\n \ second line' 

or

const string = 'first line\n' + 'second line'


Template literals make multiline strings much simpler.

Once a template literal is opened with the backtick, you just press enter to create a new line, with no special characters, and it’s rendered as-is:

const string = `Hey this string is awesome!`

Keep in mind that space is meaningful, so doing this:

const string = `First Second`


is going to create a string like this:

First Second
an easy way to fix this problem is by having an empty first line, and appending the trim() method right after the closing backtick, which will eliminate any space before the first character:

const string = `First Second`.trim()

Interpolation

Template literals provide an easy way to interpolate variables and expressions into strings.

You do so by using the ${...} syntax:

const myVariable = 'test'
const string = `something ${myVariable}` //something test
inside the ${} you can add anything, even expressions:

const string = `something ${1 + 2 + 3}`
const string2 = `something ${foo() ? 'x' : 'y'}`


 

Series: React JS Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.