Skip to content

Instantly share code, notes, and snippets.

@wiledal
Last active May 4, 2022 14:18
Show Gist options
  • Save wiledal/1888a24fafc11cafba73a8c12ac9d8a0 to your computer and use it in GitHub Desktop.
Save wiledal/1888a24fafc11cafba73a8c12ac9d8a0 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>
@pbanigo
Copy link

pbanigo commented Apr 15, 2022

Worked. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment