web-profile

JavaScript Template Literals

Template literals are a new way of defining strings by using backticks ` instead of single or double quotes.

const name = 'Jim';
console.log(`Hello ${name}`); // Output: Hello Jim


const a = 2;
const b = 3;
console.log(`${a} + ${b} = ${a + b}`); // Output: 2 + 3 = 5


console.log(`This is
a multiline
string.`);
/*
This is
a multiline
string
*/


console.log(`First line\nSecond line\t\t\tafter tabs`);
/*
First line
Second line     after tabs
*/

Leave a Reply

Your email address will not be published.