This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here! Smartphone icons created by Freepik - Flaticon
4.1.3 JS String Templates
Synonyms:
Template Literals
Template Strings
String Templates
Back-Tics Syntax
Back-Tics Syntax
Template Literals use back-ticks (``) rather than the quotes ("") to define a string:
Example
let text = `Hello World!`;
Try it yourself
Quotes Inside Strings
With template literals, you can use both single and double quotes inside a string:
Example
let text = `He's often called "Johnny"`;
Try it yourself
Multiline Strings
Template literals allows multiline strings:
Example
let text =
`The quick
brown fox
jumps over
the lazy dog`;
Try it yourself
Interpolation
Template literals provide an easy way to interpolate variables and expressions into strings.
The method is called string interpolation.
The syntax is:
${...}
Variable Substitutions
Template literals allow variables in strings:
Example
let firstName = "John";
let lastName = "Doe";
let text = `Welcome ${firstName}, ${lastName}!`;
Try it yourself
Automatic replacing of variables with real values is called string interpolation .
Expression Substitution
Template literals allow expressions in strings:
Example
let price = 10;
let VAT = 0.25;
let total = `Total: ${(price * (1 + VAT)).toFixed(2)}`;
Try it yourself
Automatic replacing of variables with real values is called string interpolation .
HTML Templates
Example
let header = "Templates Literals";
let tags = ["template literals", "javascript", "es6"];
let html = `<h2>${header}</h2><ul>`;
for (const x of tags) {
html += `<li>${x}</li>`;
}
html += `</ul>`;
Try it yourself
JavaScript icons used in the buttons provided by ICONS8.COM . Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Strings part 1
JS Strings part 2
JS Strings part 3
JS Strings length Property
JS Strings the escape sequence \"
JS Strings the escape sequence \'
JS Strings the escape sequence \\
JS Statements
JS Strings line break
JS Strings line break +
JS Strings line break NOT
JS Strings typeof
JS Strings - equal value
JS Strings - not equal type
JS Strings - cannot compare Objects
JS Strings - Objects cannot be compared
JS Strings slice()
JS Strings The slice() Method part 1
JS Strings The slice() Method part 2
JS Strings The slice() Method part 3
JS Strings the substring() method
JS Strings the substr() method part 1
JS Strings the substr() method part 2
JS Strings the substr() method part 3
JS Strings the replace() method part 1
JS Strings the replace() method part 2
JS Strings the replace() method part 3
Mobile test of using JS CSS
JS Strings the replace() method part 4
JS Strings the replace() method part 5
JS Strings Convert string part 1
JS Strings Convert string part 2
JS Strings Convert string part 3
JS Strings Convert string part 4
JS Strings The padStart() Method part 1
JS Strings The padStart() Method part 2
JS Strings The padStart() Method part 3
JS Strings The padEnd() Method part 1
JS Strings The padEnd() Method part 2
JS Strings The padEnd() Method part 3
JS Strings The charAt() Method
JS Strings The charCodeAt() Method
JS Strings Property Access part 1
JS Strings Property access part 2
JS String Methods String split() part 1
JS String Methods String split() part 2
JS String The indexOf() Method
JS Numbers part 1
JS Numbers part 2
JS Numbers part 3
Floating Point Precision part 1
Floating Point Precision part 2
Numbers and Strings part 1
Numbers and Strings part 2
Numbers and Strings part 3
Numbers and Strings part 4
Numbers and Strings part 5
Numbers and Strings part 6
Numbers and Strings part 7
Numbers and Strings part 8
JS Numbers, NaN (Not a Number) part 1
JS Numbers, NaN (Not a Number) part 2
JS Numbers, NaN (Not a Number) part 3
JS Numbers, NaN (Not a Number) part 4
JS Numbers, NaN (Not a Number) part 5
JS Numbers, NaN (Not a Number) part 6
JS Numbers Infinity part 1
JS Numbers Infinity part 2
JS Numbers Infinity part 3
JS Numbers - Hexadecimal part 1
JS Numbers - Hexadecimal part 2
JS Numbers As Objects part 1
JS Numbers As Objects part 2
JS Numbers As Objects part 3
JS Numbers As Objects part 4
JS Number Methods part 1
JS Number Methods part 2
JS Number Methods part 3
JS Number Methods part 4
JS Number Methods part 5
JS Number Methods part 6
JS Number Methods part 7
JS Number Methods part 8
JS Number Methods part 9
JS Number Methods MAX_VALUE Property
JS Number Methods MIN_VALUE Property
JS Number Methods POSITIVE_INFINITY Property part 1
JS Number Methods POSITIVE_INFINITY Property part 2
JS Number Methods NEGATIVE_INFINITY Property part 1
JS Number Methods NEGATIVE_INFINITY Property part 2
JS Number Methods NaN part 1
JS Number Methods NaN part 2
JS Number Methods Number Properties
JS Arrays part 1
JS Arrays part 2
JS Arrays part 3
JS Arrays part 4
JS Arrays part 5
JS Arrays part 6
JS Arrays part 7
JS Arrays part 8
JS Arrays part 9
JS Arrays part 10
JS Arrays part 11
JS Arrays part 12
JS Arrays part 13
JS Arrays part 14
JS Arrays part 15
JS Arrays part 16
JS Arrays part 17
JS Arrays part 18
JS Arrays part 19
JS Arrays part 20
JS Arrays part 21
JS Arrays isArray() Method
JS Arrays isArray() function
JS Arrays The instanceof Operator
JS Arrays The toString() Method
JS Arrays The join() Method
JS Arrays The pop() Method part 1
JS Arrays The pop() Method part 2
JS Arrays The push() Method part 1
JS Arrays The push() Method part 2
JS Arrays The shift() Method part 1
JS Arrays The shift() Method part 2
JS Arrays The shift() Method part 3
JS Arrays The shift() Method part 4
JS Arrays Bracket Indexing
JS Arrays The length Property
JS Arrays The delete Method
JS Arrays The splice() Method part 1
JS Arrays The splice() Method part 2
JS Arrays The splice() Method part 3
JS Arrays The concat() Method part 1
JS Arrays The concat() Method part 2
JS Arrays The concat() Method part 3
JS Arrays The slice() Method part 1
JS Arrays The slice() Method part 2
JS Arrays The slice() Method part 3
JS Arrays The slice() Method part 4
JS Arrays The toString() Method part 1
JS Arrays The toString() Method part 2
JS Arrays The sort() Method part 1
JS Arrays Sort in Reverse
JS Arrays The sort() Method part 2
JS Arrays The sort() Method part 3
JS Arrays The sort() Method part 4
JS Arrays The sort() Method part 5
JS Arrays The sort() Method part 6
JS Arrays The sort() Method part 7
JS Arrays The sort() Method part 8
JS Array Sort The Highest Number
JS Array Sort The lowest number
JS Arrays sort the highest number
JS Arrays sort the lowest number
JS Arrays sort car objects by age
JS Arrays sort car objects by type
JS Arrays The forEach() Method part 1
JS Arrays The forEach() Method part 2
JS Arrays The map() Method part 1
JS Arrays The map() Method part 2
JS Arrays The filter() Method part 1
JS Arrays The filter() Method part 2
JS Arrays The reduce() Method part 1
JS Arrays The reduce() Method part 2
JS Arrays The reduce() Method part 3
JS Arrays The reduceRight() Method part 1
JS Arrays The reduceRight() Method part 2