惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
雷峰网
雷峰网
Recent Announcements
Recent Announcements
月光博客
月光博客
G
Google Developers Blog
腾讯CDC
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
T
Tenable Blog
云风的 BLOG
云风的 BLOG
W
WeLiveSecurity
博客园 - 【当耐特】
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
P
Privacy International News Feed
MyScale Blog
MyScale Blog
K
Kaspersky official blog
T
The Blog of Author Tim Ferriss
Attack and Defense Labs
Attack and Defense Labs
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
aimingoo的专栏
aimingoo的专栏
I
Intezer
Vercel News
Vercel News
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tor Project blog
S
Security Affairs
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
博客园 - Franky
C
Cyber Attacks, Cyber Crime and Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
美团技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Security @ Cisco Blogs
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
Project Zero
Project Zero
D
Docker
L
Lohrmann on Cybersecurity
F
Full Disclosure

Dmitri Pavlutin Blog

Pure Functions in JavaScript: A Beginner's Guide Record Type in TypeScript: A Quick Intro How to Write Comments in React: The Good, the Bad and the Ugly 4 Ways to Create an Enum in JavaScript React forwardRef(): How to Pass Refs to Child Components TypeScript Function Types: A Beginner's Guide How to Use v-model to Access Input Values in Vue Mastering Vue refs: From Zero to Hero Environment Variables in JavaScript: process.env 5 Must-Know Differences Between ref() and reactive() in Vue How to Destructure Props in Vue (Composition API) Triangulation in Test-Driven Development How to Use nextTick() in Vue Programming to Interface Vs to Implementation A Smarter JavaScript Mapper: array.flatMap() Array Grouping in JavaScript: Object.groupBy() How to Access ES Module Metadata using import.meta JSON Modules in JavaScript How to Trim Strings in JavaScript TypeScript Function Overloading How to Debounce and Throttle Callbacks in Vue How to Show/Hide Elements in Vue Sparse vs Dense Arrays in JavaScript How to Fill an Array with Initial Values in JavaScript Covariance and Contravariance in TypeScript What are Higher-Order Functions in JavaScript? How to Use TypeScript with React Components Index Signatures in TypeScript How to Use React useReducer() Hook unknown vs any in TypeScript A Guide to React Context and useContext() Hook How to Use Promise.any() 2 Ways to Remove a Property from an Object in JavaScript 'return await promise' vs 'return promise' in JavaScript How to Use Promise.allSettled() How to Use fetch() with JSON JavaScript Promises: then(f,f) vs then(f).catch(f) What is a Promise in JavaScript? How to Use Promise.all() A Simple Guide to Component Props in React Don't Stop Me Now: How to Use React useTransition() hook A Simple Explanation of JavaScript Variables: const, let, var ES Modules Dynamic Import How to Memoize with React.useMemo() How to Cleanup Async Effects in React Why Math.max() Without Arguments Returns -Infinity How to Debounce and Throttle Callbacks in React Don't Confuse Function Expressions and Function Declarations in JavaScript How to Use ES Modules in Node.js Solving a Mystery Behavior of parseInt() in JavaScript How to Use Array Reduce Method in JavaScript 3 Ways to Merge Arrays in JavaScript A Guide to Jotai: the Minimalist React State Management Library The Difference Between Values and References in JavaScript How to Implement a Queue in JavaScript A Helpful Algorithm to Determine "this" value in JavaScript React useRef() Hook Explained in 3 Steps 7 Interview Questions on "this" keyword in JavaScript. Can You Answer Them? How to Greatly Enhance fetch() with the Decorator Pattern 7 Interview Questions on JavaScript Closures. Can You Answer Them? What's a Method in JavaScript? array.sort() Does Not Simply Sort Numbers in JavaScript How to Solve the Infinite Loop of React.useEffect() The New Array Method You'll Enjoy: array.at(index) What's the Difference between DOM Node and Element? Why Promises Are Faster Than setTimeout()? Everything About Callback Functions in JavaScript How React Updates State 5 Mistakes to Avoid When Using React Hooks 5 Best Practices to Write Quality JavaScript Variables Type checking in JavaScript: typeof and instanceof operators 3 Ways to Check if a Variable is Defined in JavaScript React Forms Tutorial: Access Input Values, Validate, Submit Forms Prototypal Inheritance in JavaScript How to Timeout a fetch() Request How to Learn JavaScript If You're a Beginner A Simple Explanation of React.useEffect() A Simple Explanation of JavaScript Iterators How to Use React Controlled Inputs Everything about null in JavaScript How to Use Fetch with async/await Getting Started with Arrow Functions in JavaScript An Interesting Explanation of async/await in JavaScript Front-end Architecture: Stable and Volatile Dependencies Is it Safe to Compare JavaScript Strings? How to Access Object's Keys, Values, and Entries in JavaScript What Actually is a String in JavaScript? 3 Ways to Shallow Clone Objects in JavaScript (w/ bonuses) Checking if an Array Contains a Value in JavaScript JavaScript Event Delegation: A Beginner's Guide How to Parse URL in JavaScript: hostname, pathname, query, hash 3 Ways to Detect an Array in JavaScript How to Get the Screen, Window, and Web Page Sizes in JavaScript 3 Ways to Check If an Object Has a Property/Key in JavaScript How to Compare Objects in JavaScript Object.is() vs Strict Equality Operator in JavaScript Own and Inherited Properties in JavaScript 5 Differences Between Arrow and Regular Functions How to Use Object Destructuring in JavaScript Your Guide to React.useCallback()
How to Use Template Strings in JavaScript
Dmitri Pavlutin · 2020-01-22 · via Dmitri Pavlutin Blog

