Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active August 16, 2018 15:19
Show Gist options
  • Save rjcorwin/795fcc4f6899a337bcc5a9967557dcf2 to your computer and use it in GitHub Desktop.
Save rjcorwin/795fcc4f6899a337bcc5a9967557dcf2 to your computer and use it in GitHub Desktop.
//
// Conditional statements.
//
// Simple.
var text1 = `Numbers are great, ${1 === 1 ? '1 is equal to 1' : '1 is not equal to 1. Uh oh.'}.`
console.log(text1)
// Advanced.
var text2 = `Numbers are great, ${(()=>{
if ( 1 === 2 ) return `1 is equal to 2`;
else if ( 1 === 1 ) return `1 is equal to 1`;
})()}.`
console.log(text2)
//
// Iterate on arrays.
//
// Simple.
var words = ['This', 'is', 'an', 'array']
var sentence = `Here is an example of iterating through an array. ${words.map(word => `${word} `).join('')}.`
console.log(sentence)
// Advanced.
let shoppingList = ['milk', 'bread', 'cheese']
var markup = `
<h2> Shopping List </h2>
<ul>
${shoppingList.map(item => `
<li>
${item}
</li>
`).join('')}
</ul>
`
console.log(markup)
// http://wesbos.com/template-strings-html/
// https://ponyfoo.com/articles/es6-template-strings-in-depth
// https://www.keithcirkel.co.uk/es6-template-literals/
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment