Questions tagged [template-strings]

Prefer using template-literals tag over template-strings that is the old name from previous ECMAScript specifications.

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

127 questions
333
votes
10 answers

Usage of the backtick character (`) in JavaScript

In JavaScript, a backtick† seems to work the same as a single quote. For instance, I can use a backtick to define a string like this: var s = `abc`; Is there a way in which the behavior of the backtick actually differs from that of a single…
vancewang
  • 3,990
  • 3
  • 12
  • 14
173
votes
18 answers

Convert a string to a template string

Is it possible to create a template string as a usual string let a="b:${b}"; an then convert it into a template string let b=10; console.log(a.template());//b:10 without eval, new Function and other means of dynamic code generation?
KOLANICH
  • 2,234
  • 2
  • 16
  • 19
105
votes
3 answers

Backticks calling a function

I'm not sure how to explain this, but when I run console.log`1` In google chrome, I get output like console.log`1` VM12380:2 ["1", raw: Array[1]] Why is the backtick calling the log function, and why is it making a index of raw:…
98
votes
4 answers

ES6 Template Literals Vs concatenated strings

I have the following code for Ecma-Script-6 template literals let person = {name: 'John Smith'}; let tpl = `My name is ${person.name}.`; let MyVar="My name is "+ person.name+"."; console.log("template literal= "+tpl); console.log("my…
Naeem Shaikh
  • 14,231
  • 4
  • 43
  • 80
85
votes
2 answers

Template String As Object Property Name

Why does JavaScript not allow a template string as an object property key? For example, when I input: foo = {`bar`: 'baz'} into the NodeJS REPL, it throws a SyntaxError with "Unexpected template string" with a long stack trace. Property values are…
83
votes
7 answers

Template Strings ES6 prevent line breaks

I have a long string, which I build using ES6 template strings, but I want it to be without line breaks: var string = `As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than…
dim0_0n
  • 2,078
  • 5
  • 21
  • 44
32
votes
6 answers

Number formatting in template strings (Javascript - ES6)

I was wondering if it is possible to format numbers in Javascript template strings, for example something like: var n = 5.1234; console.log(`This is a number: $.2d{n}`); // -> 5.12 Or possibly var n = 5.1234; console.log(`This is a number:…
aquavitae
  • 14,088
  • 7
  • 53
  • 99
30
votes
6 answers

What are the actual uses of ES6 Raw String Access?

What are the actual uses of String.raw Raw String Access introduced in ECMAScript 6? // String.raw(callSite, ...substitutions) function quux (strings, ...values) { strings[0] === "foo\n" strings[1] === "bar" strings.raw[0] === "foo\\n" …
Venkat.R
  • 6,616
  • 5
  • 34
  • 57
26
votes
4 answers

Is it possible to have a comment inside a es6 Template-String?

Let's say we have a multiline es6 Template-String to describe e.g. some URL params for a request: const fields = ` id, message, created_time, permalink_url, type `; Is there any way to have comments inside that backtick…
lukash
  • 468
  • 1
  • 4
  • 10
25
votes
1 answer

How to nest template strings in ES6?

I got an prefer-template error from eslint. For the workaround, I changed my code to use a template string inside the require function which is nested inside the url function as following: { background:…
lvarayut
  • 11,333
  • 14
  • 59
  • 80
23
votes
1 answer

Why can functions be called without parentheses when using template strings?

I have a simple logging function: function log(str) { console.log('logged: ', str); } If I call it without parentheses (currently using Chrome's dev tools) and pass in a template string, like this: log`foo` The output is: logged: ["foo", raw:…
22
votes
1 answer

How can I construct a Template String from a regular string?

So I have this string: var name = "Chaim"; var templateStr = "Hello, my name is ${name}"; How can I convert it into a template-string so that the result would be equal to: var template = `Hello, my name is ${name}`; Is there a way to…
haim770
  • 45,540
  • 6
  • 91
  • 121
18
votes
2 answers

Why does IntelliJ keep removing backticks from this JavaScript template string?

I want to put "backticks" around my template strings. IntelliJ keeps removing them every time I try the wrap them around the string. Anyone's got a clue why its happening and how to solve this? I added a little code snippet of my .vue file where the…
Lucca
  • 1,158
  • 2
  • 12
  • 19
13
votes
3 answers

ES6 tagged templates practical usability

I understand the syntax of ES6 tagged templates. What I don't see is the practical usability. When is it better than passing an object parameter, like the settings in jQuery's AJAX? $.ajax('url', { /*this guy here*/ }) Right now I only see the…
Gábor Imre
  • 4,754
  • 2
  • 28
  • 44
12
votes
7 answers

Javascript conditional in template string

Is there a way to do conditional within a template string? For example: let x, y; x = ... y = ... let templateString = `${x} ${y}`; I don't want the space in the template string after x to be output if y is undefined. How would I achieve that…
Boon
  • 37,606
  • 51
  • 186
  • 296
1
2 3
8 9