what is an arrow function javascript code example
Example 1: javascript arrow function
// Non Arrow (standard way)
let add = function(x,y) {
return x + y;
}
console.log(add(10,20)); // 30
// Arrow style
let add = (x,y) => x + y;
console.log(add(10,20)); // 30;
// You can still encapsulate
let add = (x, y) => { return x + y; };
Example 2: arrow function javascript
// an arrow function is also called a lambda or an anonymous function
let myFunction = () => {
// some logic
}
Example 3: javascript this inside arrow function
function A() {
this.val = "Error";
(function() {
this.val = "Success";
})();
}
function B() {
this.val = "Error";
(() => {
this.val = "Success";
})();
}
var a = new A();
var b = new B();
a.val // "Error"
b.val // "Success"
Example 4: js arrow function
hello = () => {
return "Hi All";
}
Example 5: arrow function javascript
hello = () => {
return "Hello World!";
}