loading...

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

dexygen profile image George Jempty Updated on ・1 min read

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 ;)

Posted on by:

dexygen profile

George Jempty

@dexygen

- Full-stack/front-end web developer since 1999 - Speaker at technical user meetings - Writer of pre-publication technical reviews

Discussion

markdown guide