The provided content discusses the enhancements to JavaScript string handling with the introduction of ES6 template literals, which offer a more readable and flexible way to create and manipulate strings, including multi-line strings, interpolation, and HTML templates.
Abstract
The article titled "ES6 Cool Stuffs — A new JS string with template literals" delves into the limitations of JavaScript's string handling prior to ES6 and the improvements brought by template literals. It highlights how template literals simplify string creation, modification, concatenation, and the handling of long strings, interpolation with expressions, multi-line strings, and HTML templates. The author emphasizes the pain points of the old approach, such as the cumbersome use of concatenation and the difficulty in maintaining readability with complex strings. The new syntax using backticks allows for cleaner code, easier embedding of expressions, and better handling of multi-line strings and indentation. The article also touches on the advanced features of tagged template literals, which provide access to raw strings and enable more sophisticated string processing, such as localization and automatic HTML markup generation. The author concludes by discussing the performance aspects of template literals and expresses a preference for their simplicity and utility over traditional methods.
Opinions
The author expresses a clear preference for template literals over the traditional string handling methods in JavaScript, citing ease of use and improved readability.
There is an acknowledgment of the limitations of pre-ES6 string handling, particularly in dealing with complex strings involving multi-lines, interpolation, and HTML templates.
The author suggests that template literals are not just a syntactic sugar but a significant enhancement to JavaScript's capabilities in string manipulation.
While appreciative of template literals, the author has reservations about tagged template literals, mainly due to concerns about code readability.
The author values the stability of performance across different browsers when using template literals compared to traditional string concatenation.
There is a desire expressed for even more advanced string formatting features in JavaScript, akin to StringBuilder.AppendFormat in C#.
The article concludes with an open invitation for readers to share their thoughts and experiences with ES6 template literals, indicating a community-driven approach to learning and improvement.
ES6 Cool Stuffs — A new JS string with template literals
Cracking your head over string puzzle or relaxing with a template, which one you prefer? 😆
Well, strings support is one of nice features provided by modern languages for user interface. The question is to which supported level each language chooses to provide to strings’ functionalities — aka to what extend the language is willing to take in order to make our developer life easier.
So what’s up with JavaScript before ES6 and after ES6 on this matter?
Strings in JavaScript
Up until recent, JavaScript’s strings are provided and supported with only simple functionalities — such as:
Creation: JavaScript’s strings are created simply using string literals or String() global object directly.
let name = "Maya",
greetings = str1 + ", my name is " + name;
let greetings2 = [str1, ", my name is ", name].join('');
Simple. But wait a minute, what about
Long string (try to make it readable) ?
let longStr = "I'm a super long long long long long long\" +
"longlonglonglonglonglong\" +
"longlonglonglonglonglong\" +
"longlonglonglonglonglong\" +
" text. Check me out!";
Interpolate using expression, in addition to variable ?
//Need to use () to wrap expression
let printStr2 = "Hello " + (isWorld ? "World" : "me");
console.log(printStr2); //Hello World
Multi-lines string ?
let rhyme ="Are you sleeping? Are you sleeping?\nBrother John, Brother John!\nMorning bells are ringing!\nMorning bells are ringing!\nDing, ding, dong.\nDing, ding, dong.";
HTML raw template ?
let person={ name:"Maya",
hobbies: ["reading", "drawing", "traveling"],
job:"Developer"};
Or paragraph strings with indentation (or other special character)?
let paragraph ="Paragraph: \n"+"\t1. Point 1:\n"+"\t\t1.1 Sub-point 1:\n"+"\t\t1.2 Sub-point 2:\n"+"\t2. Point 2:\n"+"\t\t2.1 Sub-point 1:\n"+"\t\t2.2 Sub-point 2:\n"+"\t3. Point 3:\n"+"\t\t3.1 Sub-point 1:\n"+"\t\t3.2 Sub-point 2:\n"+"Conclusion: I'm exhausted! Did I miss anything?";
console.log(paragraph);
Or just combine ‘single quote’ and “double quote” in the same string?
let introduction = 'My name is "Maya Shavin", I' + "'m using single quote (') and double quote " + '(") here';
Enough to start feeling pained and headache? I bet you did (I did, many times — by the way). If so, clap your hands strongly for our hero today — template literals.
What is template literals?
Template literals is a additional way to create and handle dynamic strings/string templates, thus easing our pain by providing simpler and better solutions for mentioned cases above.
To put it simple — Template literals are string literals allowing embedded expressions.
How does it work?
Basic Syntax
Forget about which to choose between ‘single quote’ and “double quote” — JavaScript doesn’t care which one you use anyway.
And say hi to `backticks` , now you can use both‘single quote’ and “double quote” in the same string without any problem!
let myStr = `my new string`;
let introduction = `My name is "Maya Shavin", I'm using single quote (') and doublequote (") here`;
Expressions/Variables interpolation (of course)
Thank God now template literals can contain placeholders — which are indicated as${expression}. This syntax allows us to put any code expression in it — not just variable — evaluate that expression and produce the proper string value. Hence instead of
Cool, isn’t it? From now on, it’s up to us entirely to decide how much logic we want squeeze in the expression 😄.
Just bear in mind, don’t be too lazy (or greedy) and put too much logic inside it — think about other poor developers that may need to read your code one day. Readable comes first and foremost ✋!
Multi-lines strings and indented strings
This is probably one of the most awaiting feature for strings (at least for me, I’m sick of \n or \t , so unclean 😫)
So remember the tiring paragraph example above? It now can be re-write as:
let paragraph =
`Paragraph:1. Point 1:1.1 Sub-point 1:1.2 Sub-point 2:2. Point 2:2.1 Sub-point 1:2.2 Sub-point 2:3. Point 3:3.1 Sub-point 1:3.2 Sub-point 2:Conclusion:I'm exhausted! Did I miss anything?`;
console.log(paragraph);
What you see is what you get— that’s exactly how it works. Clean and clear as much as it can be! Bye bye 🐛
Note: Bear in mind — spacing does matter here. After all, what you see is what you get! 😉
What else?
HTML templates
With the help of multi-lines and indentation feature mentioned above, we finally can create neat, clean and understandable markup template without the help of 3rd party or making someone crazy just by looking at our code 😆
Note here I use nesting template literal for hobbies list items with <li> tag. This enables the possibility to break your templates into neat and easy-to use sub-components, maintaining the readability we are aiming for. Don’t you agree?
And the result will look like:
Feeling great so far? Good, because there is still more.
The above are the core features of template literals. However, despite the coolness, there are several limitations for template literals, as stated in the next section.
Limitations
No built-in syntax for looping
Think of when you need to print a list of dates
let days = ["Mon", "Tue", "Wed", "Thurs", "Fri", "Sat", Sun"];
Template literals alone will not able to help you to achieve it. You will need the help of Array.prototype.map and Array.prototype.join to accomplish it
let table = `<ul>
${days.map(day => `<li>${day}</li>`).join(" ")}
</ul>`;
BUT this is not as efficient in performance compared to traditional direct concatenation += with loop, even though it does not look as good:
let table = "<ul>";
for (let i = 0; i < days.length; i++){
table += `<li>${days[i]}</li>`;
}
table += "</ul>";
No support for different languages and locales format
Template literals are not meant to handle multiple language-specific formatting of numbers, dates, etc..
Luckily (or not), in an effort to address these limitations, ES6 also brings new concept to us , a more advanced version of template literals — Tagged Template Literals. Let’s have a quick look!
Tagged template literals
Tagged templates— template literals methods — mean to allow parsing template literals passed to a function call into that function’s input parameters as:
Template strings: array of static strings in passed template literals, separated by expressions — this is known in compile time
Substitutions: any expression appeared in template literals — delimited by ${} — on their own variables and are known only at run-time.
For instance, this following template literal
`My name is ${name}, and I'm ${age} years old`
when being passed to a function call, it will be parsed into the arguments:
["My name is", ", and I'm ", " years old"], <value of name>, <value of age>
In which:
Template strings: [“My name is”, “, and I’m “, “ years old”]
Substitutions: <value of name>, <value of age>
How do we pass it to function call?
Unlike normal, in order to pass a tagged template, we will use `backticks` and without() literal — which means:
func(`My name is ${name}, and I'm ${age} years old`); //Wrong
func`My name is ${name}, and I'm ${age} years old`; //Correct
And it will be interpreted as equivalent as
func([“My nameis”, “, and I’m “, “ years old”], <valueofname>, <valueof age>);
Hence in this example:
let name = "Maya", age = "30";
parseStr`My name is ${name}, and I'm ${age} years old`;
Sound advanced but not very useful, doesn’t it? So what will it be good for?
Saw the “raw” in the template strings array? Yup, that raw is a specialproperty, which allows user to access the raw strings, without processing all the special characters, i.e:
What else can it be useful for? Pretty everything we can think of or need tagged template for, such as creating re-usable HTML mark up template automatically, creating text localization (for supporting different languages and locales), etc… or even just get insane and become crazy with it 🚀!
Finally, after all the coolness, here comes the most important question — what about performance for template literals? Any specific performance improvement?
Performance
To be fair, template literals is meant to be an additional way of handling dynamic strings — which means it is aimed to fill in the missing string features JavaScript did not provide up to now and not to replace existing functionalities by any chance. As I always believe, everything exists for a reason 😉.
As seen here, template literals are faster in Chrome, but it can vary (depends on type of browser). To my opinion, while string concatenation can be a hit or a miss in performance in different browser, template literals performs quite more stable in multi-lines, concatenation and interpolation.
Conclusion
The truth is, personally I found template literals is simpler, more interesting and more useful than tagged template literals. Partially because I’m not a fan of unclear readability when using it (honestly, a function call without () ?).
I know there are much more complex use cases and I’m sure there is much deeper advantage in using tagged template literals. However, that requires more research in the future and actual usage exploration along the way.
For now, template literals rules for me, especially over the traditional way of multi-lining, concatenation and interpolation strings 🚀!
After all, who has the will and strength to go back to + and " " or \n days? I certainly don’t!
Finally, only one more little tiny wish for additional string feature — the same ability like StringBuilder.AppendFormat in C#. That will make my day 😜!
What do you think? Feel free to share your opinion. Sharing is caring, you know 😃