Created
March 1, 2024 16:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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