Working on node.js (server side), I wonder if I should use all back-ticks (`) instead of the regular quotes (" or ') all the time since it will make the code consistent. Is there any specific reason for keeping different type of quotes for different things. Will it affect performance if all non-substitution quotes are converted to back-ticks?
-
2Black tick also know as template literals have multiple usage other than just enclosing a string.Black ticks come with features like placeholder , interpolation,multiline string . You can decide which will be useful depending on the project requirement – brk Nov 02 '17 at 03:57
-
2@brk I think the OP is clear on what the back tick can do. They're asking what the impacts are to use all back ticks project wide to keep the code consistent. – alexisSchreier Apr 25 '19 at 20:46
3 Answers
the back-tick allows you to use string templating for example:
var value = 4;
var str = `text with a ${value}`
// str will be : 'text with a 4'
for "
vs '
I say look at this post: https://stackoverflow.com/a/9959952/6739517
As for performance, it seems like it would be the same if you are just using back-ticks for plain strings. However when building strings it looks like concatenation is still the way to go. Take a look here:
2018 update: It seems that ES6 string templating can be faster than concatenation in some cases. Take a look at the following post for some hard numbers:
Are ES6 template literals faster than string concatenation?
2020 update: Generally speaking you should not be worried about performance when considering which type of quotation to use. There might be tiny differences but as many have pointed out these are such tiny improvements you are likely better off optimizing your code rather than considering which character to use. That said, this doesn't really answer the question of consistency. For me, I generally follow Airbnb's style guide. That is strings are always declared with single quotes (') unless you need to build a string then you should avoid concatenation and only use string templating with backticks (`). Finally, double quotes are reserved for JSON and HTML.

- 3,911
- 2
- 17
- 29
-
I know there's no single guide that will please everyone, but as worried as AirBNB is about "code searchability" (literally the only reason given for: "no long-line concatenation" and "no unnecessary escapes"), why not use backticks all the time? Backticks make escaping quotes unnecessary, and I have to imagine single/double quotes in strings are more common than backticks for AirBNB (and almost everyone else). – jmorganmartin Oct 28 '21 at 23:19
-
1@jmorganmartin If I had to guess, I'd say it's because most typists find it easier and more natural to reach the quote key than the backtick key. While it may be a negligible amount of actual savings to the bottom line, appeasing us fickle programmers can mean a lot as well. I hate when I have to reach for the backtick and I'm happy we only use it for templates. – iamtheddrman Nov 02 '21 at 02:38
The performance difference between creating strings using backticks or single quotes would be so absurdly small that I don't think you should consider it as a reason to use one or the other. Here is some basic evidence of that.
However - I would argue against using template string for strings that are not templates simply because it's clear when you use single quotes that no templating is going to occur. If I saw a string with backticks - I would immediately start hunting to see what was going to be substituted, or why they were used. By contrast, single quotes are very clear.
I don't think it will affect performance - but I don't think you should do it "just to be consistent" either. It's not consistent because it's a completely different construct - the same as a while
or for
loop. They are different tools for different jobs.

- 8,749
- 4
- 47
- 57
-
4This is the only answer that actually answers the "spirit" of the question! – BIOStheZerg Mar 11 '20 at 14:18
-
@BIOStheZerg thanks for noticing. I was confused at the answers explaining what a template string was, especially as the asked clearly already knew... no idea what he accepted one of them. – Shadow Mar 12 '20 at 12:19
Back ticks(``) are called template literals. They are part of ES6. Difference is:
var name = "world";
var greetES5 = 'Hello '+name;//using single quote
var greetES6 = `Hello ${name}`;//using ticks
You can refer MDN here for more info.

- 939
- 10
- 13
-
-
9This part I already understand but this is not what I asked. My question was that is there any performance difference between `x` `=` `\`test\`` and `x = 'test'` (where there is no conversion of variable involved)? If there is no difference then its better to use backtick (`) all over the code for consistency and maintainability. – Ulysses Apr 26 '19 at 04:04