Created
December 10, 2019 19:11
-
-
Save paulmiller3000/41bce98df0c9a3dcf275bc589f04cce7 to your computer and use it in GitHub Desktop.
How to Return Two Values to a Function in ES6
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>How to Return Two Values to a Function in ES6</title> | |
</head> | |
<body> | |
<div class='container'> | |
<form action='#' onsubmit='funFunction();return false'> | |
<select name='vehicle-selector' id='vehicle-selector' required /> | |
<option value='BaseStar'>BaseStar</option> | |
<option value='Raptor'>Raptor</option> | |
<option value='Viper' selected='selected'>Viper</option> | |
<input type='submit' value='Submit'> | |
</form> | |
</div> | |
</body> | |
<script type='text/javascript'> | |
const isSpaceship = (ele) => { | |
if (ele) { | |
console.log('Happy path. Ele exists.'); | |
let selectedOption = ele.options[ele.selectedIndex].text; | |
if ( selectedOption == 'Raptor' || selectedOption == 'Viper') return { ready: true, value: selectedOption }; | |
} | |
return { ready: false }; | |
} | |
const funFunction = () => { | |
const documentStatusElement = document.getElementById('vehicle-selector'); | |
let { ready, value } = isSpaceship(documentStatusElement); | |
switch (value) { | |
case 'Raptor': | |
console.log(`value: ${value}. Plan to do The Special. But first do this thing.`); | |
break; | |
case 'Viper': | |
console.log(`value: ${value}. Plan to do The Special. But first do that thing.`); | |
break; | |
default: | |
console.log(`value: ${value}. Do this other thing.`); | |
} | |
ready == true | |
? console.log(`ready: ${ready}. Do The Special.`) | |
: console.log(`ready: ${ready}. Do this other thing.`); | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment