Forked from wiledal/template-literals-1-if-statements.js
Created
December 4, 2018 14:38
-
-
Save iulia-codes/5f252a7fd86007c8ca91b2f079abc814 to your computer and use it in GitHub Desktop.
Template Literal Examples: if-statement
This file contains 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
/* | |
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