ES6 Template Strings

April 15, 2018 Oclemy Javascript Data Types 0 minutes, 27 seconds

Template Strings were introduced in ES 6 and allows us to:

  1. Perform strings concatenation without the + operators.
  2. Insert variables into a string.

Before we were used to concatenating strings using the + or , operators:

console.log(lastName + ", " + firstName + " " + middleName)

However with template strings we can insert variable values into our srings using the ${}:

console.log(`${lastName}, ${firstName} ${middleName}`)

Between the ${} you can add any Javascript expression that returns a value.

Template Strings preserve the whitespaces making it convenient for displaying things like code samples.

With template strings, our statement can span multiple lines of code.

es6

Comments