DEV Community

Cover image for Using Backticks as Syntactic Sugar in JavaScript and Scheme
Justin Ethier
Justin Ethier

Posted on

Using Backticks as Syntactic Sugar in JavaScript and Scheme

ECMAScript 6 introduced the concept of template literals to provide a convenient way to build up strings in JavaScript.

For example:

> let h = "Hello", w = "World";
> `${h} ${w}!
  ${h} ${w} from line 2`

"Hello World!
Hello World from line 2"
Enter fullscreen mode Exit fullscreen mode

This is reminiscent of using backticks in Scheme to construct lists using quasiquotation:

> (let ((h 'hello)
      (w 'world))
  `(,h ,w !))

(hello world !)
Enter fullscreen mode Exit fullscreen mode

Maybe its not a coincidence that template literals were called quasi-literals when originally proposed:

This scheme extends EcmaScript syntax with syntactic sugar to allow libraries to provide DSLs that easily produce, query, and manipulate content from other languages that are immune or resistant to injection attacks such as XSS, SQL Injection, etc.

Especially since there are no other commonly-used languages that use backticks for string interpolation.

Anyway, thought this was an interesting observation on the evolution of the JavaScript language.

Top comments (0)