Backbencher.dev

JS Daily 24 - Template Literal Expression

Last updated on 14 Dec, 2020

What is the output of following code?

const fun = () => "Backbencher";
const str = `My name is ${ fun() }`;
console.log(str);
----o----

The output is "My name is Backbencher".

ES6 introduced template literals. A template literal is enclosed in backticks(``). Inside template literals, we can place JavaScript expressions using ${}. Here, in our example, we invoked a function fun() which returns "Backbencher". That returned value is substituted in the string literal.

--- ○ ---
Joby Joseph
Web Architect