2018-10-04 14:47:55 +01:00
---
title: Arrow Functions
---
## Arrow functions
2019-02-24 03:04:53 -05:00
ES6 has introduced a new syntax for defining a function.
2018-10-04 14:47:55 +01:00
```javascript
// Old Syntax
function oldOne() {
console.log("Hello World..!");
}
// New Syntax
2018-10-18 20:31:33 -05:00
const newOne = () => {
2018-10-04 14:47:55 +01:00
console.log("Hello World..!");
}
2018-10-21 07:35:33 +01:00
// Or on one line
const newOne = () => console.log("Hello World..!");
2018-10-04 14:47:55 +01:00
```
2018-10-24 22:52:59 +02:00
The new syntax may be confusing. There are two major parts of it.
2018-10-04 14:47:55 +01:00
2018-10-18 20:31:33 -05:00
1. const newOne = ()
2018-10-04 14:47:55 +01:00
2. => {}
2018-10-18 20:31:33 -05:00
The first part is just declaring a variable and assigning the function (i.e) () to it. It just says the variable is actually a function. The `const` keyword is used to indicate that the function won't be reassigned. Refer [this ](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/guide/english/javascript/es6/let-and-const/index.md ) to learn more about `const` and `let` .
2018-10-04 14:47:55 +01:00
Then the second part is declaring the body part of the function. The arrow part with the curly braces defines the body part.
Another example with parameters:
```javascript
let NewOneWithParameters = (a, b) => {
console.log(a+b); // 30
}
NewOneWithParameters(10, 20);
```
Parentheses are optional when there's only one parameter name:
```javascript
let newOneWithOneParam = a => {
console.log(a);
}
```
2018-11-26 02:20:59 -06:00
When there is only one statement or operation in the function body, braces are optional and the result is returned or undefined.
```javascript
let a = 10;
let newOneParamWithNoBrackets = b => a + b;
console.log(newOneParamWithNoBrackets(20)); // 30
```
2018-10-12 04:30:38 +05:30
An incredible advantage of the arrows function is that you can not rebind an arrow function. It will always be called with the context in which it was defined. Just use a normal function.
```javascript
// Old Syntax
axios.get(url).then(function(response) {
this.data = response.data;
}).bind(this);
// New Syntax
axios.get(url).then(response => {
this.data = response.data;
});
```
2019-02-24 03:04:53 -05:00
For more information on arrow functions visit [MDN ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions ).