When working with string in JavaScript, you might end up writing code like this:


    const name = 'Chris';
    const age = 28;
    const sentence = 'My name is: ' + name + ' and I am ' + age + ' years old.

Which is ok, kind of. It get's the job done but it's difficult to read and there are single quotes and plus signs everywhere. Boo!

Enter... Template Literals! Using template literals we can write the same code like this:


    const name = 'Chris';
    const age = 28;
    const sentence = `My name is ${name} and I am ${age} years old.`;

Which is much easier to read! In both examples, the sentence variable is a String. Note: Template literals are opened within back ticks not single quotes!

This is part of the JavaScript bite-size series of posts. Check out the rest here!