Created
May 2, 2018 20:39
-
-
Save sarahquigley/b7e4d1c4c4ec49009a1ecdfa22ecc6d7 to your computer and use it in GitHub Desktop.
Example of ES6 Default Function Parameters
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
/* ES5 */ | |
function dogExplainsCats (behaviour, reason) { | |
if (reason === undefined) { | |
reason = 'evil'; | |
} | |
return 'Why cats ' + behaviour + '? Because cats ' + reason + '.'; | |
} | |
dogExplainsCats('poke you in face when sleeping'); | |
// > 'Why cats poke you in face when sleeping? Because cats evil.' | |
/* >= ES6 */ | |
function dogExplainsCats (behaviour, reason = 'evil') { | |
return `Why cats #{behaviour}? Because cats #{reason}!`; | |
} | |
dogExplainsCats('poke you in face when sleeping'); | |
// > 'Why cats poke you in face when sleeping? Because cats evil.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment