Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Created March 1, 2024 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isurfer21/9e67abd490ea08eb5169c326aca64f16 to your computer and use it in GitHub Desktop.
Save isurfer21/9e67abd490ea08eb5169c326aca64f16 to your computer and use it in GitHub Desktop.
A custom function in EcmaScript to insert the template literals (variables) in the given template string using tagged template. It is helpful in keeping all the templates separately.
function fitIn(template, ...values) {
// console.log(template, values);
let result = [];
for (let i = 0; i < template.length; i++) {
result.push(template[i]);
if (i < values.length) {
result.push(values[i]);
}
}
return result.join("");
}
const firstName = "Abhishek",
lastName = "Kumar";
const result = fitIn`Hello ${firstName} ${lastName}!`;
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment