Formatted Strings Using Template Strings in JavaScript

Following is the code for formatted strings using template strings in Javascript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .sample {
      font-size: 20px;
      font-weight: 500;
      color: blueviolet;
   .sample {
      color: red;
<h1>Formatted Strings Using Template Strings JavaScript</h1>
<div class="sample">
`The person name is ${}. His age and rollno are
${personObj.age} and ${personObj.rollno} respectively`
<div class="result"></div>
<br />
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to see the template string formatted with the
personObj values</h3>
   let BtnEle = document.querySelector(".Btn");
   let resEle = document.querySelector(".result");
   let personObj = { name: "Rohan Sharma", age: 12, rollno: 22 };
   let arr = [22, 55, 11, 19, 55];
   BtnEle.addEventListener("click", (event) => {
      resEle.innerHTML = `The person name is ${}. His age and rollno are
      ${personObj.age} and ${personObj.rollno} respectively`;


On clicking the ‘CLICK HERE’ button −

Updated on: 18-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started