Skip to content

Latest commit

 

History

History
76 lines (68 loc) · 1.57 KB

09_function.md

File metadata and controls

76 lines (68 loc) · 1.57 KB

Arrow function

Also known as lambda expression (a function without a name)

let sum = function (a, b) {return a + b};
let add = (a, b) => a + b;

console.log(sum(3,6)); // 9
console.log(add(3,6)); // 9
let greeting = () => "How's your day?";
function greet() {
    return "How's your day?";
}

console.log(greeting()); // How's your day?
console.log(greet()); // How's your day?

Example

let points = [1, 3, 5];

let addOne = function(element) {
   return element + 1;
}
points = points.map(addOne);
console.log(points); // [2, 4, 6]

points = points.map(element => element + 1);
console.log(points); // [3, 5, 7]

Arrow function ignore this

Normal function

this.color = 'blue';

let showcolor = function () {
    console.log('color', this.color);
}
showcolor(); // color undefined
this.color = 'blue';

let showcolor = function () {
    this.color = 'green';
    console.log('color', this.color);
}
showcolor(); // color green

Arrow function

this.color = 'blue';

let arrowshow = () => {
    console.log('this.color in arrow function:', this.color);
}
arrowshow(); // this.color in arrow function: blue

Filter

Normal function

let isPass = (grade) => {
    return grade >= 60;
}

let scores = [73, 52, 69, 43, 90, 77, 59, 81];
let passing = scores.filter(isPass);
console.log(passing); // [73, 69, 90, 77, 81]

Arrow function

let scores = [73, 52, 69, 43, 90, 77, 59, 81];
let passing = scores.filter(element => element >= 60);
console.log(passing); // [73, 69, 90, 77, 81]