Saturday, February 24, 2018

JavaScript: ES6 Arrow Functions

1. It is also called “fat arrow” =>
2. These is more concise syntax for writing expressions
3. It doesn’t have its own “this”
4. These are anonymous and change the way “this” binds in functions
5. By using arrow function we can avoid having to type the function keyword, return keyword, and curly brackets ( i.e. it is implicit )

Arrow Function with One Parameter

Traditional Way

function test(param) { return param+10; }
test(10);

New Way

var test=(param) => param + 10 ;
test(10);

Arrow Function with Multiple Parameters

Traditional Way

function test(param1,param2) { return param1 + param2; }
test(10,10);

New Way

var test=(param1, param2) => param1 + param2 ;
test(10,10);

Arrow Function without Parameters

Traditional Way

function test() { return 10; }
test();

New Way

var test=() => 10 ;
test();

Arrow Function with Multiple Statements

Traditional Way

function test() {
      var p1=10;
      var p2=20;
      return p1+p2;
 }
test();

New Way

var test=() => {
       let p1=10;
       let p2=20;
      return p1+p2;
}
test();

Arrow Function with Object

Traditional Way

function test(name,email) {
    return
      {
          Name: name,
          Email: email
      };
  }
test(‘rama’,’rama@gmail.com’);

New Way

var test=(name,email) => ({ Name:name, Email:email});
test(‘rama’,’rama@gmail.com’);

Happy Coding :)

No comments:

Post a Comment