- ES6 provides template string to render JavaScript expression.Template strings are string literals allowing embedded expressions.
- Template strings are enclosed by the back-tick ``.
- The expressions are included with dollar sign and curly braces as ${}.
- In this demo, “We will learn to use ES6 template string”.
- The following code contains a student detail object aStudent with name,subject and country property with detail value.the name,subject and country values are represented as ${aStudent.name},${aStudent.subject} and ${aStudent.country}.
<!DOCTYPE html> <html> <head> <script src="http://static.jsbin.com/js/vendor/traceur.js"></script> <meta charset="utf-8"> <title>Traceur ES6 Template Feature</title> </head> <body> <script> var aStudent = { name:"Sandeep Kumar Patel", subject:"Computer", country:"India" }; var studentDetail = `Name: ${aStudent.name} Subject: ${aStudent.subject} Country: ${aStudent.country}`; console.log(studentDetail); </script> </body> </html>
- The output of the previous code can be found in the following JSBIN link.