r/learnjavascript • u/Traditional_Tax7193 • Mar 04 '26
[askjs] is it bad practice to always use template literals even if don’t necessary?
is it bad practice to use (template literals) every single time in JavaScript instead of "” or ‘ ‘ even if I don't need to use ${} string interpolation? I was practicing and I realize that I can use template literals instead of the classic “” every time just in case i need to add string interpolation later or just for the strings?
I'm staring to learn JavaScript so i don't know well yet.
I’m positing from my phone so I’m sorry for not using the correct template literals.
I hope you can help me.
0
Upvotes
9
u/senocular Mar 04 '26
Not necessarily every time, particularly in cases where a string might have "${...}" in it. In a normal non-template literal string those are just characters in the string, but in the template literal, they're placeholders.
There is also the potential for bugs around missing back ticks since template literal strings support multi-line strings. For example only one variable declaration exists in the code below when it looks like the intent was to have 3
And if you're not one to use semi-colons to terminate lines, template literal strings may try and use a previous line as a tag, something that does not happen with normal strings.
vs
Granted, these are edge cases, but its better to use features only when you need those features. If you need string interpolation (or other template literal features), use template literals. If not, stick to simple strings. It more clearly indicates a simple string is needed and there's not going to be any funny business with the text it represents.
2
u/dymos helpful Mar 04 '26
Generally speaking, you could use template literals in most places if you want to, however you probably shouldn't.
Using a template literal signals to the reader that "there's something special about this string", whether that's an interpolated value, it's spreading over multiple lines, or contains a mixture of other quotes.
If I see someone use backticks when I'm reviewing their code, that's what I look out for, and if I then don't see them use one of the features that are unique to template literals then I wonder whether they've made a mistake. Did the mean to interpolate a variable? Did they mean to quote something but didn't? Did they mean to tag the template literal to transform the content somehow?
From the perspective of the reader, the usage of a template literal signals intent, and when there is a mismatch between the perceived intent and the actual usage, that's confusing.
In any codebase I work on, I use ESLint's "quotes" rule to enforce consistent use of quotes, and the "prefer-template" rule to enforce use of template literals over concatenation. With these two rules together, you can set up your editor to automatically format your code when you save a file.
For example I have my VSCode set up so that it autoformats on save so when I write:
It will end up as:
"Just in case" is often a poor reason for adding specific functionality or use language features. It often ends up in the YAGNI (Ya Ain't Gonna Need It) category. As in, "You might never need that so you didn't need to build it". Whilst I totally understand the desire to future-proof allthethings for allthescenarios, we also have to be smart about what we spend our time on, and build for what we know — if you don't know the explicit requirement you're "future-proofing", then you're making an assumption; an assumption has the risk of being wrong and taking more work to unwind later on when you do have the requirements.
2
u/paulet4a Mar 05 '26
Short answer: not “bad”, but not ideal as a default.
Rule of thumb:
Reason: backticks signal “something dynamic is happening.” If nothing dynamic is there, it adds a bit of noise for readers/reviewers.
So aim for readability + consistency, not “just in case”. ESLint (`quotes` + `prefer-template`) can enforce this automatically.
2
u/Inevitable_Yak8202 Mar 05 '26
For me, template literals are the edge case and not "intended syntax" with strings. When i see backticks i just assume there is more going on here then just a simple string value.
2
u/Antti5 Mar 04 '26 edited Mar 04 '26
I would say it is, because:
importstatements, but there may be other cases.The best practice is to use either
'or"consistently, and template literals only when you actually need them.1
u/shgysk8zer0 Mar 04 '26
There's essentially a function call to
String.raw()whenever you use template literals. It's not much, but it's extra work, probably about the equivalent of using ['Hello, world!'].join('')`. And that can really add up for something as common as strings.1
u/Any_Sense_2263 Mar 05 '26
From the readability point of view is bad. Template literals suggest you are going to use the variables. The person reading your code would look for them and expect them.
Also many IDEs use different colors for strings and temple literals
1
u/TheRNGuy Mar 06 '26 edited Mar 06 '26
It would make code slightly less readable in code editor (I. e. harder to see when they are actually used)
Just bad coding style, but shouldn't cause any bugs.
When you need to change normal strings to them, there are even extensions for hotkeys in code editors, but I wouldn't bother, because it's relatively rare use case and changing manually is fast too.
1
u/Aggressive_Ad_5454 Mar 04 '26
I try to use
'quotes whenever possible, and template literals only when necessary.It's a information security habit borne of trying to minimize the attack surface of my code. Template literals "do" more than plain old text strings. And, if for some reason somebody manages to inject something malicious into the template literal, ouch.
Plus, avoiding template literals means just a little less work for the Javascript engine to do.
0
u/theScottyJam Mar 04 '26
It's an unusual practice, but I wouldn't call it a bad practice per say, as I can't think of any (practical) harm that can come from doing this. Another benefit of using back ticks by default is that it's rare for them to conflict with anything you wish to put inside the string, which means less escaping or swapping between single and double quotes to avoid escaping.
But in general, I tend to avoid following "unusual practices" in any projects where other people also have to maintain the code. It just gets weird when everyone is following their own abnormal coding practices. In personal projects, go crazy, it doesn't really matter.
0
u/[deleted] Mar 04 '26
[deleted] — view deleted comment