ES6 Template Literals or Template Strings

ES6: Template Literals or Template Strings
const a = 5;
const b = 6;
let sum = a+b;//ES5 String concatenation
console.log(“The sum of “ + a + “ and “ + b + “ is “ + sum);
const a = 5;
const b = 6;
let sum = a+b;//ES6 Template literals
console.log(`The sum of ${a} and ${b} is ${sum}`);
console.log(`Hello “this is text in in double quotes” and ‘this one in single quotes’`);//And this print the same without any error.
//Output: Hello "this is text in in double quotes" and 'this one in single quotes'
const a = 5;
const b = 6;
//let sum = a+b;
//Instead of making third variable sum just pass a+b in console.log statement.
//ES6 Template literals
console.log(`The sum of ${a} and ${b} is ${a+b}`);
//new Date().getFullYear() returns the current yearfunction whichYear(){
return `This year is ${new Date().getFullYear()}.`;
}
whichYear();//Output : This year is 2020.
const year=new Date().getFullYear()//Never do this it’s not wrong but not a good practise
//const yearStr=`${year}`;
//Instead you can write it like this no need to wrap in template string
const yearStr=year;

--

--

--

Full-stack Developer | UX/UI | Never ending thirst to learn.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Keep it Small: Find Components By Bundle-Size and Dependencies

JavaScript — For…In Statement

Adopt These Strategies to Choose the Right Tech Stack

tech stack

Unit Testing Behavior of React Components with Test-Driven Development

Step 1 — Create an Apollo GraphQL server

State of JavaScript 2017

Building To-Do List App Using vanilla JavaScript For Absolute Beginners

Enjoy making DAPPs using SvelteWeb3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sourabh Gupta

Sourabh Gupta

Full-stack Developer | UX/UI | Never ending thirst to learn.

More from Medium

Express and React with concurrently

JWT Tokens and What’s special about them?

Masking Uploaded File Name

sanitizeEntity in Strapi v4