es6 template string example

In javascript es5, this is illegal because the new line \n is a line feed.

var str = 
"I am a 
programmer ";

To make it work, each new line has to be concatenated.

var str = 
"I am a " +
"programmer ";

Template string is a new feature introduced in es6 what allows the use of string interpolation. The snippet below declares a person object with name, career and location properties. The values of these properties can be directly referenced by putting it in between a pair of curl brackets with the dollar sign at the beginning. The entire sentence or the string you want to compose has to be wrapped in a pair of backticks, the key is usually the same key for tilde, which is located on the top left corner under the esc key on the keyboard.

let person = {
	"name" : "Amy",
	"career": "Model",
	"location": "New York"
let sentence = 
Hi, my name is ${}. 
I am a ${} living in ${person.location}. 
// Hi, my name is Amy. 
// I am a Model living in New York

Search within Codexpedia

Custom Search

Search the entire web

Custom Search