String interpolation is replacing placeholders with values in a string literal.

The string interpolation in JavaScript is done by template literals (strings wrapped in backticks `) and ${expression} as a placeholder. For example:


const number = 42;

const message = `The number is ${number}`;

console.log(message); // => 'The number is 42'


Let's see in more detail, with examples and best practices, how to use template strings.

Table of Contents

  • 1. The string literals
  • 2. The placeholders
    • 2.1 Implicit to string conversion
  • 3. Escaping placeholders
  • 4. Best practices
    • 4.1 Refactor string concatenation
    • 4.2 Helper variables
    • 4.3 Single quotes in placeholders
    • 4.4 Alternative solutions
  • 5. String interpolation in TypeScript
  • 6. String interpolation in React
  • 7. Conclusion

1. The string literals

In JavaScript, there are 3 ways to create string literals.

The first, which I prefer for plain strings, is to wrap the string into a pair of single quotes ':


const message = 'Hello, World!';


The second, which I use rarely, is to wrap the string into a pair of double quotes ":


const message = "Hello, World";


The third, which permits string interpolation, is to wrap the string into a pair of backticks `:


const message = `Hello, World!`;


The string literal wrapped in backticks ` is also named template string. This is the literal that supports the string interpolation.

Let's detail the template strings.

2. The placeholders

A great benefit of the template strings is the ability to easily inject dynamic values into the string using placeholders. The expression inside the placeholder is evaluated during runtime, and the result is inserted into the string.

The placeholder has a special format: ${expressionToEvaluate}. The expression inside the placeholder can be anything that evaluates to a value (but usually to a string):

  • variables: ${myVar}
  • operators: ${n1 + n2}, ${cond ? 'val 1' : 'val 2'}
  • even function calls ${myFunc('argument')}

Here's an example:


const greeting = 'Hello';

const who = 'World';

const message = `${greeting}, ${who}!`;

console.log(message); // => 'Hello, World!'


`${greeting}, ${who}!` is a template string having placeholders ${greeting} and ${who}.

During runtime, the first placeholder ${greeting} is replaced with the value of greeting variable ('Hello'), and the same for ${who} ('World!'). The string interpolation result is 'Hello, World!'.

You can put any expression inside the placeholder: either an operator, a function call, or even more complex expressions.


const n1 = 2;

const n2 = 3;

const message1 = `The sum is ${n1 + n2}`;

console.log(message1); // => 'The sum is 5';

function sum(num1, num2) {

return num1 + num2;

}

const message2 = `The sum is ${sum(n1, n2)}`;

console.log(message2); // => 'The sum is 5'


${n1 + n2} is a placeholder consisting of the addition operator and 2 operands. The placeholder ${sum(n1, n2)} contains a function invocation. All these values are evaluated during runtime and the resulting value is inserted into the template string.

2.1 Implicit to string conversion

The placeholder expression result is implicitly converted to a string.

For example, a number in a placeholder is transformed into a string:


const n = 3.5;

const message = `The number is ${n}`;

console.log(message); // => `The number is 3.5`


The expression n of the placeholder ${n} is evaluated to number 3.5. The number 3.5 is then transformed into a string '3.5', and inserted into the interpolation result: 'The number is 3.5'.

If the placeholder contains an object, following the conversion to string rule, the object is converted to a string too. The toString() method of the object is called to get the string representation of the object.

For example, let's insert an array into a template string:


const numbers = [1, 2, 3];

const message = `The numbers are ${numbers}`;

console.log(message); // => 'The numbers are 1,2,3'


The placeholder ${numbers} contains an array of numbers.

toString() array method executes array.join(',') when the array is converted to a string. The string interpolation result is 'The numbers are 1,2,3'.

3. Escaping placeholders

You cannot use the sequence of characters "${someCharacters}" without escaping it because the placeholder format ${expression} has a special meaning in the template literals.

Let's create a string literal containing the sequence of characters ${abc}:


const message = `Some weird characters: ${abc}`;

// Throws "ReferenceError: abc is not defined"


Inserting ${abc} directly throws an error because JavaScript interprets ${abc} as a placeholder (and there's no abc variable defined).

A backslash \ before the placeholder-like sequence of characters \${abc} solves the problem:


const message = `Some weird characters: \${abc}`;

console.log(message); // => 'Some weird characters follow: ${abc}'


In the template string `Some weird characters: \${abc}` JavaScript interprets \${abc} as a sequence of characters, rather than a placeholder.

Alongside with ${abc}, the sequence of characters like ${abc and ${ also have to be escaped with a backslash \:


const message = `Some weird characters: \${abc} \${abc \${`;

console.log(message); // => 'Some weird characters: ${abc} ${abc ${'


4. Best practices

4.1 Refactor string concatenation

The template strings should be used instead of string concatenation to construct lengthy strings.

If for some reason you're still concatenating string literals and expressions using + operator:


const n1 = 2;

const n2 = 3;

const message = 'The sum of ' + n1 + ' and ' + n2 + ' is ' + (n1 + n2);

console.log(message); // => 'The sum of 2 and 3 is 5'


Then switch to template strings and placeholders instead:


const n1 = 2;

const n2 = 3;

const message = `The sum of ${n1} and ${n2} is ${n1 + n2}`;

console.log(message); // => 'The sum of 2 and 3 is 5'


The template string requires less code and is easier to read than the concatenation of string.

4.2 Helper variables

When the template string contains many complex expressions, it might decrease the readability of the literal.

Here's a template string having placeholders with complex expressions:


const n1 = 2;

const n2 = 3;

const message =

`Sum: ${n1 + n2}, difference: ${n1 - n2}, pow: ${Math.pow(n1, n2)}`;

console.log(message); // => 'Sum: 5, difference: -1, pow: 8'


The more complex the placeholders are, the more tempting is to add helper variables to store intermediate values.


const n1 = 2;

const n2 = 3;

const sum = n1 + n2;

const diff = n1 - n2;

const pow = Math.pow(n1, n2);

const message = `Sum: ${sum}, difference: ${diff}, pow: ${pow}`;

console.log(message); // => 'Sum: 5, difference: -1, pow: 8'


With the introduction of helper variables sum, diff, and pow, the template string becomes easier to read. Additionally, the code self-documents when the intermediate variables are used.

4.3 Single quotes in placeholders

I recommended using single quotes ' rather than backticks ` in the expressions inside the placeholder.

Let's use the ternary operator. When the placeholder uses backticks ` it's quite difficult to read the template string because there are too many backticks in the template string:


function getLoadingMessage(isLoading) {

return `Data is ${isLoading ? `loading...` : `done!`}`;

}


Using single quotes inside the placeholder just looks better:


function getLoadingMessage(isLoading) {

return `Data is ${isLoading ? 'loading...' : 'done!'}`;

}


4.4 Alternative solutions

The string interpolation is helpful in many situations. But when the template string becomes large, with complex placeholder expressions, you might look for other solutions.

The following component builds the CSS class based on 2 variables:


function LoadingMessage({ isLoading, isModal }) {

const className =

`${isLoading ? 'loading' : ''} ${isModal ? 'modal' : ''}`;

return (

<div className={className}>

{isLoading ? 'Loading...' : 'Done!'}

</div>

);

}


The template literal that determines the class name has 2 ternary operators and a mix of string literals. You may find it difficult to understand.

A tool like classnames might be a better choice than the template string. The tool constructs the class name in a more declarative way.

Let's refactor the component to use classnames:


import classNames from 'classnames';

function LoadingMessage({ isLoading, isModal }) {

const className = classNames({

loading: isLoading,

modal: isModal

});

return (

<div className={className}>

{isLoading ? 'Loading...' : 'Done!'}

</div>

);

}


This version of the component, which uses the classnames tool, is easier to understand than the template string version.

Adding more CSS classes (for example to handle isErrorLoading) would not decrease the readability of the version that uses classnames utility (contrary to using a template literal).

5. String interpolation in TypeScript

The string interpolation in TypeScript works the same way as in JavaScript:


const count: number = 10;

const message: string = `You have ${count} products`;

console.log(message); // => 'You have 10 products'


6. String interpolation in React

The string interpolation in React consists of 2 cases:

  • Attribute interpolation: attribute={`My string ${placeholder}`}
  • Element's text interpolation: <span>My string {placeholder}</span>

Here's an example that shows both cases:


import React from 'react';

export function BuyProductsButton({ count, buy }) {

return (

<button

title={`Buy ${count} products`}

onClick={buy}

>

Buy {count} products

</button>

);

}


<BuyProductsButton count={10} buy={() => {}} /> would render the output:


<button title="Buy 10 products">

Buy 10 products

</button>


During the interpolation of both attribute and text strings, placeholders ${count} and correspondingly {count} are replaced with 10.

7. Conclusion

The template strings are great to insert values into strings in a concise and readable manner. And avoid the clumsy string concatenation approach.

In JavaScript, the template string implements the string interpolation.

A template string is defined by wrapping a sequence of characters into a pair of backticks `I'm template string`. The template string placeholders have the format ${expression}, for example `The number is ${number}`.

Don't overcomplicate the string literal. If the template string uses complex expressions, try to introduce intermediate variables to store the expressions before putting them into placeholders.

As soon as you need a value inserted into a string literal, the template string is the way to go.

Do you have any questions about template strings in JavaScript?