Skip to content

Instantly share code, notes, and snippets.

@anthonycoffey
Last active May 30, 2019 23:46
Show Gist options
  • Save anthonycoffey/4d9ebd04e5413428e253d539b088c00b to your computer and use it in GitHub Desktop.
Save anthonycoffey/4d9ebd04e5413428e253d539b088c00b to your computer and use it in GitHub Desktop.
Template Literals and String Interpolation
var welcome = `Welcome!` // Welcome!
var name = 'John Doe'
var greeting = `Hello, ${name}.` // Hello, John Doe.
// you can use more than one variable in a template literal
var month = 5
var day = 30
var year = 2019
var displayDate = `${month}-${day}-${year}`
console.log(displayDate) // 5-30-2019
// we can also use expressions inside of the dollar sign and curly braces
console.log(`${month}-${day + 1}-${year}`) // 5-31-2019
// using template literals, we can break lines without breaking our code!
var multipleLines = `
${welcome}
${greeting}
`
console.log(multipleLines) // Welcome! Hello, John Doe.
// you can still do string interpolation without template litearls, but it's not as clean...
console.log(month + '-' + day + '-' + year) // 5-30-2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment