Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a downside to using ES6 template literals syntax without a templated expression?

Is there a reason (performance or other) not to use backtick template literal syntax for all strings in a javascript source file? If so, what?

Should I prefer this:

var str1 = 'this is a string'; 

over this?

var str2 = `this is another string`; 
like image 488
Joshua Breeden Avatar asked Jun 12 '16 18:06

Joshua Breeden


People also ask

Should you always use template literals?

Don't use template literals unless you need interpolation, multiline literals, or unescaped quotes and apostrophes.

Can I use JavaScript template literals?

The basic syntax of JavaScript template literalsUsing the backticks, you can freely use the single or double quotes in the template literal without escaping.

Are template literals faster?

Theoretical speaking (unless the JS is compiled), template literals would be slower since the 'string' needs to be parsed regardless of placeholder existence.

Why backticks are used in JavaScript?

Backticks are an ES6 feature that allows you to create strings in JavaScript. Although backticks are mostly used for HTML or code embedding purposes, they also act similar to single and double quotes. Besides, using backticks makes it easier for string operations.


2 Answers

The most significant reason not to use them is that ES6 is not supported in all environments.

Of course that might not affect you at all, but still: YAGNI. Don't use template literals unless you need interpolation, multiline literals, or unescaped quotes and apostrophes. Much of the arguments from When to use double or single quotes in JavaScript? carry over as well. As always, keep your code base consistent and use only one string literal style where you don't need a special one.

like image 192
Bergi Avatar answered Sep 21 '22 12:09

Bergi


Code-wise, there is no specific disadvantage. JS engines are smart enough to not have performance differences between a string literal and a template literal without variables.

In fact, I might even argue that it is good to always use template literals:

  • You can already use single quotes or double quotes to make strings. Choosing which one is largely arbitrary, and you just stick with one. However, it is encouraged to use the other quote if your string contains your chosen string marker, i.e. if you chose ', you would still do "don't argue" instead of 'don\'t argue'. However, backticks are very rare in normal language and strings, so you would actually more rarely have to either use another string literal syntax or use escape codes, which is good.

    For example, you'd be forced to use escape sequences to have the string she said: "Don't do this!" with either double or single quotes, but you wouldn't have to when using backticks.

  • You don't have to convert if you want to use a variable in the string in the future.

However, those are very weak advantages. But still more than none, so I would mainly use template literals.

A real but in my opinion ignorable objection is the one of having to support environments where string literals are not supported. If you have those, you would know and wouldn't be asking this question.

like image 30
Timo Türschmann Avatar answered Sep 23 '22 12:09

Timo Türschmann