String Literal in ES6


In ES6 we have new string template literals. Here we are using the back tick for putting the string with variables. Also we can also work on multiple line

Example

Take firstName , lastName , yearOfBirth . Also take a function with name calcAge(year).

Display all records with Es5. ALso with Es6

Solution :

//ES5

var firstName = "Mohan";

var lastName = "Kumar";

var yearOfBirth = 1990;

function calculateAge(year){

   return (2018-year);

}

console.log('The person with name '+firstName+' '+lastName+' born in '+yearOfBirth+' is of age '+calculateAge(yearOfBirth)+' Years.')

 

//Es6

var firstName = "Mohan";

var lastName = "Kumar";

var yearOfBirth = 1990;

function calculateAge(year){

   return (2018-year);

}

console.log(`The person with name ${firstName} ${lastName} born in ${yearOfBirth} is of age   ${calculateAge(yearOfBirth)} Years.`);

Also, give an example of startsWith(), endsWith(), includes(), firstName.repeats(5)

 

Site Footer