> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/languages/javascript/functions.md).

# Functions

```javascript
let greeting = function(){
    return 'Hello, World!';
}

let message = greeting();
console.log(message);            //Hello, World!
```

```javascript
let greeting = function greet(name){
    return 'Hello, ' + name;
}

let message = greeting('World!');
console.log(message);                //Hello, World!
```

## Arrow Functions&#x20;

* a.k.a. Anonymous functions, or Lambda functions.
* Introduced in ES6.
* Arrow functions do not have their own `this` value.

```javascript
let greeting = () => { 
    return 'Hello, World!'; 
}

let message = greeting();
console.log(message);        // Hello, World!
```

```javascript
let greeting = (name) => 'Welcome ' + name + ' to the World of JavaScript!';

let message = greeting('Jerry');
console.log(message);        // Welcome Jerry to the World of JavaScript!
```

```javascript
let msg = {
    name: "John",
    regularFunction: function(){
        console.log('Hello, ' + this.name);
    },
    arrowFunction: () => console.log('Hi ' + this.name)
}

msg.regularFunction();        //Hello, John
msg.arrowFunction();          //Hi
```

Here in this example, when we call `this.name` from `msg.arrowFunction()`, it looks for the `name` variable in the global context and cannot find it. Therefore only Hi gets printed. if we try to print `this.name` it will return an empty string.

> Generators
>
> * a Generator function is a function that generates iterator.

## Immediately Invoked Function Expression (IIFE)

* a.k.a. a Self-Executing Anonymous Function.
* a JavaScript function that runs as soon as it is defined.
* let us group our code and works in isolation, independent of any other code.

```javascript
(function(){                                // IIFE
    console.log('Hello, World!');
})();

(() => {                                    // Arrow Function IIFE
    console.log('Hello, World!');
})();

(async () => {                              // async IIFE
    console.log('Hello, World!');
})();
```

{% hint style="info" %}
`this` keyword refers to the owner of the function we are executing.
{% endhint %}

## Async Functions

Promise

* When resolved it makes the result available
* States: Pending, Fulfilled, Rejected
* Promise.all - takes multiple promises and returns a promise which resolves when all supplied promises are done.
* Promise.race - is the same as Promise.all but it resolves when first promise is done.

### Shared Array Buffers & Atomics

* An ArrayBuffer instance represents a piece of memory: binary data
* Typed arrays are needed to access data in an ArrayBuffer
* The SharedArrayBuffer is an ArrayBuffer for which underlying data can be shared across threads
* Atomics global variable provides a collection of tools to work with the SharedArrayBuffer
