Home >Web Front-end >JS Tutorial >ES6 for Now: Template Strings
ES6 Template Literals: A Modern Approach to JavaScript Strings
Key Highlights:
(This article is part of a Microsoft web development series. Thanks to our partners for supporting SitePoint.)
ES6 is the present and future of JavaScript. This series explores its most useful features. My coding style consistently uses single quotes for strings, offering advantages in HTML string construction:
<code class="language-javascript">// Single quotes simplify HTML attribute quoting var buttonText = 'Save'; </code>
Avoiding escape characters improves readability. While HTML is flexible, prioritizing human-readable code is key. I've learned from past experiences with the complexities of DHTML to appreciate cleaner solutions.
Expression Substitution and Enhanced Readability:
My preference for single quotes stems from my PHP background, where they prevent variable substitution, improving performance. JavaScript previously lacked this feature, necessitating string concatenation:
<code class="language-javascript">var animal = 'cow'; var sound = 'moo'; alert('The animal is ' + animal + ' and its sound is ' + sound); </code>
This becomes cumbersome with longer strings and often leads to linting errors. JavaScript's lack of native multiline strings further complicates matters.
Template Literals: A Solution:
ES6 template literals solve these problems. The backtick (`) allows expression substitution and multiline strings:
<code class="language-javascript">var animal = 'cow'; var sound = 'moo'; alert(`The animal is ${animal} and its sound is ${sound}`);</code>
The ${}
construct handles any JavaScript expression, enabling calculations and object property access:
<code class="language-javascript">var output = `ten times two is ${10 * 2}`;</code>
Multiline strings are now effortless:
<code class="language-javascript">var list = ` - Buy Milk - Be kind to Pandas - Forget about Dre `;</code>
Tagged Templates:
Template literals can be "tagged" with a function, enabling custom string manipulation:
<code class="language-javascript">function urlify(str) { return encodeURIComponent(str); } urlify`http://example.com`;</code>
This function receives the string as an argument, allowing for operations like URL encoding.
Accessing String Parts within a Tag Function:
A tag function receives both the string parts and the embedded expressions as separate arguments:
<code class="language-javascript">function tag(strings, values) { console.log(strings); // Array of string parts console.log(values); // Array of embedded expressions } tag`you ${3 + 4} it`;</code>
The strings.raw
property provides the raw string parts, including escape sequences.
Browser Compatibility and Feature Detection:
While most modern browsers support template literals, ES6 transpilation (e.g., using Babel) is necessary for older browsers. Feature detection ensures compatibility:
<code class="language-javascript">// Single quotes simplify HTML attribute quoting var buttonText = 'Save'; </code>
Further Reading and Resources: (Links omitted for brevity, but original text included links to various articles on template literals.)
Microsoft Web Development Series: (Details about Microsoft's web development resources, including tools and learning materials, were included in the original text but omitted here for brevity.)
Frequently Asked Questions: (The original text contained a comprehensive FAQ section on template literals, which is omitted here due to length constraints.)
The above is the detailed content of ES6 for Now: Template Strings. For more information, please follow other related articles on the PHP Chinese website!