Template literals for string interpolation
Template literals for string interpolation
JavaScript template literal syntax.
Template literals in JavaScript programs are enclosed by backticks (`), and programmers can manually insert variables or expressions inside ${}.
`Test, ${expression}!`
Basic string interpolation in JavaScript.
Instead of concatenating strings by applying the + operator in a JavaScript program, you can apply template literals to insert variables directly into a string data type variable.
Example of interpolation in JavaScript.
let programming = “Javascript”;
let price = 999;
let text = `Welcome, to ${programming} and course price is – ${price}`;
console.log(text); // result is – Welcome, to Javascript and course price is – 999
Here, ${programming} and ${price} are two placeholder variables in the current program, which are replaced by the programming and price variable values in the current program.
Expression interpolation in JavaScript.
Javascript programmers can manually insert any valid program expression inside ${} along with program variables. It contains program calculations, user-defined function calls, and many other features.
Example of expression interpolation in JavaScript.
let p = 1;
let q = 2;
let total = `${p} + ${q} = ${p + q}`;
console.log(total); // result is – 1 + 2 = 3
In the above program, the program expression here contains the evaluated 3 results of ${p + q}, and the program result is previewed in the string.
Continue Reading On - https://vcanhelpsu.com














