JavaScript Template Strings


Template strings are a powerful feature of modern JavaScript released in ES6. It lets us insert/interpolate variables and expressions into strings without needing to concatenate like in older versions of JavaScript. It allows us to create strings that are complex and contain dynamic elements. Another great thing that comes with template strings is tags. Tags are functions that take a string and the decomposed parts of the string as parameters and are great for converting strings to different entities.

   <script>

   var user = "ramesh babu";
   var address = "hyderabad";

   document.write(`My name is ${user} and iam from ${address}`);

   </script>

 <script>

  var user = "ramesh babu";
  var address = "hyderabad";

  // we can use this one in function also
  function mydetails(user,address){
    return `${user} ${address}`;
  }

  var hello = `Hellow ${mydetails(user,address)}`;

  // it will add in the variables also
  // var hello = `My name is ${user} and iam from ${address}`

  document.write(hello);

  // it will print the values in document..
  // document.write(`My name is ${user} and iam from ${address}`);

  // it will print the values in console also
  console.log(hello);

  </script>