Skip to content

Instantly share code, notes, and snippets.

@dandigangi
Last active September 23, 2018 18:53
Show Gist options
  • Save dandigangi/3de5cb50886e5aaa1653645348d40de3 to your computer and use it in GitHub Desktop.
Save dandigangi/3de5cb50886e5aaa1653645348d40de3 to your computer and use it in GitHub Desktop.
Javascript ES5 vs ES6 Functions
// Expanding on a conversation about being terse with ES5 and ES6 functions on Twitter w/ @podrazque
// Being terse isn't inherently bad but it is important to think about how other developers will infer and understand what was written.
//////
// Example of a terse, one line function using a ES6 fx expression w/ arrows and a implicit return.
const objToArray = (data) => Object.keys(data).map((key) => data[key])
// See below for overview of some different function usage w/ ES5 & ES6
const magic = 'poof';
// ES5 Functions
// Declaration
function doMagic() {
return magic; // Explicit Return
}
// Expression
var doMagic = function() {
return magic; // Explicit Return
}
//////
// ES6
// Declaration
function doMagic2() => {
return magic; // Explicit Return
}
// Expression
const doMagic = () => {
return magic; // Explicit Return
}
// Expression
const doMagic2 = () => magic; // Implicit Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment