Skip to content

Instantly share code, notes, and snippets.

@chrismilleruk
Forked from paulrouget/functions.html
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismilleruk/fee4290734a702df458a to your computer and use it in GitHub Desktop.
Save chrismilleruk/fee4290734a702df458a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset=utf-8 />
<title>Defining JavaScript functions, the ES6 way</title>
<h1>Defining JavaScript functions, the ES6 way</h1>
<em>Use Firefox 22 (Firefox Nightly)</em>
<script>
// old school
var square = function(x) {
return x * x;
}
console.log(square(3));
// Expression closures
// doc: http://mzl.la/10TNpzc
// *Edit:* I've been told that Expression Closures are not part of ES6. Still cool.
var square = function(x) x * x;
console.log(square(3));
// Arrow functions
// spec: http://bit.ly/KN3z1c
var square = x => { return x * x; };
console.log(square(3));
// Arrow function + expression closure
var square = x => x * x;
console.log(square(3));
// Destructuring assignment
// doc: http://mzl.la/Xed4Ua
var squareAndCube = x => [x*x, x*x*x];
var [s,c] = squareAndCube(3);
console.log(s + ", " + c);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment