DEV Community

George Jempty
George Jempty

Posted on • Updated on

The Pro's and Pro's of Javascript String Templates

Yep, you read it right, the Pro's and Pro's (not Con's). This morning I committed code containing a string template as follows:

alert(`You've found the solution (${solution}) !!`);

Actually I started with double-quoted segments and concatenation, so the apostrophe in "You've" didn't cause an issue. But had I started with single quotes, I'd have had to escape that apostrophe.

I knew I shouldn't be doing the string concatenation and so changed to the string template as above. But it dawned on me, that by using a string literal, you don't have to escape either single or double quotes inside the string.

That's just one Pro. I decided to write a JSPerf to be sure there weren't any performance ramifications. But on Chrome anyway, the differences are infinitesimal. YMMV (your mileage may vary) but until then I'm sticking to my guns: Pro's and Pro's ;)

Discussion (0)