Last active
May 30, 2019 23:46
-
-
Save anthonycoffey/4d9ebd04e5413428e253d539b088c00b to your computer and use it in GitHub Desktop.
Template Literals and String Interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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