Template literals provide an easily readable way to embed expressions within string literals.
For example -
const thirdSmartPlanet = "earth";
const fact = `${thirdSmartPlanet} is flat`;
console.log("fact: ", fact); // fact: earth is flat
Earlier, you had to combine strings and variable like so -
const fact = thirdSmartPlanet + " is flat";
Hope you observed the backtick (’`’)?
Not only that -
You can output on multiple lines without breaking a sweat
const statement = `dogs are our best friends`; console.log(statement); /* output dogs are our best friends */Evaluate expressions
const a = 3; const b = 2; console.log(`sum of a and b is ${a + b}`); // sum of a and b is 5Nest template literals (though I don’t know why you would want to do that)
const a = 3; const b = 2; console.log( `sum of a and b is ${`${a + b > 10 ? "more than 10" : "less than 10"}`}` );