Template literals are JavaScript string literals which allow embedded expressions. They allow for multi-line strings and string interpolation. In previous ECMAScript specifications, they were called "template strings".
Questions tagged [template-literals]
547 questions
11
votes
2 answers
template literals in jsx
I want to use this but props
10
votes
3 answers
Are JavaScript template literals guaranteed to call toString()?
const num = 42
const str = `My number is ${num}`
In this code what guarantee do I have about the conversion of num to a string ?
Is it guaranteed to just call its toString() method or could the conversion be done in another way ?

Drax
- 12,682
- 7
- 45
- 85
10
votes
2 answers
How to call calling a function inside a Template literal
How can I go about calling a function inside a Template literal.
The function syntax in the attempt below shows in the HTML:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
var html…

Program-Me-Rev
- 6,184
- 18
- 58
- 142
9
votes
1 answer
Is it possible to generate string literal combinations with template literal in TypeScript?
Is it possible to generate permutation of combinations of string literal with template literal in TypeScript?
type MetaKey = 'meta';
type CtrlKey = 'ctrl';
type ShiftKey = 'shift';
type AltKey = 'alt';
type ModiferKeyCombinations = ???
where…

aztack
- 4,376
- 5
- 32
- 50
9
votes
2 answers
Why can't String.raw end with a backslash?
String.raw can be used to create a string that contains backslashes, without having to double up those backslashes.
Historically, you'd need to double up backslashes when creating a string:
let str = "C:\\Program…

Lonnie Best
- 9,936
- 10
- 57
- 97
9
votes
4 answers
How would you turn a JavaScript variable into a Template literal?
I would like to take text that I generated and stored in a string and use it like a template literal.
var generatedText = "Pretend this text was generated and then stored in a variable. ";
generatedText = "But I still need to use it as a template…

Trevor
- 139
- 1
- 7
9
votes
3 answers
Setting HTML Button`onclick` in template literal
I have an html template that i'm using template literals for. The function looks like the below
// postCreator.js
export const blogPostMaker = ({ title, content, address, id }, deletePost) => {
const innerHtml = `
Elliott McNary
- 1,149
- 3
- 11
- 20

Elliott McNary
- 1,149
- 3
- 11
- 20
9
votes
4 answers
Text highlighting for ES6 template literals (backticks) in IntelliJ IDEA
Where should I look in the settings to remove the green background highlighting for text within the backticks? I think it is related to HTML in non-HTML files, probably it has nothing to do with quotes or backticks. I've been searching through…
user89862
8
votes
2 answers
Truly recursive Template Literal for comma-separated strings in Typescript
I am trying to define a Typescript template literal for a string containing comma-separated values. Can I make this definition truly recursive and general?
See this typescript playground to experiment with the case.
Each comma separated value…

cefn
- 2,895
- 19
- 28
8
votes
6 answers
Ternary inside string literals
Been trying to add a conditionals inside a template strings. I am unable to do it. I have two strings say t1 and t2, and when t2 is undefined or empty just display t1 and when t2 is present append that t2 inside parentheses along with t1
let t1=…

joy08
- 9,004
- 8
- 38
- 73
8
votes
1 answer
HTML Element versus Javascript Template Literals
Mozilla says Web components consist of three main technologies:
Custom elements
Shadow DOM
HTML templates
Is number 3, "HTML templates", even necessary in light of ECMAscript's Template Literals?
Look at this example I got from James…

Lonnie Best
- 9,936
- 10
- 57
- 97
8
votes
2 answers
javascript console.log new feature with 'raw'?
I have encounter this example and was completely lost...
const test = (hey) => console.log(hey);
console.log(test `wtf`);
First all this is valid, in the console.log, it appear to be
["wtf", raw: Array[1]]
It's like the function is been executed…

Bill
- 17,872
- 19
- 83
- 131
7
votes
2 answers
How do I escape a backtick in a JavaScript raw string?
I have a raw string (created with the String.raw method and template literal), which is supposed to contain several backslashes and backticks. Since the backticks are required to be escaped even in a raw string, I use backward slashes to escape…

Arjun
- 1,261
- 1
- 11
- 26
7
votes
1 answer
Template literals as string content in JSX
I'm wondering whats the best practice for strings values mixed with variables inside JSX tags, I've listed the options I'm familiar with:
render() {
const {totalCount} = this.state;
const totalCountStr = `Total count: ${totalCount}`;
…

Tim
- 165
- 3
- 9
7
votes
1 answer
Array map in template literal renders an extra comma between items
In the following example I am trying to render a list of posts (title, body and their tags):
const container = $('.container');
posts.forEach((post)=> {
container.append(
`