Forked from wiledal/template-literals-1-if-statements.js
Created
October 10, 2017 03:29
-
-
Save antenando/ae4f44c117459dfe165dcd05d9d47972 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