Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save santiago-puchginer-snkeos/d9c6107a40c10aa7e0d498b29e2ec19b to your computer and use it in GitHub Desktop.
Save santiago-puchginer-snkeos/d9c6107a40c10aa7e0d498b29e2ec19b to your computer and use it in GitHub Desktop.
Arrow functions in Javascript (ES6)
// Snippet for arrow functions (ES6)
// Example of a normal arrow function
let substract = (a, b) => {
return a - b;
}
// Example of a one-line arrow function
let add = (a, b) => a + b;
// Another useful example of arrow functions
let nums = [2, 3, 4, 5];
let doubled = nums.map((n) => 2*n);
// Be aware that arrow functions have parent lexical scope, that is, "this"
// keyword refers to the parent of the current Object, not the Object itself
let person = {
name: "Ryan",
hobbies: ["Robots", "Reading"],
showHobbies() {
this.hobbies.forEach( (hobby) => console.log(`${this.name} likes ${hobby}`) );
}
}
person.showHobbies();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment