0

Currently there are three ways of declaring strings in ES6 that I am aware of.

String Template Literals

`Hello World!`

Pros: Easy to extend with dynamic content (${}). Looks most appealing with dynamic content.

Cons: Looks very strange and pointless when used without ${}. Awkard to use since it prints only after another character has been typed and also can add unwanted accents when typed carelessly (o -> รณ)

I am using a german keyboard, so I am not 100% sure if those problems apply to other langauge specific keyboards too.

Single Quotes

'Hello World!'

Pros: Very minimal and clean

Cons: Used for single characters ('a', 'b', 'c') in other langagues (e.g. C#)

Double Quotes

"Hello World!"

Pros: Very familiar and used in other langauges the same way (e.g. C#)

Cons: Visually, takes up most space and looks less "clean" (this might be just my opinion).

Thoughts

I want to keep my code clean and consistent. Switching between those versions feels awkard and unclean to me.

What are your opinions? Which version do you prefer and why? Did I miss any pros or cons?

I also wonder if there are any downsides considering performance when using those different methods, especially looking at string template literals.

oemera
  • 3,223
  • 1
  • 19
  • 33
  • 2
    No point in using a template literal if there are no substitutions in it or you don't need the multi-line aspect. To do so, just creates more work for the run-time to evaluate it (searching it for template directives). Whereas single and double quote strings can be parsed and evaluated once when the code is first parsed because their full value is already known at that time. This would be somewhat analogous to using `sprintf()` on every string in C, even when there were no %x characters in the string that `sprintf()` could use - just wasteful. โ€“ jfriend00 Jan 28 '20 at 09:32
  • 1
    What you are referring to regarding backticks is called a "dead key": https://en.wikipedia.org/wiki/Dead_key . I believe it should be possible to disable this behavior but I could be wrong. โ€“ Felix Kling Jan 28 '20 at 09:37
  • 1
    @jfriend00: I think engines could easily optimize the "no interpolation" case. After parsing the engine knows whether the template literal contains dynamic parts or not. But I agree with you regardless :) โ€“ Felix Kling Jan 28 '20 at 09:38

0 Answers0