DEV Community

Passionate Coder
Passionate Coder

Posted on

5 1

Template Literals in ES6

Uses/ Benefits

1) Template literals are used to concatenate strings.
2) Placeholders/variables can be easily interpolated with template literals.
3) Makes code more readable and Clean

How to write

4) Strings or placeholders needs to be wrapped inside backtick
5) variables needs to wrapped like ${variable}

Usecases

1) Display : The sum of 10 and 15 is 25.

let a = 10;
let b = 15;

"The sum of "+a+" and "+b+" is "+(a+b) +"." // ES5
`The sum of ${a} and ${b} is ${a+b}` // ES6
Enter fullscreen mode Exit fullscreen mode

2) Display :
I am a Developer.
I love to code.

"I am a Developer.\nI love to code." // ES5
`I am a Developer. 
 I love to code.` // ES6 no need to use \n for new line. It will keep format same as written
Enter fullscreen mode Exit fullscreen mode

3) Display : Let's code together

"Let's code together"  // ES5
'Let\'s code together' // ES5
`Let\'s code together` // ES6
Enter fullscreen mode Exit fullscreen mode

For more information follow
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

You can also watch this:

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
frontendengineer profile image
Let's Code • Edited

nice Moni! Would like to add another material to help the community

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay