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
How to fix Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') "loadeddata" in mobile audio api
I've been following this youtube video https://www.youtube.com/watch?v=1-CvPn4AbT4 to build a mobile audio player with playlist to embed in a music website. At 1:12:16 I'm getting stuck with an error reading; Uncaught TypeError: Cannot read…

mim
- 57
- 9
-1
votes
3 answers
How can I get the number of characters in a line in a template literal?
I have a multi-line template literal. I want to splice it based on line number and character index in the line. For that, I need the number of characters in a given line. How can I get this number?
As for what I have tried, well, I pretty much only…

mikwee
- 133
- 2
- 14
-1
votes
1 answer
How to make a true/false ternary operator simpler in a HTML template?
Below a partial html template of mine; I find it quite ugly...
Is there a way to make ${dod.city === true ? true : false} ternary simpler?
I couldn't find any way online.
Thanks a lot.

charnould
- 2,480
- 3
- 19
- 23
-1
votes
1 answer
What is difference between parameter with ' ' and parameter without ' ' in Template literals (js)
What is the difference between parameter with quotation and parameter without quotation in Template literals in JavaScript
Why when I put quotation >> ('${key}') it does the function >> fun('${key}') while when I delete quotation >> (${key}) it…

memo_o
- 37
- 1
- 3
-1
votes
2 answers
How to select multiple idexed ids using Javascript?
i have 6 divs with id wrapperX (X is number 1 till 6)
when I want to change its background color with for loop error show on the log which says Uncaught TypeError: Cannot read property 'style' of null
when I'm not use template literal it works but I…

Musadarj
- 62
- 3
- 9
-1
votes
2 answers
Template literals in methods on objects Javascript
I am having an issue when trying to console.log a template literal. As you can see below, I have a simple object of samsung, which has a method where it turns on and prints the template literal to the console.
let samsung = {
model: "S21",
…

CG-180
- 23
- 4
-1
votes
2 answers
Function to replace a substring with a function value in javascript
I need a function which will accept test arguments and return a formatted response
format('Hello, {name}!', {name: 'world'}); // Hello, world!
format('Hello, {user.name.first} {user.name.last}!', {user: {name: {first: 'John', last: 'Smith'}}}); //…

Ninad Singh Negi
- 21
- 8
-1
votes
1 answer
GraphQL mutation using template literal types ${}
I am currently having issues with Graphql mutation. Hard coding updating elements works but option 2 where I pass in argument does not.
On Google Dev Tools Network, I see I am passing [object Object] as request for elements.
I tried changing to code…

javascriptnoob
- 101
- 10
-1
votes
1 answer
Javascript template literals work when declared as a variable, but not when passed as a json template
Trying to wrap my head around Javascript. If I have a declared variable with template literals, for example:
var templateObject = `Student name is ${filteredJSONExample.name} and student age is ${filteredJSONExample.age}.`
This works as per…

Beyond Space Time
- 45
- 4
-1
votes
1 answer
Template literals inside of quotation marks
How can I write this so it works?
document.getElementById("td-`${i}`")

vturkovic
- 117
- 2
- 8
-1
votes
2 answers
javascript - simplify: if Var is empty-ish then "", else enclose Var with text
I am writing es templates.
I want to make the codes as simple as possible and at best avoid using (function(){return XXX})()
The context: When a list has nothing, prints nothing, otherwise prints the list with ul tags around.
${
// same filter 2…

John Lok Ho
- 3
- 1
- 2
-1
votes
2 answers
Issues with console.log vs. return in JS
I'm running into some issues here and I was hoping to get some help. I have the following function with a for loop as well as a template literal. If I use console.log I am getting the results I expect (posted below) however If I try to return it, i…

Randy
- 1
- 3
-1
votes
2 answers
Could not insert data into the template literal for SMS distribution
I have a problem with the template literals JS.
I get user data and an SMS template from a MySQL table.
// User fields
let name = data.info.name; // Chingis
let start_datetime = data.info.start_datetime; // 05.07.2020 15:20
let end_time =…

Chingis Khuurak
- 13
- 2
-1
votes
1 answer
How to perform for loop inside backticks in javascript?
I have group by an array of elements based on their resturant id.
function grouparray(list,key)
{
return list.reduce(function(rv,x){
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
},{});
};
var group…

ram ram
- 27
- 5
-1
votes
1 answer
Using template literal to dynamically add values to setState in React without eval()
I'm new at React and trying to create a scoreboard app which has the following state:
state = {
home_team: [
{
id: 1,
name: "Jobe Watson",
goals: 0,
behinds: 0,
},
{
id: 2,
name:…

Tom Madsen
- 13
- 1