Arrow Functions

A function is a block of code, self-contained, that can be defined once and run any time you want. A function can optionally accept parameters and returns the value. If you have been using JavaScript in past then you are probably familiar with this traditional way to write functions in JavaScript.

Traditional Function -->

let a = 22;
let b = 37;

function ArrowFunc() {
  return a + b;
}

In 2015 new version of JavaScript was introduced to the world which knows as ES6. So arrow function is one of the best features of ES6. This is how we can Write the Arrow function in JavaScript

Arrow Function -->

let a = 22;
let b = 37;

const ArrowFunc = () => {
  return a + b;
};

This Arrow Function syntax allows an implicit return when there is no body block. Arrow Function syntax automatically binds .this keyword and it is a shorter way to write function. In Arrow Function we can also remove return and still, code will get executed as shown below.

Arrow function without return statement -->

let a = 22;
let b = 37;

const ArrowFunc = () => a + b;

As we know whenever we write a function in code we can pass the value to that function also with the help of parameters. so as shown in below example we can pass the parameters in Arrow Function.

Arrow function with Parameters -->

let a = 22;
let b = 37;

const ArrowFunc = (num3) => {
  return a + b + num3;
};

As we saw in the above Arrow Function example when we pass parameters to Arrow Function we write Parentheses also but in Arrow function, we can remove those Parentheses also and still code will work as shown below.

Arrow function with parameters but without Parentheses -->

let a = 22;
let b = 37;

const ArrowFunc = num3 => {
  return a + b + num3;
};

So guys This is a short introduction to Arrow Function in JavaScript (ES6). I hope you got some knowledge through this blog.

Thank You ! I'll see you in the next blog

Have a nice day! ๐Ÿ˜‰

Everything you can imagine is REAL โœจ

ย