Functional equivalent of backtick javascript operator?

What is the functional equivalent of the Backtick Javascript operator?

As explained here , you can perform simple string interpolation in Javascript by doing:

> var n = 42;
undefined
> `foo${n}bar`
'foo42bar'

However, I want to save the "template" itself in a variable. How should I do it? As far as I can tell, the backtick operator only works with literals, not with variables, so I cannot:

> var n = 42;
undefined
> var template = "foo${n}bar";
> `template`
'template'

Is there any functional equivalent of a back table operator that allows passing a variable instead of a string literal, so I could do:

> var n = 42;
undefined
> var template = "foo${n}bar";
> backtick_eval(template)
'foo42bar'
+4
source share

Source: https://habr.com/ru/post/1668330/


All Articles