DEV Community

Maximilian
Maximilian

Posted on

Why I always use backticks for strings

A quick one from me

If you're using single quotes or double quotes for your strings and not backticks (template literals), then I'm afraid your sanity may be in question.

What happens when you DON'T use backticks

If you're using single quotes (the worst offender) and your string requires an apostrophe, you're back to using double quotes. Now your code has inconsistent string syntax. The reverse is also true, albeit less common - if you're using double quotes and you need double quotes in your string, you're back to using single quotes. In both of these cases, if you ever need to pass in a variable, you're introducing template literals / backticks - a 3rd string syntax.

What happens when you use backticks

There are 2 main advantages:

Firstly, your codebase's string syntax is consistent. You can pass in apostrophes / single quotes and double quotes without thinking -- a freedom you don't appreciate until it's taken away from you -- and secondly, you are free to pass in variables whenever you like without going back and changing those damned quotation marks!

I wondered if people didn't do this because of some kind of performance advantage where template literals would be slower or more expensive, but I've found nothing that supports this theory, so it begs the question:

Why would you use any other syntax?

Top comments (5)

Collapse
 
casraf profile image
Chen Asraf

I see what you're saying, but for me backticks vs quotes provide intent for other readers of your code (whether collaborators or coworkers or your future self).

Interpolated strings will need expressions and possibly variables to fill up their contents.

When you aren't interpolating a string but using it as a constant, that might provide extra clues as to the behavior of code. Is this variable checking for something that can be changed? What happens with different inputs?

This is generally true for reading through code more quickly, but doesn't necessarily make much of a difference when diving deep into each block.

That said, intent still has importance IMO. When code is being refactored, it's crucial to consider what "dependencies" you may have for various data structures and placements.

Literal strings will appear more as constant values, rather than computed values, which may make refactoring results differ.

It's just a matter of opinion imo. In terms of performance, they're pretty much the same as each method has its own pros and cons in different usages so I think they pretty much balance out.

Collapse
 
zoppatorsk profile image
Zoppatorsk

I do agree, I shld use backticks more often to make things more consistent.. for me the reason is I forget about it and also that at least for me requires pressing a total of 3 keys to make a backtick while a single-qoute only requires 1 key.

Collapse
 
saikojosh profile image
Josh Cole

Love this πŸ˜‰
One thing about performance - and another benefit to using backticks - concatenating strings is slower than variable interpolation in JS because there are extra steps to check the types of the operands.

So not only does using backticks reduce noise in git diffs and eliminate the need to escape quote marks, it’s also more performant.

Collapse
 
decker67 profile image
decker

eslint does not allow πŸ˜‚

Collapse
 
saikojosh profile image
Josh Cole

@decker67 it does if you configure it to 😜