Javascript String Template Literals with Functions

Introduction

Template Literals with Functions are called tagged template literals.

With these, you can modify a template literal using a function.

function countdown(stringLiteralArray, ...values){ 
       console.log(stringLiteralArray); 
       console.log(stringLiteralArray[1]); 
       console.log(values); //w  ww .  j av  a  2s . c o m
       console.log(values[0]);  
       console.log(values[1]);  

  let fullSentance = values[0] + 
                     stringLiteralArray[1] + 
                     values[1] + 
                     stringLiteralArray[2]; 
  return fullSentance; 
} 
let one = 1; 
let two = 2; 
let results = countdown `${one} Mississippi ${two} Mississippi`; 
console.log(results); //returns 1 Mississippi 2 Mississippi 



PreviousNext

Related