var str = `string value`;
// Without template literal console.log('Without template literal \n multiline string'); // With template literal console.log(`Using template literal multiline string`);
Without template literal multiline string Using template literal multiline string
var name = 'World'; var cname = 'lidihuo'; console.log(`Hello, ${name}! Welcome to ${cname}`);
Hello, World! Welcome to lidihuo
var x = 10; var y = 20; console.log(`The product of the variables ${x} and ${y} is: ${x*y}`);
The product of the variables 10 and 20 is: 200
function TaggedLiteral(str) { console.log(str); } TaggedLiteral `Hello World`;
[ 'Hello World' ]
function TaggedLiteral(str, val1, val2) { console.log(str); console.log(val1+" "+val2); } let text = 'Hello World'; TaggedLiteral`Colors ${text} ${10+30}`;
[ 'Colors ', ' ', '' ] Hello World 40
var rawText = String.raw`Hello \n World \n Welcome back! ` console.log(rawText)
Hello \n World \n Welcome back!
console.log(String.fromCodePoint(49)) console.log(String.fromCodePoint(80, 76))
1 PL