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
-1
votes
1 answer
Render MULTIPLE Template Literals of Multiple Javascript variables
I have the following code above, which came from this answer:
const interpolate = (str, obj) => str.replace(
/\${([^}]+)}/g,
(_, prop) => obj[prop]
);
const generatedText = "But I still need to use it as a template it to get…

Gustavo Aguiar
- 87
- 13
-1
votes
1 answer
How to set onclick listener with parameter when creating HTML with a template literal?
I'm working on a code quiz and used the map function to iterate through an array of objects which each contain a title, choices and an answer for each question. I used a template literal to make the code easily readable. The key in the map function…

Matthias Quintero
- 51
- 11
-1
votes
2 answers
javascript template litreral in side a react link tag to={} attribute
Hi, I am using template literal in a react app link tag and it is showing below error message please tell if anyone knows how to add it correctly?
Unexpected template string expression no-template-curly-in-string

famo
- 160
- 1
- 13
-1
votes
1 answer
JavaScript template literals and implicit coercion
What value should be assigned to "x" in order to meet the following condition:
// let x = ?;
console.log(`${x}` != '' + x) // true
console.log(`${x}` !== '' + x) // true

ebv
- 9
- 4
-1
votes
1 answer
Unexpected white space using function in template literal
I'm making an app using API from one website that stream music and I run into a problem.
When I click on play button I'm getting an error: Uncaught SyntaxError: Unexpected end of input and when I click on it it shows only this:…

Nikola Seke
- 122
- 2
- 10
-1
votes
1 answer
Not able to interpolate
I'm currently doing the codecademy introduction to javascript.
const takeOrder = (topping) => {
console.log('Order: pizza topped with ${topping}');
};
takeOrder("mushrooms");
shouldn't this work too instead of
const takeOrder = (topping) => {
…

Cause
- 1
- 1
-1
votes
2 answers
array literal: This type can not be coerced to string
I have an object
const arr = [{ x: 1, y: 2 }];
I want to use it in template literals, because of my use case but getting error, Suppose if we log this array using template literal like
console.log(`${arr}`);
We will get this error
array literal:…

Amante Ninja
- 647
- 1
- 6
- 26
-1
votes
1 answer
Template Literals How To Make a ?Universal Rule?
I'm new to coding but I am using template literals in a project using css variables. this example sets all the variables in one shot inside a function. this is referring to inputs which all have an eventlistener on…

benjaminadk
- 468
- 6
- 18
-2
votes
2 answers
Using Javascript Template Literals With The prepend() Method
I have a loop of button elements that are outputted with a while loop from data called from a MySQL database via PHP.
A user can add a button to this list and I want to add the new button and it's associated HTML using the prepend() method on the…

pjk_ok
- 618
- 7
- 35
- 90
-2
votes
1 answer
Mapping over elements matching string
I have a map
.map((item: { amount: number}) =>
The issue is that object prop is not always amount but will start with that. so it could be
.map((item: { amountTotal: number}) =>
.map((item: { amountConsolidated: number}) =>
Is there a way to regex…

TommyD
- 913
- 3
- 17
- 32
-2
votes
1 answer
How to concatenate string and html element with literals?
basically trying to combine some text with a link:
const myText = `${t('privacyTxt')}` + `${{t('privacyLink')}}`;
result:
To learn how we process your data, visit our[object Object]
What am I missing?

CodingLittle
- 1,761
- 2
- 17
- 44
-2
votes
1 answer
How to puts html elements inside of the template literal expression
let word = "kemal"
let test = document.createElement("div");
test.innerText = `lorem la la bla bla "${word}"`;
document.querySelector("body").appendChild(test);
I want to make bold font to word variable. But I got an error about…

Hasan Tezcan
- 1,116
- 1
- 11
- 23
-2
votes
2 answers
How to write newline in google script
how can I write multiline string google script
"Dear "+mailData.name+","+"Thank you for writing to us. We have received your message about "+mailData.subject+
" and will get back to you within 24 hours. Until then, you can give us an email…

Joel Sunny Varghese
- 3
- 1
- 3
-2
votes
1 answer
Combining HTML with Json
I am trying to create one HTML file that has JSON data embedded in it. The one file can then use the JSON data to create HTML such as a table and so forth.
I have tried template literals, but I'm not sure if I am doing it correctly or what I am…

yanci
- 163
- 1
- 13
-3
votes
2 answers
Javascript: template literals not returning ${} syntax output
I am watching a YouTube video about learning javascript from some years ago, and in the video the code seems to work except on my VScode.
Here is the code, could you please let me know what I might have missed out:
function Person(firstName,…

The Future
- 55
- 1
- 5