Arrow Functions
Video
JavaScript Notes
JavaScript
// ES6 (fat) Arrow functions
// works best in callback function scenarios
// parentheses around input if more than one
// curly braces around function if more than one line / command
// return only needed if more than one line of code
let numbers = [123, 234, 345, 456, 567]
let names = ['Alex', 'Bree', 'Cara', 'Cole', 'Devon', 'Riley'];
// simple function syntax
// create a new array of values from the array greater than 300
let big = numbers.filter(function(item){
return item > 300;
});
// arrow function version - 1 parameter
let bigA = numbers.filter( item => item > 300);
console.log(big);
console.log(bigA);
// simple function syntax
names.forEach( function (item, index) {
console.log(index, item);
})
// arrow function version - 2 parameters
names.forEach ( (item, index) => console.log(index, item) );
Feedback
Submit and view feedback