# 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/>


---

# Agent Instructions: 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:

```
GET https://ga0-2.gitbook.io/seifxr10anz-content/day-2-functions-and-event-handlers-review/functions-review.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
