Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 9148

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 59k
Author
Asked: November 28, 20242024-11-28T07:42:10+00:00 2024-11-28T07:42:10+00:00

ES6 – A beginners guide – Template Literals

  • 59k

Welcome back! This time I will be covering Template Literals (or Template Strings as some people call them). Primarily, Template Literals are just “syntactic sugar” in the sense that the substitutions it makes make the code much more readable and it can remove the requirement for more variables in your code. Let's look at some examples…

The ES5 way

var person = {name: "Stefan Wright", age: 33}; var retString = "My name is: " + person.name + ", and I am " + person.age + " years old.";  console.log(retString); // Returns: My name is: Stefan Wright, and I am 33 years old 
Enter fullscreen mode Exit fullscreen mode

Above is a really simple example, we only have 2 variables to concatenate into our string and we're only using double quotes. It's not actually too hard to read as ES5. Let's try a harder variation, this time let's imagine we want to create a JSON string:

var device = {device_id:1, guid: "00000000-0000-0000-0000-000000000000", owner: "Stefan", name: "Samsung S20 Plus 5G"}; var todaysDate = "01/01/01"; var retString = '{"device_id":"' + device.device_id + '","device_guid":"' + device.guid + '","device_name":"' + device.name + '", "device_owner":"' + device.owner + '","device_loanDate":"' + todaysDate + '"}';  console.log(retString); // Returns: "{"device_id":"1","device_guid":"00000000-0000-0000-0000-000000000000","device_name":"Samsung S20 Plus 5G", "device_owner":"Stefan","device_loanDate":"01/01/01"}") 
Enter fullscreen mode Exit fullscreen mode

Wow! isn't that horrible! Such a mix of quotation marks, singles, doubles, did we miss any? (well, in my example we didn't but it took a long time to write!). This way of working is susceptible to syntax errors and it will take a long time to debug.

The ES6 way of working

Let's tidy this up a bit, bring in ES6 and the first example:

const person = {name: "Stefan Wright", age: 33}; const retString = `My name is: ${person.name}, and I am ${person.age} years old.`;  console.log(retString); // Returns: My name is: Stefan Wright, and I am 33 years old 
Enter fullscreen mode Exit fullscreen mode

Above you can see that we have removed the + symbols for concatenation, we've changes the double quotes for back-ticks and our variables are now wrapped with ${}. Let's have a look at the really long and horrible looking second example from above:

const device = {device_id:1, guid: "00000000-0000-0000-0000-000000000000", owner: "Stefan", name: "Samsung S20 Plus 5G"}; const todaysDate = "01/01/01"; const retString = `{"device_id":"${device.device_id }","device_guid":"${device.guid}","device_name":"${device.name}", "device_owner":"${device.owner}","device_loanDate":"${todaysDate}"}`;  console.log(retString); // Returns: {"device_id":"1","device_guid":"00000000-0000-0000-0000-000000000000","device_name":"Samsung S20 Plus 5G", "device_owner":"Stefan","device_loanDate":"01/01/01"} 
Enter fullscreen mode Exit fullscreen mode

So we've reduced the ES5 version down from 190 down to 166 characters, whilst making the whole string more readable and syntax highlighting in the IDE aids in making it even easier to read. You'll also notice that the output no longer returns escaped quotations which saves on further possible processing.

Extra

When using Template Literals, it is also possible to perform simple function evaluations such as adding two numbers together:

const device = {device_id:1, guid: "00000000-0000-0000-0000-000000000000", owner: "Stefan", name: "Samsung S20 Plus 5G"}; const todaysDate = "01/01/01"; const retString = `{"device_id":"${device.device_id + 10}","device_guid":"${device.guid}","device_name":"${device.name}", "device_owner":"${device.owner}","device_loanDate":"${todaysDate}"}`;  console.log(retString); // Returns: {"device_id":"11","device_guid":"00000000-0000-0000-0000-000000000000","device_name":"Samsung S20 Plus 5G", "device_owner":"Stefan","device_loanDate":"01/01/01"} 
Enter fullscreen mode Exit fullscreen mode

We could also call another function to be evaluated whilst building the Template Literals such as below where I call a function to add 21 days to the provided date:

const addDays = (date, days) => {     var result = new Date(date);     result.setDate(result.getDate() + days);     return result; } const device = {device_id:1, guid: "00000000-0000-0000-0000-000000000000", owner: "Stefan", name: "Samsung S20 Plus 5G"}; const todaysDate = new Date("01/01/01"); const retString = `{"device_id":"${device.device_id}","device_guid":"${device.guid}","device_name":"${device.name}", "device_owner":"${device.owner}","device_loanDate":"${todaysDate}","device_returnDate":"${addDays(todaysDate, 21)}"}`;  console.log(retString); // Returns: {"device_id":"11","device_guid":"00000000-0000-0000-0000-000000000000","device_name":"Samsung S20 Plus 5G", "device_owner":"Stefan","device_loanDate":"Mon Jan 01 2001 00:00:00 GMT+0000 (Greenwich Mean Time)","device_returnDate":"Mon Jan 21 2001 00:00:00 GMT+0000 (Greenwich Mean Time)"} 
Enter fullscreen mode Exit fullscreen mode

beginnerscodenewbiejavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.