Last active
September 23, 2018 18:53
-
-
Save dandigangi/3de5cb50886e5aaa1653645348d40de3 to your computer and use it in GitHub Desktop.
Javascript ES5 vs ES6 Functions
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
// 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