Javascript quotes and template literals

You can use template literals to ease your coding with strings. Especially with many parameters and multi lines.

Don’t mix placeholder ${ } with element indicators. Check the tutorial :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

console.log('string text line 1\n' +
'string text line 2');
// "string text line 1
// string text line 2"
// or
console.log(`string text line 1
string text line 2`);
// "string text line 1
// string text line 2" 

var a = 5;
var b = 10;
console.log('Fifteen is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.');
// "Fifteen is 15 and
// not 20."

var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b} and
not ${2 * a + b}.`);
// "Fifteen is 15 and
// not 20."

Leave a Reply

Your email address will not be published. Required fields are marked *