> For the complete documentation index, see [llms.txt](https://guide-angular.wishtack.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide-angular.wishtack.io/ecmascript-6+/arrow-functions.md).

# Arrow Functions

```javascript
/* 90s */
function sayHi(userName) {
    console.log('Hi ' + userName);
}

/* 2000s */
var sayHi = function (userName) {
    console.log('Hi ' + userName);
};

/* 2015 */
const sayHi = (userName) => {
    console.log('Hi ' + userName);
};

/* 2018 */
export function sayHi(userName) {
    console.log('Hi ' + userName);
}
```

## No binding

Les arrow functions ne peuvent être "bound" et accèdent donc au `this` du closure parent.

```javascript
class Customer {

    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    sayHi() {
        console.log('Hi ' + this.firstName);
    }
    
    sayHiLater() {
        setTimeout(() => {
            this.sayHi();
        }, 1000);
    }

}
```

## Exemple avec `Array.filter` et `Array.map`

```javascript
const productList = [
    {
        title: 'Browserstack',
        price: 50
    },
    {
        title: 'Keyboard',
        price: 20
    },
    {
        title: 'Prerender',
        price: 10
    }
];

const cheapProductList = productList.filter((product) => {
    return product.price < 25;
});

const cheapProductTitleList = cheapProductList.map((product) => {
    return product.title;
});

console.log(cheapProductTitleList); // ['Keyboard', 'Prerender']

```

## One-liner

Peu importe le contexte, les fonctions de callback sont souvent des "one-liners".\
Dans ce cas, les accolades, le `return` et le `;` peuvent être retirés.\
De même, si la fonction ne prend qu'un seul paramètre, les parenthèses peuvent être également ignorées.

On peut aussi remarquer le pattern builder des méthodes `filter` et `map` qui nous permet de chaîner les appels.

```javascript
const cheapProductTitleList = productList
    .filter(product => product.price < 25)
    .map(product => product.title);
```

{% hint style="warning" %}
En cas de shadowing de nom de variable, évitez les variables à une lettre ou des noms génériques.

`filter(u => u.id === user.id)`

`filter(it => it.id === user.id)`
{% endhint %}

{% hint style="success" %}
Il est commun de préfixer par un underscore \`\_\` les variables servant à itérer.

`filter(_user => _user.id === user.id`*`)`*
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://guide-angular.wishtack.io/ecmascript-6+/arrow-functions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
