Skip to content

Instantly share code, notes, and snippets.

@edwingustafson
Last active June 29, 2018 17:41
Show Gist options
  • Save edwingustafson/2a6ddfa178ecef4d07bdcba3892fb648 to your computer and use it in GitHub Desktop.
Save edwingustafson/2a6ddfa178ecef4d07bdcba3892fb648 to your computer and use it in GitHub Desktop.
Condition assignment with and without variable bindings and logic
// conditionally assign variable binding
let coinflip1;
if( Math.random() > 0.5 ) {
coinflip1 = 'heads';
} else {
coinflip1 = 'tails';
}
// conditionally assign constant binding
const coinflip2 = Math.random() > 0.5 ? 'heads' : 'tails';
// awkward, but possible to perform logic in the branches via IIFE
const cointflip3 = Math.random() > 0.5 ?
'heads' :
(() => {
console.log('Ouch!');
return 'tails';
})()
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment