> For the complete documentation index, see [llms.txt](https://ga0-2.gitbook.io/seifxr10anz-content/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ga0-2.gitbook.io/seifxr10anz-content/day-2-functions-and-event-handlers-review/functions-review.md).

# Functions review

Lets review functions!

```javascript
function biggerOf(num1, num2) {
  if (num1 > num2) {
    return num1
  } else {
    return num2
  }
}

console.log(biggerOf(3, 20))
console.log(biggerOf(300, 20))
const biggerNum = biggerOf(23 * 4, 13 * 14))
console.log(biggerNum)
```

What happens if....

* I don't have a `return` at all in the function?
* I try to `console.log(num1)` outside the function? After the function? After the function is called?
* What if I use `const` or `let` to create a variable inside the function?
* How many parameters can I have?
* What happens if I put some code after the `return`?

How do I return multiple bits of information from a function?  Build this function:

```javascript
function longestAndShortestWord(words) {}
```

The transform it into this function, returning an object:&#x20;

```javascript
{ longest: 'sentence', shortest: 'a' }
```

```javascript
function longestAndShortestWord(words) {
  let longestSoFar = "";
  for (let word of words) {
    if (word.length > longestSoFar.length) {
      longestSoFar = word;
    }
  }
  return longestSoFar;
}

longestAndShortestWord(["this", "is", "a", "sentence", "yeah"]);
```

## Arrow functions

An **arrow function expression** is a compact alternative to a traditional [function expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function), with some semantic differences and deliberate limitations in usage:

* Arrow functions don't have their own bindings to [`this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this), [`arguments`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments), or [`super`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super).
* Arrow functions cannot be used as [constructors](https://developer.mozilla.org/en-US/docs/Glossary/Constructor).&#x20;

#### Syntax

```
param => expression

(param) => expression

(param1, paramN) => expression

param => {
  statements
}

(param1, paramN) => {
  statements
}
```

Now lets rewrite the above function as an arrow function

```javascript
const longestAndShortestWord = (words) => {
  let longestSoFar = "";
  for (let word of words) {
    if (word.length > longestSoFar.length) {
      longestSoFar = word;
    }
  }
  return longestSoFar;
};
```

One more example, let write a function to multiply two numbers

```javascript
// Notice the lack of return when there are no wrapping curly brackets
const multiply = (num1, num2) => num1 * num2
```

{% hint style="info" %}
Try rewriting functions you've using the arrow function syntax =>&#x20;
{% endhint %}

### References

* <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions>
* <https://dmitripavlutin.com/differences-between-arrow-and-regular-functions/>
