Questions tagged [template-literals]

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".

547 questions
7
votes
5 answers

How to use template literals (``) within template literals?

I have a very specific edge case which I need to use a template literal within a template literal but can't get it done. The code looks something like this:

Something something SomeLink

However I will have to…
theJuls
  • 6,788
  • 14
  • 73
  • 160
7
votes
1 answer

Check if a string is a template literal in JavaScript

Is it possible to test if a string is a template literal? Something like: const x = "foo" const y = `${x}bar` // "foobar" isTemplateLiteral(x) // false isTemplateLiteral(y) // true
Dan Mandel
  • 637
  • 1
  • 8
  • 26
7
votes
1 answer

changing string delimiters to backticks : possible impact?

ES6 introduced template strings delimited by backticks `. In which cases would replacing single ' or double " quotes around a string by backticks yield a different result, or otherwise be unsafe ? Escaping of existing backticks inside the code is…
6
votes
1 answer

How do I create a multiline string in Raku?

In JavaScript (ES6), you can use template literals (``) to create multiline strings as shown in the following example: const html = `

Raku is ofun.

` What's the Raku equivalent of this?
uzluisf
  • 2,586
  • 1
  • 9
  • 27
6
votes
1 answer

Template Literal type equivalent

I have an enum like so: export enum ApiFunctions { "setHidden" = "HIDE", "setReadOnly" = "SET_READ_ONLY", "setVisible" = "SHOW", "setDescription" = "SET_DESCRIPTION", "setName" = "SET_NAME", "makeRequest" = "MAKE_REQUEST" } Earlier…
6
votes
0 answers

Get class name as type in TypeScript

In TypeScript 4.1, is it possible to get a class name or a function name as literal string type? e.g. class Foo { } type Tpl = `${Foo["name"]}_${"a" | "b" | "c"}`; // error // should be // "Foo_a" | "Foo_b" | "Foo_c" Edit (extended use…
6
votes
2 answers

How to import HTML files as templates into Rollup and compile to concatenated strings

I'm creating a build process for Optimizely experiments using Rollup. We were currently using Webpack but that exports bloated code for this use case. I want to be able to import .html files as templates and compile them to ES5 compatible…
chrisfwd
  • 81
  • 1
  • 7
6
votes
3 answers

Passing array of strings to a GraphQL query, convers to integers when embedded

I'm trying to figure out a way to pass an array of strings and embed it into a query (using React, GraphQL). The problem is it accepts the parameter as an array of strings, but converts it to a string when I embed it. Let's say I have this function…
Elena
  • 61
  • 1
  • 1
  • 2
6
votes
2 answers

How do I put a single backslash into an ES6 template literal's output?

I'm struggling to get an ES6 template literal to produce a single backslash it its result. > `\s` 's' > `\\s` '\\s' > `\\\s` '\\s' > `\\\\s` '\\\\s' > `\u005Cs` '\\s' Tested with Node 8.9.1 and 10.0.0 by inspecting the value at a Node REPL (rather…
brabster
  • 42,504
  • 27
  • 146
  • 186
6
votes
3 answers

Pass a JS Object as the scope to a JS Template Literal?

In python, you can do variable string interpolation like this: song_context = { "adjective": "funny" } ella_sings = "my {adjective} valentine".format(**song_context) Here, the song_context object formats variables in the ella_sings string. In ES6…
Myer
  • 3,670
  • 2
  • 39
  • 51
6
votes
1 answer

Combining ES6 unicode literals with ES6 template literals

If I want to print a unicode Chinese character in ES6/ES2015 javascript, I can do this: console.log(`\u{4eb0}`); Likewise, if I want to interpolate a variable into a template string literal, I can do this: let x = "48b0"; console.log(`The character…
6
votes
2 answers

JSON and Template Literals for multiline strings

I want to store some sql strings in a JSON file. This is how the JSON file looks in my ideal world [ "select t.index1, t.title, t.description, t.insertDate from myTable t join anotherTable t2 on t.index1 = t2.fIndex where t2.active = 1", …
santiago arizti
  • 4,175
  • 3
  • 37
  • 50
6
votes
1 answer

What does this `…${…}…` code in the node docs mean?

I am to trying to learn Express library and Node.js one step at a time. First I am looking at is the specifics of the Node reqiure(moduleName) function. I took a look at the documentation for this, and found some weird code in the example…
5
votes
5 answers

Javascript: Add together literal HTML results in loop

New to JS. All of the questions I could find with similar titles to mine were a bit too complicated for me to understand. Simply put, I am trying to loop through an array and return one result at the end that essentially adds/concatenates all of the…
Katie Reynolds
  • 317
  • 2
  • 16
5
votes
1 answer

Template literals and parentheses-less function calls

I recently learned that ES6 allows to make function calls without parentheses when using a template literal as a parameter. E.g. showstring`Hello World`; After reading a few articles on this functionality, however, I have come away with scant…
codemonkey
  • 7,325
  • 5
  • 22
  • 36