6 ms·
In ES6 (and earlier, in E) you can write variable_name`string with ${expression} in it`, where the value of variable_name must be a function. The function gets
by abecedarius 10y ago
In ES6 (and earlier, in E) you can write variable_name`string with ${expression} in it`, where the value of variable_name must be a function. The function gets called with an array of the substrings between the ${expression} parts, plus the values of the expressions. The function can return anything, including a non-string value. (So e.g. you might define dom so that dom`<p>Hello world` returns a new DOM node.) If you write just `string with ${e}` then there's a default for the variable_name part which does simple interpolation, like Python's f-strings.
So it's a powerful way of embedding other languages in the host language, where f-strings are not. The quasiParserGenerator library I linked to shows some of the potential.