Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antenando/ae4f44c117459dfe165dcd05d9d47972 to your computer and use it in GitHub Desktop.
Save antenando/ae4f44c117459dfe165dcd05d9d47972 to your computer and use it in GitHub Desktop.
Template Literal Examples: if-statement
/*
Template literals if-statement example
Using a single-line conditional, we can create an if-statements within template literals.
*/
function makeHTML(title) {
return `
${title ? `
This element has a title, and it is "${title}"
` : `
This element does not have a title.
`}
`
}
var element1 = document.createElement('div')
var element1.innerHTML = makeHTML('This is a title!')
// Result: <div>This element has a title, and it is "This is a title!"</div>
var element2 = document.createElement('div')
var element2.innerHTML = makeHTML()
// Result: <div>This element does not have a title.</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment