3.17 Arrow Function

var square = (x) => {
var result = x * x;
return result;
}

- Running console.log(square(9)); outputs 81

var square = (x) => x * x;

var user = {
name : 'Jude',
sayHi : () => {
console.log('Hi');
}
}

This example has no problems at all. But the key point is that arrow functions do not bind this.

var user = {
name : 'Jude',
sayHi : () => {
console.log(`Hi. I am ${this.name}`);
}
}
user.sayHi();

We get the output Hi. I am undefined.

Solution is

var user = {
name : 'Jude',
sayHiAlt () {
console.log(`Hi. I am ${this.name}`);
}
}
user.sayHiAlt();
var user = {
name : 'Jude',
sayHiAlt () {
console.log(arguments);
console.log(`Hi. I am ${this.name}`);
}
}
user.sayHiAlt(1, 2, 3);

We get

{ '0': 1, '1': 2, '2': 3 }
Hi. I am Jude

This works because we are using the normal function. If we are using the arrow function, this will not work.