this in arrow functions 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 in javascript
//function old way
function PrintName(name){
console.log("my name is ", name) ;
}
// Arrow function es6
const PrintName = (name) => {
return console.log("my name is " , name) ;
}
Example 3: arrow function javascript
//If body has single statement
let myFunction = (arg1, arg2, ...argN) => expression
//for multiple statement
let myFunction = (arg1, arg2, ...argN) => {
statement(s)
}
//example
let hello = (arg1,arg2) => "Hello " + arg1 + " Welcome To "+ arg2;
console.log(hello("User","Grepper"))
//Start checking js code on chrome inspect option
Example 4: javascript arrow function
let errow = () => {
//the code you want to return;
};
Or
let errow = ('paramiter') => {
//the code you want to return
}
Example 5: arrow function javascript
// Traditional Function
function bob (a){
return a + 100;
}
// Arrow Function
let bob = a => a + 100;
Example 6: arrow function javascript
// Traditional Function
function (param) {
var a = param * 3;
return a;
}
//Arrow Function
(a, b) => {
let c = (a * b) + 3;
return c;
}